Get-Service From All Computers in AD OU

Welcome Forums General PowerShell Q&A Get-Service From All Computers in AD OU

This topic contains 3 replies, has 3 voices, and was last updated by

4 years, 5 months ago.

  • Author
  • #20603

    Points: 0
    Rank: Member

    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.

  • #20604

    Points: 0
    Rank: Member

    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

  • #20605

    Points: 0
    Rank: Member

    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?

  • #20629

    Points: 21
    Rank: Member

    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.

The topic ‘Get-Service From All Computers in AD OU’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort