2011-04-29 53 views
1

非常直接的問題:我不知道爲什麼「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 
    } 
    } 
} 
+0

爲什麼不使用AD或QuestAD命令行程序? AD: – Tom 2011-05-03 12:05:51

+0

@Tom - 這實際上是我第一次使用Powershell,所以我不熟悉Quest AD。我一定會檢查出來的。 – 2011-05-03 12:11:28

+0

這是值得的。這些cmdlet非常簡單。例如: AD:GET-ADUser便有-Filter * -Properties CN,部門,physicaldeliveryofficename 任務:獲取qaduser -includedproperties CN,部門,physicaldeliveryofficename – Tom 2011-05-03 12:23:23

回答

3

很多事情必須在那裏說。

1.屬性

對於一個屬性的存在被查詢時,它首先必須存在於您目錄的模式。 SCHEMA定義目錄條目可以包含的類型和屬性。在該模式中,該屬性必須被定義爲「可以」或「必須」存在於一個類型中。例如objectClass屬性必須存在於所有類型中。

如果我在我的Windows 2K8 R2的模式一看,我可以看到你的屬性:

enter image description here

現在,如果我使用Apache目錄工作室,我可以看到physicalDeliveryOfficeName存在12種( 11一個正常的服務器上忘記SlxAuteur)

enter image description here

結論第一部分的:你可能(如果你^ h足夠的權利)在userinetOrgPerson上設置此屬性。

2.您搜索的屬性

你會發現這裏的目錄搜索的使用的樣本下的方式。我添加代碼以修改指定用戶的physicalDeliveryOfficeName屬性。

$dn = New-Object System.DirectoryServices.DirectoryEntry ("LDAP://192.168.183.138:389/dc=societe,dc=fr","[email protected]","blabla") 

# Look for users 
$Rech = new-object System.DirectoryServices.DirectorySearcher($dn) 
$rc = $Rech.filter = "((objectCategory=person))" 
$rc = $Rech.SearchScope = "subtree" 
$rc = $Rech.PropertiesToLoad.Add("distinguishedName"); 
$rc = $Rech.PropertiesToLoad.Add("sAMAccountName"); 
$rc = $Rech.PropertiesToLoad.Add("ipphone"); 
$rc = $Rech.PropertiesToLoad.Add("telephoneNumber"); 
$rc = $Rech.PropertiesToLoad.Add("memberOf"); 
$rc = $Rech.PropertiesToLoad.Add("distinguishedname"); 
$rc = $Rech.PropertiesToLoad.Add("physicalDeliveryOfficeName"); # Your attribute 


$liste = $Rech.findall() 
foreach ($usr in $liste) 
{ 
    # Write-Host $usr.Properties["samaccountname"] 
    if ($usr.Properties["samaccountname"] -eq "massin") 
    { 
    Write-Host $usr.Properties["distinguishedname"] 
    $dnUser = New-Object System.DirectoryServices.DirectoryEntry ("LDAP://192.168.183.138:389/$($usr.Properties["distinguishedname"])","[email protected]","blabla") 
    $dnUser.put("physicalDeliveryOfficeName", "1 rue de la source") 
    $res = $dnUser.setinfo() 
    $res 
    } 
} 

下面是結果:

enter image description here

備註:目錄搜索

  1. 哪裏開始搜索
  2. 你想要的屬性節點(它不是必須的,但這是一個最佳實踐),如果你不給他們,你不能確定他們被檢索。
  3. 深度(鹼,ONELEVEL,子樹)
  4. 濾波器

如果屬性不查詢或爲空,因此不會出現在結果

+0

它看起來像我錯過了我所需要的性能的顯式加載。查看我編輯的工作示例。 – 2011-05-02 20:40:23