Powershell - verify object exists in AD
Solution 1
Create an array - @()
. If the array has 1 or more objects in it - which is $true
- then you know the computer exists. If the array has 0 objects in it - which is $false
- then you know the computer doesn't exist. I know some people don't like the ErrorAction
to be set to SilentlyContinue
but you're "Outputting an Error" if an error does occur.
$serverlist = get-content ServerList.txt
foreach ($server in $serverlist) {
if (@(Get-ADComputer $server -ErrorAction SilentlyContinue).Count) {
Write-Host "#########################"
Write-Host "Computer object exists"
Write-Host "#########################"
}
else {
Write-Host "#########################"
Write-Host "Computer object NOT FOUND"
Write-Host "#########################"
}
}
Another thing you could try are try catch
blocks. Sorta like this:
$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 "Computer object NOT FOUND"
Write-Host "#########################"
}
}
Solution 2
Line 3, change $serverlist to $server
With regards to handling a not found result. I'd try flipping the logic :
$serverlist = get-content ServerList.txt
foreach ($server in $serverlist) {
$tempVar = Get-ADComputer $server
if ($tempVar -like "Get-ADComputer : Cannot find an object with identity" ) {
Write-Host "#########################"
Write-Host "Computer object NOT FOUND"
Write-Host "#########################"
}
else{
Write-Host "#########################"
Write-Host "Computer object exists"
Write-Host "#########################"
}
}
Richard
Updated on March 10, 2020Comments
-
Richard about 4 years
i have a text file containing arround 100 servers, how can i push these into a script and test if they exist within AD? I have a simple script below:
$serverlist = get-content ServerList.txt foreach ($server in $serverlist) { if (Get-ADComputer $serverlist ) { Write-Host "#########################" Write-Host "Computer object exists" Write-Host "#########################" } else { Write-Host "#########################" Write-Host "Computer object NOT FOUND" Write-Host "#########################" } }
the above does not work returning a error:
Get-ADComputer : Cannot convert 'System.Object[]' to the type 'Microsoft.ActiveDirectory.Management.ADComputer' required by parameter 'Identity'. Specified method is not supported.
Can someone please explain does the get-adcomputer only allow a single object? Also if i remove the txt file and add a server shown below:
if (Get-ADComputer "server name" )
The above provides only results if the server exists within AD, if the server does not the error is shown below:
Get-ADComputer : Cannot find an object with identity: 'iuiub' under: 'DC=####,DC=#####,DC=#####'
Thank you for any insight / help!
Phil
-
Loïc MICHEL about 10 yearsyou can test result like that :
if ( (Get-ADComputer "server1") -eq $null){"not found"} else {"alive"}
-
Richard about 10 yearsthank you for the comment kayasax, though i have tried this and this does not work for a computer object that does not exist, it reports a error and writes out "alive" even if it doesnt exist - thank you for the reply :)
-
E.V.I.L. about 10 yearsYou need to use the
ErrorAction
parameter forGet-ADComputer
. You need to let it know what you want it to do if there is an error.
-
-
Richard about 10 yearsThank you for the advice, the extra line allows me to pipe out computer objects that exist! just still having problems with objects that do not exist within the domain... thank you for the help.
-
Richard about 10 yearswow... this had me going for ages and i did end up trying the "try & catch" blocks but my syntax was not correct. Thank you, went with the second option.. works a treat. cheers!
-
Patrick Burwell over 2 yearsI still get 'Get-ADComputer : Cannot find an object with identity:' errors. How do you stop this?