Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

powershell

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?

like image 240
Ireshad Avatar asked Jul 21 '13 04:07

Ireshad


People also ask

How can I tell if a Service is running on a remote computer?

Windows natively has a command line tool which can be used to check if a service is running or not on a remote computer. The utility/tool name is SC.exe. SC.exe has parameter to specify the remote computer name. You can check service status only on one remote computer at a time.

How do I get remote computer services in PowerShell?

Getting Remote Services With Windows PowerShell, you can use the ComputerName parameter of the Get-Service cmdlet to get the services on remote computers. The ComputerName parameter accepts multiple values and wildcard characters, so you can get the services on multiple computers with a single command.

How do I list running services in PowerShell?

Using the Get-Service PowerShell cmdlet, you can generate a list of Windows Services running on your Windows 10/8/7 computer. Open an elevated PowerShell console, type Get-Service and hit Enter. You will see a list of all the Services installed on your Windows system.


2 Answers

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.

like image 176
Andy Arismendi Avatar answered Oct 20 '22 13:10

Andy Arismendi


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.

like image 3
ravikanth Avatar answered Oct 20 '22 12:10

ravikanth