# Loop through servers and check services status

**URL:** <https://forums.powershell.org/t/loop-through-servers-and-check-services-status/21263>\
**Category:** PowerShell Help\
**Created:** [January 2, 2023, 8:04am UTC](https://forums.powershell.org/t/loop-through-servers-and-check-services-status/21263 "2023-01-02T08:04:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![aristosv](https://avatars.discourse-cdn.com/v4/letter/a/76d3ee/32.png) [@aristosv](https://forums.powershell.org/u/aristosv)\
**Post date:** [January 2, 2023, 8:04am UTC](https://forums.powershell.org/t/loop-through-servers-and-check-services-status/21263/1 "2023-01-02T08:04:03Z")

</div>

I want to check the status of multiple services on multiple remote servers. And if they are not running, start them. So I thought I would first define which services I want to check for each server.

```auto
if ($servername = 'server1') {$services = 'service1','service2','service3','service4'}
if ($servername = 'server2') {$services = 'service5','service6','service7','service8'}
if ($servername = 'server3') {$services = 'service9','service10','service11','service12'}

```

And then create a function to loop though each server, and check it’s services, depending on the server name.

```auto
function check_services {
    foreach ($service in 'server1') {
        if ((Get-Service -ComputerName server1 -Name $service).Status -ne 'Running') {
            Write-Host $service on server1 is not running. Starting it.
            Set-Service -ComputerName server1 -Name $service -Status Running
        }
        else {
            Write-Host $service is running on server1
        }
    }
}

```

I then realized I will have to create a function for each server I want to check.

How can I create a single function that will go through each server and check it’s corresponding services?

---

<div class="post-metadata">

**Author:** ![adam-bukowski](https://avatars.discourse-cdn.com/v4/letter/a/9d8465/32.png) [@adam-bukowski](https://forums.powershell.org/u/adam-bukowski)\
**Post date:** [January 2, 2023, 9:40am UTC](https://forums.powershell.org/t/loop-through-servers-and-check-services-status/21263/2 "2023-01-02T09:40:17Z")

</div>

Please read about `invoke-command`. Should solved your problem with proper code 🙂

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [January 2, 2023, 9:42am UTC](https://forums.powershell.org/t/loop-through-servers-and-check-services-status/21263/3 "2023-01-02T09:42:07Z")

</div>

aristosv,  
Welcome back to the forum. 👋🏽 … long time no see.

> [@aristosv](#):
>
> So I thought I would first define which services I want to check for each server.

Why do you use `if` statements for that? … and BTW:

> [@aristosv](#):
>
> `if ($servername = 'server1')`

In PowerShell the equal sign (`=`) is the assignment operator. If you want to check for equality you should use `-eq`. 👆🏽

> [@aristosv](#):
>
> I then realized I will have to create a function for each server I want to check.

Why do you think you need a function? … and BTW: `Set-Service` is used to change the settings of a given service - not to start or restart it. Therefor you should use `Start-Service` or `Restart-Service`.

> [@aristosv](#):
>
> How can I create a single function that will go through each server and check it’s corresponding services?

I’d recommend to save the needed information in a structured way - for example a CSV file. Thhen you can iterate with a nested loop of the servers and for each server over the services.

In general: what you’re asking for is a very common and simple task what has been asked a lot of times and already answered a lot of time. You don’t have to re-invent the wheel again. Use your prefered internet search engine to search for examples you can adapt to your particular needs. You will find some even here on [PowerShell.org](http://PowerShell.org).

Here you have some examples:

> [@Unable to get the Output of Hashtable in Table format](https://forums.powershell.org/t/unable-to-get-the-output-of-hashtable-in-table-format/21194/2):
>
> Anudeep, Welcome to the forum. wave:t4 I’d use a [PSCustomObject] instead of a hashtable in such a case. It’s even easier to read and maintain. param( [string[]]$computername ) foreach ($computer in $computername) { $filebeat\_status\_before = Get-Service -Name filebeat -ComputerName $computer Get-Service -Name filebeat -ComputerName $computer | Restart-Service $filebeat\_status\_after = Get-Service -Name filebeat -ComputerName $computer [PSCustomObject]@{ ComputerN…

> [@Get-Service with if/else checks](https://forums.powershell.org/t/get-service-with-if-else-checks/20535/2):
>
> Landon, Welcome to the forum. wave:t4 Since I like re-usable output I’d use a slightly different approach and avoid Write-Host prosa with fancy colors … wink Besides this I’d recommend to avoid connecting to one computer more than once at a time. $ComputerList = Get-Content -Path 'C:\\<file location\>\Desktop\Machines.txt' $ServiceQueryList = Get-Content -Path 'C:\\<file location\>\ServiceList.txt' $Result = foreach ($ComputerName in $ComputerList) { if (Test-Connection -ComputerName $C…

---

<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:16pm UTC](https://forums.powershell.org/t/loop-through-servers-and-check-services-status/21263/4 "2024-05-16T20:16:13Z")

</div>


