Check status of remote services using Powershell

kam_uk
kam_uk used Ask the Experts™
on
Hi

I have a couple of Windows 2008 R2 servers running an app named FINANCE. There is a service on there named "FINANCE SERVICE 1" that I need to restart sometimes.

Since I've got a Windows 2008 R2 'admin' server, I'd like to be able to use Powershell to

i. Find out the status of "FINANCE SERVICE 1" on a given machine
ii. Start, stop or restart that service if I need

Is this possible?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2016
Commented:
Get-Service -ComputerName myserver | Where-Object { $_.Name -imatch "FINANCE SERVICE 1" } | ForEach-Object {$_.Start() }

and if you prefer to check before :

> Get-Service -ComputerName myserver | Where-Object { $_.Name -imatch "FINANCE SERVICE 1" } | Format-List *
Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

Commented:
Better to use the matching operator of the cmdlet itself:
Get-Service -ComputerName MyServer -Name "FINANCE SERVICE 1" | Restart-Service

Open in new window

Ciprian LozonschiOps Team Office 365/Messaging

Commented:
Never use Restart-Service, it will do this on local machine because it down't have remote capabilities. This is a mistake a lot of people are doing
To restart a service use WMI instead.

(Get-WmiObject -ComputerName server_name -Class Win32_Service -Filter "name='FINANCE SERVICE 1'").StartService()

Open in new window

Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

Commented:
That's true, of all *-service cmdlets only get-service and set-service allow for a ComputerName. Stupid implementation.
However, restarting a service consists of stopping and starting, not only starting, so we would have to do something like:
gwmi Win32_Service -ComputerName server_name -Filter "name='FINANCE SERVICE 1'" | % {$_.StopService(); $_.StartService()} | out-null

Open in new window

Ciprian LozonschiOps Team Office 365/Messaging

Commented:
I was just commenting on the restart part. Now I see I used there StartService(), even I was thingking to stop and them start service.

Qlemo your point is good

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial