Find server in AD exists using powershell

Can some one help me out to find an OU in which the computer account exists using powershell? 

Exemple:

$computerName = "HOST-ES01"
$found = $FALSE
$domain = [ADSI]("LDAP://dc=dicasquefunfa,dc=dqf)

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

foreach ($child in $ous){
    $ou = [ADSI]("LDAP://ou=$child,dicasquefunfa,dc=dq")
    $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."}

I want to go with a list of machines and see whether there is in the field
example:
Input_Hosts.txt
Output_Host.txt

August 27th, 2015 11:21am

This isn't a "script on demand" forum, but here is an example:
Free Windows Admin Tool Kit Click here and download it now
August 27th, 2015 11:59am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics