我有一個CSV文件中約1000個用戶名的列表,我需要檢查它們是否啓用。我似乎無法找到關於如何做到這一點的教程,而不使用第三方管理單元,這不是一個選項。PowerShell腳本來檢查是否從CSV文件啓用帳戶
看起來這應該是一個相當簡單的腳本,但我似乎無法做到正確。
function Test-UserAccountDisabled
{
param($account)
$searcher = new-object System.DirectoryServices.DirectorySearcher
$searcher.filter = "(sAMAccountName=$Account)"
$user=$searcher.FindOne().GetDirectoryEntry()
if($($user.userAccountControl) -band 0x2){$true}else{$false}
}
$file = Select-FileDialog -Title "Select a file" -Directory "C:\" -Filter "All Files (*.*)|*.*"
$users = Import-Csv $file
foreach($account in $users)
{
Test-UserAccountDisabled($account)
}
它返回「你不能調用一個空值表達式的方法。」我在這裏做錯了什麼?
請注意,您在powershell中調用與cmdlet相同的函數,而不需要parens。 'Test-UserAccountDisabled $ account' **不是**'Test-UserAccountDisabled($ account)'。 Parens只適用於.NET方法。這可能是問題的一部分。 – latkin
在這種情況下,結果是相同的,因爲只有一個參數。 – Joey
發生什麼錯誤? –