非常直接的問題:我不知道爲什麼「physicalDeliveryOfficeName」屬性沒有顯示在我的輸出中。我讀過它是一個非標準的屬性,但我一直無法找到添加它的方法。除了缺少「physicalDeliveryOfficeName」之外,一切都很完美。謝謝您的幫助!Powershell LDAP - physicalDeliveryOfficeName沒有顯示
$Dom = 'LDAP://OU=XX;DC=XX;DC=local'
$Root = New-Object DirectoryServices.DirectoryEntry $Dom
$selector = New-Object DirectoryServices.DirectorySearcher
$selector.SearchRoot = $root
$selector.pagesize = 1000
$adobj= $selector.findall() | where {$_.properties.objectcategory -match "CN=Person"}
(Get-Content c:\FILENAME.txt) | Foreach-Object `
{ `
foreach ($person in $adobj){
$prop=$person.properties
if ($prop.cn -like "*" + $_.substring(1, 3) + "*")
{
$s1 = $_ -replace $_.substring(0, 4), $prop.cn
$s2 = $s1 -replace "AD_DEPT", $prop.department
$s3 = $s2 -replace "AD_BRANCH", $prop.physicalDeliveryOfficeName
add-content C:\FILENAME2.txt $s3
}
}
}
的AD_DEPT和AD_BRANCH都在我的原始文件只是佔位符。
編輯
我通過JPBlanc的答案閱讀和做了一些更多的研究,並結束了與該工作示例。關鍵似乎是在指定要加載的屬性。謝謝!
$strFilter = "(&(objectClass=Person)(department=*))"
$objDomain = New-Object System.DirectoryServices.DirectoryEntry
$objOU = New-Object System.DirectoryServices.DirectoryEntry("LDAP://OU=XX;DC=XX;DC=local")
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.SearchRoot = $objOU
$objSearcher.PageSize = 1000
$objSearcher.Filter = $strFilter
$objSearcher.SearchScope = "OneLevel"
$colProplist = "cn","department","physicaldeliveryofficename"
foreach ($i in $colPropList){$objSearcher.PropertiesToLoad.Add($i)}
$colResults = $objSearcher.FindAll()
remove-item \\SERVER\FTPROOT\FOLDER\FILENAME.MODIFIED
(Get-Content \\SERVER\FTPROOT\FOLDER\FILENAME) | Foreach-Object `
{ `
foreach ($person in $colResults){
$prop = $person.properties
if ($prop.cn -like "*" + $_.substring(1, 3) + "*")
{
$s1 = $_ -replace $_.substring(0, 4), $prop.cn
$s2 = $s1 -replace "AD_DEPT", $prop.department
$s3 = $s2 -replace "AD_BRANCH", $prop.physicaldeliveryofficename
add-content \\SERVER\FTPROOT\FOLDER\FILENAME.MODIFIED $s3
break
}
}
}
爲什麼不使用AD或QuestAD命令行程序? AD: – Tom 2011-05-03 12:05:51
@Tom - 這實際上是我第一次使用Powershell,所以我不熟悉Quest AD。我一定會檢查出來的。 – 2011-05-03 12:11:28
這是值得的。這些cmdlet非常簡單。例如: AD:GET-ADUser便有-Filter * -Properties CN,部門,physicaldeliveryofficename 任務:獲取qaduser -includedproperties CN,部門,physicaldeliveryofficename – Tom 2011-05-03 12:23:23