Use array to check services’ status

Welcome Forums General PowerShell Q&A Use array to check services’ status

Viewing 3 reply threads
  • Author
    Posts
    • #246048
      Participant
      Topics: 1
      Replies: 1
      Points: 13
      Rank: Member

      Hi guys,

      Learning powershell and enjoying every minute of it.

      At work, we have a morning checklist, and one of the items is for checking the status of some services on a server.

      I can check the services status individually in a script 1 line at a time, but I want to use an array and pass it to the get-service command.  This will definitely advance my powershell scripting knowledge.

      What I have so far is 8 lines running the get-service command for each service and a pause command at the end so the console doesn’t close on me.

      get-service -computername servername | where-object {$_.name -eq ‘ServiceName’}

      So far in my research, I know that a variable is needed:  $servicelist = @(‘service1′,’service2′,’service3’)

      Also, probably the use of foreach.  Just need to know how to put it all together.

      Thank you.

      “Give a man a fish and feed him for a day; teach a man to fish and you feed him for a lifetime.”

       

    • #246057
      Participant
      Topics: 12
      Replies: 539
      Points: 1,304
      Helping Hand
      Rank: Community Hero

      The Get-Service cmdlet takes a -Name parameter, so the filtering can happen at the cmdlet itself and not through the pipeline | where-object statement. It also takes multiple values for that parameter, so your entire command can go like:

      • This reply was modified 2 months, 3 weeks ago by Sam Boutros.
    • #246069
      Participant
      Topics: 1
      Replies: 1
      Points: 13
      Rank: Member

      Sweeeeet!  Thank you!  And here I was complicating things… 😀

    • #246270
      Participant
      Topics: 12
      Replies: 38
      Points: 284
      Helping Hand
      Rank: Contributor

      I’m going beyond the stated question, but since you mentioned learning Powershell. I thought you might be interested in exporting the results of the query to a file.

      It will allow you to log the information over time and allow you to get more familiar with other aspects of Powershell. If so you might want to look at cmdlets such as Out-File or for a more data specific format Export-Csv. And for naming the file(s) consistently you can look at the Get-Date cmdlet and how to format it.

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