Problem:
When looking for a computer object in Active Directory, depending on how you call “Get-ADComputer” will determine if an error is thrown or not.
Solution:
To avoid the terminating error, use the “Filter” parameter to search for the computer object.
$Computer = Get-ADComputer -Filter {Name -eq $Name}
Explanation:
I was creating a test case to make one of my scripts idempotent but when I used the “Identity” parameter, my script was failing on this cmdlet. Even if I used “Ignore” or “SilentlyContinue” for the ErrorAction, I would still receive a terminating error. By filtering for the computer object, if nothing is found then the “Computer” variable is set to “null”. Then when running a test, like:
if($Computer)
{
...
}
we can successfully apply conditions in the code.