Powershell LDAP - physicalDeliveryOfficeName未显示

时间:2011-04-29 20:08:43

标签: powershell active-directory ldap

非常直截了当的问题:我不确定为什么“physicalDeliveryOfficeName”属性没有显示在我的输出中。我读过它是一个非标准的属性,但我找不到添加它的方法。除了缺少的“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
    }
  }
}

1 个答案:

答案 0 :(得分:3)

那里必须说很多事情。

<强> 1。存在属性

对于要查询的属性,它首先必须出现在您目录的SCHEMA中。 SCHEMA定义了目录条目可以包含的类型和属性。在模式中,该属性必须定义为类型中的“可能是”或“必须”。例如,objectClass属性必须存在于所有类型中。

如果我查看Windows 2K8 R2的架构,我可以看到您的属性:

enter image description here

现在,如果我使用Apache Directory Studio,我可以看到physicalDeliveryOfficeName存在12种类型(普通服务器上的11种忘记了SlxAuteur)

enter image description here

第一部分的结论:您可能(如果您有足够的权利)在userinetOrgPerson上设置此属性。

<强> 2。搜索属性的方式

您可以在目录搜索器的使用示例下找到。我添加代码来修改指定用户的physicalDeliveryOfficeName属性。

$dn = New-Object System.DirectoryServices.DirectoryEntry ("LDAP://192.168.183.138:389/dc=societe,dc=fr","administrateur@societe.fr","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"])","administrateur@societe.fr","blabla")
    $dnUser.put("physicalDeliveryOfficeName", "1 rue de la source")
    $res = $dnUser.setinfo()
    $res
  }
}

结果如下:

enter image description here

备注:目录搜索

  1. 开始搜索的节点
  2. 你想要的属性(这不是强制性的,但这是一种最佳做法)如果你没有给他们你不能确定它们是否被检索。
  3. 深度(基数,单级,子树)
  4. 过滤器
  5. 如果未查询属性或为空,则结果中不会显示该属性