2011-06-20 6 views
1

イムを使用して取得し、私はは、ネストされたOUのは、QADのコマンドレットを使用せずに、ネストされたOU内のサーバー用のOU情報を取得するためのPowerShellスクリプトを記述しようとしているのPowershell

$computerName = "DC1" 
$found = $FALSE 
$domain = [ADSI]("LDAP://dc=contoso,dc=com") 

$ous = ($domain.psbase.children | 
     Where-Object {$_.psBase.schemaClassName -eq "OrganizationalUnit"} | 
     Select-Object -expand Name)   

foreach ($child in $ous){ 
    $ou = [ADSI]("LDAP://ou=$child,dc=contoso,dc=com") 
    $computers = ($ou.psbase.children | 
        Where-Object {$_.psBase.schemaClassName -eq "Computer"} | 
        Select-Object -expand Name) 

    foreach ($client in $computers){ 
     if ($client -eq $computerName) { 
      Write-Host "Found $computerName in" $ou.psBase.name 
      $found = $TRUE 
     } 
    } 
} 

if (-not $found) {Write-Host "$computerName not found."} 
以下のようなコードを書くために1人のスタックメンバーによって助けられました

入れ子構造のOUでコンピュータの存在を確認するために、同じ変更を行うための助けが必要でした。

おかげで、 Vinith

答えて

2

あなたはadsisearcherアクセラレータを使用することができます。

$searcher = [adsisearcher]'(&(ObjectCategory=computer)(Name=DC1))' 
$searcher.FindOne() 
+0

こんにちは@shay税...それは2行までしてやらコードの万行あー、よかった:) ...私は$ compnameを宣言し、$ searcher = [adsisearcher] '(&(objectcategory = computer)(Name = $ compname))で使用すると、$ searcherを実行します。 findone()はnullの出力を返します – PowerShell

+0

愚かな間違いは一重引用符を削除するのを忘れました:) thanks @shaylevy – PowerShell

+0

こんにちは@シェイレビあなたはこの質問で私を助けてくれますか?http://stackoverflow.com/questions/6773322/unable-to-query-adsisearcher-for-trusted-domains-powershell – PowerShell

2
# Reference -- http://powergui.org/thread.jspa?threadID=17534 
# List and count user objects per OU 

Add-PSSnapin Quest.ActiveRoles.ADManagement -ErrorAction SilentlyContinue 
Import-Module ActiveDirectory -ErrorAction SilentlyContinue 
cd\ 
cls 

$File = "C:\Scripts\CountComputersInOu.csv" 
#To specify parent OU "your.domain.com/computer" 
$StartOU = "your.domain.com/" 
$strFilter = ‘(objectClass=Computer)’ 

foreach ($targetou in Get-QADObject -SearchRoot $StartOU -Type organizationalUnit) 
{ 

$Parent = [ADSI]"LDAP://$targetou" 
#"These are the users in $($Parent.PSBase.Parent.Name): " | Out-File $File -append 

$objSearcher = New-Object System.DirectoryServices.DirectorySearcher 
$objSearcher.SearchRoot = 'LDAP://'+$targetou+'' 
$objSearcher.PageSize = 1000 
$objSearcher.Filter = $strFilter 
$objSearcher.SearchScope = "onelevel" 

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

$colResults = $objSearcher.FindAll() 


"There are $($colResults.count) active computers in $($Parent.PSBase.Parent.Name)\$($targetou.name) 
" | Out-File $File -append 
} 
関連する問題