Help with script to query servers for running services

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.

$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 
}

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"
}

 

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.

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

$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."
        }
}

Fixed the formatting …

$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
        }
}

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