Get all AD computers excluding servers

Copper Contributor

Hi!

I am trying to create a script that will retrieve all computers but exclude any servers found. I also discovered that using the most basic command will return about two dozen servers and then crash with the error message shown below:

 

 

get-adcomputer -Filter * -Properties * -Server "MyServer.com"

 

 

Output:

get-adcomputer : Directory object not found
At line:1 char:1
+ get-adcomputer -Filter * -Properties * -Server "MyServer.com" | ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (:) [Get-ADComputer], ADIdentityNotFoundException
+ FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException,Microsoft.ActiveDire
ctory.Management.Commands.GetADComputer

 

Here are some command lines I have tried with no success:

 

 

get-adcomputer -Filter * -Properties * -Server "MyServer.com" | Select-Object cn,operatingsystem,canonicalname

get-adcomputer -Filter * -Properties * -Server "MyServer.com" | where-object {($_.OperatingSystem -notlike "*Server*")} | select-object cn, operatingsystem

get-adcomputer -Filter * -Properties * -Server "MyServer.com" | where-object ($_.OperatingSystem -notlike "*Server*") | select-object cn, operatingsystem

get-adcomputer -Filter 'OperatingSystem -ne "*Server*"' -Properties cn,operatingsystem | select-object cn, operatingsystem

get-adcomputer -Filter ('OperatingSystem -notlike "*Server*"') -Properties cn,operatingsystem | select-object cn, operatingsystem

 

 

I'm not sure what the error message means and I can't seem to filter out servers. Shouldn't Get-ADComputer be able to get all computers without any errors?

 

Thank you!

Rob

 

 

4 Replies
Hi, my best guess is that it is finding an object in AD that is corrupted and therfore throws an exception. you may want to try the older method of adsisearcher as per:
https://social.technet.microsoft.com/Forums/en-US/ec2732b9-d2dd-4592-82bc-71fae700a81b/powershell-li...

Have you done AD health checks to make sure you don't have orphan or corrupt objects?
Hi J Mymryk,
I will look into your suggestions and also check AD health.
Thank you!
I looked into this further by testing against all domains. Get-ADComputer works on all domains except for one. How do I determine the root cause so I can escalate this issue to another team?

You are definitely on the right track. :)
Well, this apparently known in my organization. The workaround is to include specific properties in the line of script.
So in our environment, one of our domains will fail when you use -Properties *
If I use, -Properties CN,OperatingSystem, then the script no longer crashes on the domain causing issues.