I have 3 servers, running 3 services:
serv1.exe
serviceserv2.exe
service serv3.exe
serviceUsing PowerShell how can I query each service to check if they are running and output the information to a text file?
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With