1

Active Directory OU にバインドし、コンピューターを一覧表示する次の powershell スクリプトがあります。余分な 0 を出力することを除けば、問題なく動作しているようです。理由はわかりません。誰でも助けることができますか?

 $strCategory = "computer"

 $objDomain = New-Object System.DirectoryServices.DirectoryEntry("LDAP://          OU=Computers,OU=datacenter,DC=ourdomain,DC=local")

 $objSearcher = New-Object System.DirectoryServices.DirectorySearcher($objDomain)
 $objSearcher.Filter = ("(objectCategory=$strCategory)")

  $colProplist = "name"
  foreach ($i in $colPropList){$objSearcher.PropertiesToLoad.Add($i)}

 $colResults = $objSearcher.FindAll()

 foreach ($objResult in $colResults)
 {
 $objComputer = $objResult.Properties; 
 $objComputer.name
 }

出力: 0 サーバー1 サーバー2 サーバー3

4

2 に答える 2

5

You need to capture (or ignore) the output of the PropertiesToLoad.Add method, otherwise you'll get a value for each property in $colPropList.

foreach ($i in $colPropList){[void]$objSearcher.PropertiesToLoad.Add($i)}

You can simplify and shorten your script and load a bunch of properties in one call without using a foreach loop. Another benefit of the AddRange method is that it doesn't output the length of requested properties, so there's no need to capture anything.

$strCategory = "computer"
$colProplist = "name","distinguishedname"

$searcher = [adsisearcher]"(objectCategory=$strCategory)"
$searcher.PropertiesToLoad.AddRange($colProplist)
$searcher.FindAll() | Foreach-Object {$_.Properties}
于 2012-10-31T18:14:19.317 に答える
1

PropertiesToLoad.Add を呼び出すときに、foreach ループが結果を出力していると思われます。

次のように、out-nullにパイプしてみてください。

foreach ($i in $colPropList){
    $objSearcher.PropertiesToLoad.Add($i) | out-null
}
于 2012-10-31T18:17:09.583 に答える