8

I have 3 servers, running 3 services:

  • SERVER-A running serv1.exe service
  • SERVER-B running serv2.exe service
  • SERVER-C running serv3.exe service

Using PowerShell how can I query each service to check if they are running and output the information to a text file?

scytale
  • 12,346
  • 3
  • 32
  • 46
Ireshad
  • 81
  • 1
  • 1
  • 2

2 Answers2

14

If they all have the same name or display name you can do it in one command. If not you need to run 3 commands.

If all have the same name or display name:

Get-Service -ComputerName server-a, server-b, server-c -Name MyService | 
  Select Name, MachineName, Status

If they have different names or display names:

I would do this --

@{
  'server-a' = 'service-a'
  'server-b' = 'service-b'
  'server-c' = 'service-c'
}.GetEnumerator() | ForEach-Object {
  Get-Service -ComputerName $_.Name -Name $_.Value
} | Select Name, MachineName, Status

To output to a text file use ... | Set-Content ~\Documents\Service_Status.txt where ... is one of the above.

Note - your account will need to have privileges to query the remote machines.

Andy Arismendi
  • 50,577
  • 16
  • 107
  • 124
  • 1
    How do you specify an alternative credential if your account doesn't have permission? – Joseph Aug 30 '16 at 15:46
  • 1
    @Dan, you need to use [Invoke-Command](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/invoke-command?view=powershell-6) in this case. – David S. Apr 16 '18 at 07:20
  • 1
    If your service name has a special character in it like `$` then you will need to surround the entire name with single quotes like: `get-service -computername ServerName -Name 'Service$Name' – Mike Jul 23 '18 at 17:51
  • Does the get-service also supports wild card ? – Senior Systems Engineer Sep 02 '20 at 09:25
3

There are several ways to achieve this. I am using a hash of the values since you mentioned that the server to service mapping is always one to one.

$svrHash = @{"SERVER-01"="winmgmt";"SERVER-02"="Wecsvc"}
$svrHash.Keys 
  | ForEach-Object {Get-Process -ComputerName $_ -Name $svrHash[$_] -Ea SilentlyContinue} 
  | Select ProcessName 
  | Out-File C:\Scripts\test.txt

You need to use the service name and not the .exe name.

Noctis
  • 11,507
  • 3
  • 43
  • 82
ravikanth
  • 24,922
  • 4
  • 60
  • 60
  • Thanks it worked. But I was wrong in explaining my requirement.They are Tasks and NOT services. If i open Task Manager, I should be able to see 3 different tasks in 3 different servers. – Ireshad Jul 22 '13 at 14:07
  • So, they are processes and not services? Replace Get-Service with Get-Process and it should work. – ravikanth Jul 23 '13 at 04:26
  • Yes it worked. The log contains too much details like Handles NPM(K),PM(K),WS(K),VM(M),CPU(s),Id, Process name. Is it possible to have only 2 colums as the output ( Process Name and Running (Yes or No )) Thanks – Ireshad Jul 24 '13 at 03:07
  • What do you mean by running status? A process will appear in Get-Process output only when it is running. So, if a specific process or server name is not listed in your output file, it just means that the process is not running. I updated my answer. – ravikanth Jul 24 '13 at 14:02
  • Ok, Understood, but I was planning a formatted output. Not necessary. Only prob was, if there are 2 DIFFERENT processes runnung on the same server, the Hash table does no permit assigning 2 values like, $svrHash = @{"SERVER-01"="winmgmt";"SERVER-01"="Wecsvc"} As far as single process in single server it works. How can it be extended to 2/3 different tasks in same server ? – Ireshad Jul 26 '13 at 09:18
  • , if there are 2 DIFFERENT processes runnung on the same server, the Hash table does no permit assigning 2 values like, $svrHash = @{"SERVER-01"="winmgmt";"SERVER-01"="Wecsvc"} As far as single process in single server it works. How can it be extended to 2/3 different tasks in same server ? – Ireshad Jul 27 '13 at 12:57