2014-09-30 33 views
0

我們正在致力於更新AD中的scriptPath屬性。我們正在更新小組的員工(一次約100-200人)。爲此我創建了以下腳本。Powershell腳本中的不明原因數字輸出

$newScript = "foo.vbs" 

# Load Employee List 
$employeeList = Get-Content "NAM_logon_EmployeeList.txt" 

$objDomain = New-Object System.DirectoryServices.DirectoryEntry("LDAP://OU=Users,DC=foobar,DC=com") 
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher 
$objSearcher.SearchRoot = $objDomain 
$objSearcher.PageSize = 100 
$objSearcher.SearchScope = "Subtree" 
$colProplist = "scriptPath", "distinguishedName", "cn" 

# Loop through Employee List and update the script value 
ForEach ($employee In $employeeList) { 

    $objSearcher.Filter = "(&(objectCategory=person)(objectClass=user)(mail=$employee))" 
    Foreach ($colProp in $colPropList) { 
     $objSearcher.PropertiesToLoad.Add($colProp) 
    } 
    $colResults = $objSearcher.FindAll() 

    ForEach ($user In $colResults) { 
     $ntuser = $user.Properties.Item("distinguishedName") 
     $myUser = $user.Properties.Item("cn") 

     Script to Pushout the change 
     $objUser = [ADSI]"LDAP://$($ntuser)" 
     $objUser.put("scriptPath", $newScript) 
     $objUser.SetInfo() 
     echo "Script Added for $($myUser)" 
    } 

} 

該腳本工作正常,但第18行:

$objSearcher.PropertiesToLoad.Add($colProp) 

輸出數字到PowerShell窗口。每個對象和屬性添加一個數字。

0 
1 
2 
Script Added for Smith, John 
4 
5 
6 
Script Added for Doe, Jane 

我不知道爲什麼它這樣做。有人有主意嗎?

回答

0

這只是輸出,命令正在輸出,因爲它更新對象。很多.net對象都會這樣做。如果你不希望看到的輸出執行以下操作:

$null = $objSearcher.PropertiesToLoad.Add($colProp) 
0

從文檔中發現on MSDN:

返回值

在新元素插入的從零開始的索引。

因此,您所看到的數字是要添加元素的索引。 忽視的輸出那樣,我喜歡做如下:

$objSearcher.PropertiesToLoad.Add($colProp) | Out-Null 
+0

這是一個更透徹的解釋比我提供什麼,而只是發表評論'$空='或'[空]'是最好的方法以處理擺脫不必要的輸出。 '| Out-Null'明顯變慢。 – 2014-09-30 16:57:25

+0

你是對的,它是。就我個人而言,我更喜歡它的讀法,我從來沒有遇到性能問題,所以我只是堅持下去。 – Travis 2014-09-30 16:59:17

+0

謝謝你們倆。我嘗試了兩種方式,並沒有注意到性能的差異。但我仍然會使用'$ null ='方法。 – Shawrich 2014-09-30 18:46:43