Ireshad
Ireshad

Reputation: 81

How to check if a particular service is running in a remote computer using PowerShell

I have 3 servers, running 3 services:

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

Upvotes: 8

Views: 77508

Answers (2)

ravikanth
ravikanth

Reputation: 25800

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.

Upvotes: 3

Andy Arismendi
Andy Arismendi

Reputation: 52567

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.

Upvotes: 14

Related Questions