Find OU in which a server exists using powershell
Can some one help me out to find an OU in which the computer account exists using powershell? without using the quest AD commandlets i mean someting using [ADSI开发者_运维问答]
Thanks Vinith
You could do something like this:
$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."}
You'd have to modify it if your clients are in nested OUs.
精彩评论