# Help with script to query servers for running services

**URL:** <https://forums.powershell.org/t/help-with-script-to-query-servers-for-running-services/14605>\
**Category:** PowerShell Help\
**Created:** [June 29, 2020, 10:31am UTC](https://forums.powershell.org/t/help-with-script-to-query-servers-for-running-services/14605 "2020-06-29T10:31:27Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![kg2137](https://avatars.discourse-cdn.com/v4/letter/k/35a633/32.png) [@kg2137](https://forums.powershell.org/u/kg2137)\
**Post date:** [June 29, 2020, 10:31am UTC](https://forums.powershell.org/t/help-with-script-to-query-servers-for-running-services/14605/1 "2020-06-29T10:31:27Z")

</div>

I am a novice when it comes to PS. Trying to figure out the best way to get this script to report that a server is unreachable instead of a exception error. It also still lists the service as running even though I am unable to reach the server from my computer.

```auto
$ComputerName = Get-Content "C:\temp\comp_list.txt"
foreach ($computer in $ComputerName) {
 
    $servicesToCheck = "BITS"
    
    $services = Get-Service -ComputerName $computer | Select Name, Status
    
    $results = foreach ($service in $servicesToCheck) {
       
        $checkService = $services | Where{ $_.Name -eq $service}
        
        if ($checkService) {
         
            $checkService | Select Name, Status, @{Name="ComputerName";Expression={$computer}}
        }
        else {
            
            $props = @{ComputerName=$computer;
                       Name=$service;
                       Status="Not Installed"}
            New-Object -TypeName PSObject -Property $props
        }
    }
    $results 
}

```

---

<div class="post-metadata">

**Author:** ![gnart](https://avatars.discourse-cdn.com/v4/letter/g/5e9695/32.png) [@gnart](https://forums.powershell.org/u/gnart)\
**Post date:** [June 29, 2020, 11:16am UTC](https://forums.powershell.org/t/help-with-script-to-query-servers-for-running-services/14605/2 "2020-06-29T11:16:20Z")

</div>

Destination host unreachable is the result of the destination host not up or not responding to echo request (ping). Usually, (Firewalling) blocking echo reply (in response to echo request) is the default for servers. PowerShell Get-Service does not ping the host. It uses wsman service (HTTP/HTTPS) on port (5985/5986), so it was able to Get-Service running on the destination host.

To check for destination host unreachable either (a) ping the host or (b) use PowerShell (Test-Connection hostname) or (Test-Netconnection IP-address). I thought the purpose of your script was to get services if the host was up.

Here’s a code example:

`$computer = 'UnreachableHost'`  
`if (Test-Connection -computername $computer -Quiet -Count 1) {`  
`# host reachable do stuff`  
`Write-host "Reachable"`  
`} else {`  
`# failed, log or whatever`  
`Write-host "Unreachable"`  
`}`

&nbsp;

---

<div class="post-metadata">

**Author:** ![kg2137](https://avatars.discourse-cdn.com/v4/letter/k/35a633/32.png) [@kg2137](https://forums.powershell.org/u/kg2137)\
**Post date:** [June 29, 2020, 11:35am UTC](https://forums.powershell.org/t/help-with-script-to-query-servers-for-running-services/14605/3 "2020-06-29T11:35:02Z")

</div>

Yes I want to add a check in the script to make sure the host is up before it tries to check for the services. Right now if I add a host to the list that is down (in this case an dummy host) it just spits out an exception error then still states that BITS is running on the host it can’t connect to.

I sort of have what you have suggested added in there and messing with it a bit but I can’t figure out how to get it to complete the service check after it tests the connection successfully.

---

<div class="post-metadata">

**Author:** ![kg2137](https://avatars.discourse-cdn.com/v4/letter/k/35a633/32.png) [@kg2137](https://forums.powershell.org/u/kg2137)\
**Post date:** [June 29, 2020, 11:48am UTC](https://forums.powershell.org/t/help-with-script-to-query-servers-for-running-services/14605/4 "2020-06-29T11:48:54Z")

</div>

Thanks… I think I might have it, though the formatting is a bit wonky

```auto
$ComputerName = Get-Content "C:\temp\comp_list.txt"
foreach ($computer in $ComputerName) {

    if (Test-Connection -computername $computer -Quiet -Count 1) {
 
    $servicesToCheck = "BITS"
    
    $services = Get-Service -ComputerName $computer | Select Name, Status
    
    $results = foreach ($service in $servicesToCheck) {
       
        $checkService = $services | Where{ $_.Name -eq $service}
        
        if ($checkService) {
         
            $checkService | Select Name, Status, @{Name="ComputerName";Expression={$computer}}
        }
        else {
            
            $props = @{ComputerName=$computer;
                       Name=$service;
                       Status="Not Installed"}
            New-Object -TypeName PSObject -Property $props
        }
    }
    $results 
    }
    else {
        Write-Host "$Computer is not responding."
        }
}

```

---

<div class="post-metadata">

**Author:** ![kg2137](https://avatars.discourse-cdn.com/v4/letter/k/35a633/32.png) [@kg2137](https://forums.powershell.org/u/kg2137)\
**Post date:** [June 29, 2020, 11:58am UTC](https://forums.powershell.org/t/help-with-script-to-query-servers-for-running-services/14605/5 "2020-06-29T11:58:17Z")

</div>

Fixed the formatting …

```auto
$ComputerName = Get-Content "C:\temp\comp_list.txt"

foreach ($computer in $ComputerName) {

    if (Test-Connection -computername $computer -Quiet -Count 1) {
 
    $servicesToCheck = "BITS"
    
    $services = Get-Service -ComputerName $computer | Select Name, Status
    
    $results = foreach ($service in $servicesToCheck) {
       
        $checkService = $services | Where{ $_.Name -eq $service}
        
        if ($checkService) {
         
            $checkService | Select Name, Status, @{Name="ComputerName";Expression={$computer}}
        }
        else {
            
            $props = @{ComputerName=$computer;
                       Name=$service;
                       Status="Not Installed"}
            New-Object -TypeName PSObject -Property $props
        }
    }
    $results 
    }
    else {
        $offline = @{ComputerName=$computer;
                       Name="N/A";
                       Status="Offline"}
            New-Object -TypeName PSObject -Property $offline
        }
}

```

---

<div class="post-metadata">

**Author:** ![tonyd](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/tonyd/32/1041_2.png) [@tonyd](https://forums.powershell.org/u/tonyd)\
**Post date:** [June 30, 2020, 7:16am UTC](https://forums.powershell.org/t/help-with-script-to-query-servers-for-running-services/14605/6 "2020-06-30T07:16:57Z")

</div>

You can simplify your code somewhat by just querying for the service you want

`Get-Service -ComputerName $computer -Name ‘BITS’`, or in your case `Get-Service -ComputerName $computer -Name $serviceToCheck`

&nbsp;

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:29pm UTC](https://forums.powershell.org/t/help-with-script-to-query-servers-for-running-services/14605/7 "2024-05-16T20:29:19Z")

</div>


