Check if Computer object exists in AD ?

I have a text file containing 100 server names. I would like to push these into a script and test wheather the computer object exists or if it has been decommissioned (deleted). From the results i can update a tracking sheet that has been left to the side for sometime.

Seems easy enough but i am having problems all day! Below is what i have so far..

Function check-server-object {

$serverlist = get-content "e:\serverlist.txt"
try
{
write-host "#################################"
write-host "Attempting to find computer"
write-host "#################################"

ForEach ($server in $serverlist){
#$tempVar = Get-ADComputer $server
if (Get-ADObject $server -eq $null) { Write-Host "not there"} else {Write-Host "$Server Found In AD!"}
}
}
catch {write-host "$server does not exist" -ForegroundColor Red}
}

check-server-object

I have the try & catch because i was having issues with errors etc.. little bit of a bodge. :/ any anyway so far it only gives me the result of the first server name in the text file... only another 99 to go!! why is it not giving me all the servers ?

Please can you help / explain on the logic and provide any solution to my problem.

 

Thanks,

Phil

  • Can you post five lines of your serverlist.txt file?  And since you're only testing for computers, wouldn't you use Get-ADComputer instead of Get-ADObject?

  • Thank you for the reply...Yes you can use get-adcomputer aswell.
    Format of text file is as follows:

    server1
    server2
    server3
    server4
    ....
    server100

  • A solution was found:

    $serverlist = get-content "serverlist.txt"
    foreach ($server in $serverlist) {
        try{
            Get-ADComputer $server -ErrorAction Stop
            Write-Host "#########################" 
            Write-Host "Computer object exists"
            Write-Host "#########################"
        }
        catch{
            Write-Host "#########################" 
            Write-Host "$server Computer object NOT FOUND" -ForegroundColor red
             }
    }

  • Can I suggest a more PowerShell friendly style of output?

     

    ForEach ($server in (get-content "serverlist.txt")) 
    {   Try {
            Get-ADComputer $server -ErrorAction Stop
            $Result = $true
        }
        Catch {
            $Result = $False
        }
        [PSCustomObject]@{
            Name = $server
            Found = $Result
        }
    }

    What's nice about this output is it's in the form of a PowerShell object that can then be used anyway you want.  You could pipe it into Export-CSV for easy storage.  You could pipe the results of your script into Where-Object and create a new text file of just the good ones.  

    I think it's one of the key tenant's of PowerShell that if you stick with object output instead of text your code becomes reusable!  Now, maybe that's not really necessary in this case, I get it, but it's just something that you should always keep in mind :)

     

    Good luck!

  •  

    Hello guys, 

    I'd like to request some help.

    I have already script to ping machines which are included in .txt file :

     

    $currentDirectory = [System.IO.Path]::GetDirectoryName($myInvocation.MyCommand.Definition)

    $inputFile = "$currentDirectory\Computers.txt"

    $OutputFile = "$currentDirectory\Report.csv"

    $servers = Get-Content $inputFile

    $collection = $()

    foreach ($server in $servers)

    {

        $status = @{ "ServerName" = $server; "TimeStamp" = (Get-Date -f s) }

        if (Test-Connection $server -Count 1 -ea 0 -Quiet)

        { 

            $status["Results"] = "Up"

        } 

        else 

        { 

            $status["Results"] = "Down" 

        }

        New-Object -TypeName PSObject -Property $status -OutVariable serverStatus

        $collection += $serverStatus

     

    }

    $collection | export-csv $OutputFile -NoTypeInformation 

     

    And  it returns following output : 

    TimeStamp   Results    ServerName
    2014-08-12T12:11:32    Up   

    server1

     

    I'd like to have there also info if machine exists in AD and receive output like :

    TimeStamp    Results    ServerName    FoundInAD
    2014-08-12T12:11:32    Up    server1   YES
    2014-08-12T12:11:32    Down    server2   NO

     

     

    Thanks in advance