Help with script to query servers for running services

Welcome Forums General PowerShell Q&A Help with script to query servers for running services

Viewing 5 reply threads
  • Author
    Posts
    • #239033
      Participant
      Topics: 2
      Replies: 5
      Points: 17
      Rank: Member

      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.

      [/crayon]
    • #239060
      Participant
      Topics: 6
      Replies: 20
      Points: 68
      Helping Hand
      Rank: Member

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

       

      • This reply was modified 2 months, 3 weeks ago by Gnart.
    • #239066
      Participant
      Topics: 2
      Replies: 5
      Points: 17
      Rank: Member

      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.

      • This reply was modified 2 months, 3 weeks ago by kg213749.
      • This reply was modified 2 months, 3 weeks ago by kg213749.
    • #239081
      Participant
      Topics: 2
      Replies: 5
      Points: 17
      Rank: Member

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

      [/crayon]
    • #239090
      Participant
      Topics: 2
      Replies: 5
      Points: 17
      Rank: Member

      Fixed the formatting …

      [/crayon]
    • #239267
      Participant
      Topics: 7
      Replies: 122
      Points: 592
      Helping Hand
      Rank: Major Contributor

      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

       

Viewing 5 reply threads
  • You must be logged in to reply to this topic.