PowerShell AD脚本无法正常工作

时间:2013-05-06 20:12:59

标签: powershell active-directory

出于某种原因,除homeDirectory外,一切正常。为此,所有内容都是空白的。实际上AD具有这些字段的值,但此脚本不显示该属性的任何内容。有什么想法吗?

  $objSearch = New-Object System.DirectoryServices.DirectorySearcher 
  $objSearch.PageSize = 15000 
  $objSearch.Filter = $ObjFilter 
  $objSearch.SearchRoot = "LDAP://$dn" 
  $AllObj = $objSearch.FindAll() 
foreach ($Obj in $AllObj) 
      { $objItemS = $Obj.Properties 
             $Ssamaccountname = $objItemS.samaccountname 
             $SsamaccountnameGN = $objItemS.givenname 
             $SsamaccountnameSN = $objItemS.sn 
             $SsamaccountnameEN = $objItemS.mail
             $SsamaccountnameLS = $objItemS.homeDirectory
             "$Ssamaccountname`t$SsamaccountnameGN`t$SsamaccountnameSN`t$SsamaccountnameEN`t$SsamaccountnameLS" | Out-File $UserInfoFile -encoding ASCII -append 
      } # End of foreach
   } # End of ForEach-Object 

2 个答案:

答案 0 :(得分:0)

您可以在FindAll

之前重试添加此行吗?
$objSearch.PropertiesToLoad.Add("homeDirectory");

一般来说,最好将搜索的每个属性添加到搜索中。

答案 1 :(得分:0)

如果您不喜欢使用.NET,我非常喜欢Quest Active Directory cmdlets

您的命令将是:

get-qadUser <UserName or another unique attribute> | Format-List <Attributes> | Out-File MyTextFile.txt

要获取可能属性的列表,您可以:

get-qaduser UserName -includeAllAttribute | fl * | Out-File C:\AllAttributes.txt