Link to home
Start Free TrialLog in
Avatar of malcolm29
malcolm29Flag for United States of America

asked on

PowerShell, finding data in an object returned from a cmdlet

I am new to PowerShell and am trying to understand how to search the results that get returned from a cmdlet.  

I realize this example could be accomplished another way, but I'm just playing around, so please forgive me if the actual use case is a little vague.

In this case, I'm trying to determine if the current computer is a server or not.  I assign a variable ($objListOfNonServers) to receive the collection of objects that get returned from the cmdlet (Get-ADComputer) which are not running Server OSes and which have the same machine name as the current machine.  Essentially, this should contain one object if the current machine is not a server in AD, or no objects if it is running a server OS.  But then I don't know how to test whether that collection contains any objects or not.  I would have thought the $objListOfServers.COUNT method would equal 0 if there were no matches, but I'm doing something wrong.  Can you help me figure out how to determine if the current machine is running a server operating system or not?

Here is my code

# Get this computer name
$sComputerName = $env:computername
#get list of computers that are not servers and have the name of this machine
$objListOfNonServers = Get-ADComputer -Filter { OperatingSystem -NotLike '*Windows Server*' -and Name -Like $sComputerName } | Select Name
if ($objListOfNonServers.Count -eq 0)
    { Write-Output 'This machine is a desktop' }
else
    { Write-Output 'This machine is NOT a desktop' }
ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of malcolm29

ASKER

Concise, correct, and examples given