Get-Service From All Computers in AD OU

Hey everyone!

I’m still a novice with Powershell but am completely hooked on the “power of the shell”! I’ve been playing around with some stuff on my network and ran across something I can’t seem to find an answer to. I know this is probably a really simple question to everyone here so bear with me.

My goal is to find all active directory machines in an OU that have the DHCP Server service running. I can run the command below and get a list returned from all the machines in the particular OU but I only get the DHCP Client service returned. I know for a fact that at least one of the servers in this OU has DHCP Server running. What am I missing?

Get-ADComputer -SearchBase ‘OU=servers,dc=MyDomain,dc=com’ -Filter '’ | Select -Exp Name | ForEach-Object {Get-Service -DisplayName dhcp}

Thanks for any and all help.

You’re not accessing the remote machine. Each call to Get-Service is running against the local machine as you haven’t specified the name of the remote computer.

Your code should be

Get-ADComputer -SearchBase ‘OU=servers,dc=MyDomain,dc=com’ -Filter '’ | Select -Exp Name | ForEach-Object {Get-Service -DisplayName dhcp -ComputerName $psitem }

You are looking for the DHCP server service so could make your Get-Service call

Get-ADComputer -SearchBase ‘OU=servers,dc=MyDomain,dc=com’ -Filter '’ | Select -Exp Name | ForEach-Object {Get-Service -Name DHCPServer -ComputerName $psitem }

if you wanted to cut down the return data

Thank you Richard!

I knew this was something simple. I’m just now beginning to understand the flow of the PS commands.

So if the $psitem basically tells the Get-Service cmdlet to access each remote machine, why was I getting a return of “DHCP Client” for each machine in the OU before?

So as Richard mentioned you were actually running it against the local machine easy time because you did not have the -ComputerName $psitem portion.

Basically the break down of what was happening is this:
(ol)You got a list of machines from Get-ADComputer based on your search criteria.
You reduced the selection of this to just the Name property from those objects.
For each one of those objects you ran “Get-Service -DisplayName dhcp”, which with those parameters would just run against the local machine and return the result. (/ol)

That is why you got a (i)result(/i) for each machine, you basically just used it as a counter for how many times to run Get-Service against your local machine. Assuming the machine you ran this from only has the DHCP Client service running, that should explain why you only got that as a result.

Hopefully that helped and did not make it more confusing, and if you have more questions about it we can all try to explain it differently I am sure.