我們正在致力於更新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
等
我不知道爲什麼它這樣做。有人有主意嗎?
這是一個更透徹的解釋比我提供什麼,而只是發表評論'$空='或'[空]'是最好的方法以處理擺脫不必要的輸出。 '| Out-Null'明顯變慢。 – 2014-09-30 16:57:25
你是對的,它是。就我個人而言,我更喜歡它的讀法,我從來沒有遇到性能問題,所以我只是堅持下去。 – Travis 2014-09-30 16:59:17
謝謝你們倆。我嘗試了兩種方式,並沒有注意到性能的差異。但我仍然會使用'$ null ='方法。 – Shawrich 2014-09-30 18:46:43