Problem
You want to list or manage services on a remote machine.
Solution
To retrieve the services from a remote machine, use the [System.ServiceProcess. ServiceController]::GetServices() method from the .NET Framework.
PS >[void] ([Reflection.Assembly]::LoadWithPartialName("System.ServiceProcess"))
PS >[System.ServiceProcess.ServiceController]::GetServices("LEEDESK")
Status
Name
DisplayName
Running
ADAM_Test
Test
Stopped
Alerter
Alerter
Running
ALG
Application Layer Gateway Service
Stopped
AppMgmt
Application Management
Stopped
aspnet_state
ASP.NET State Service
Running
AudioSrv
Windows Audio
Running
BITS
Background Intelligent Transfer Ser...
Running
Browser
Computer Browser
Stopped
CiSvc
Indexing Service
To control one, use the WhereObject cmdlet to retrieve that one specifically and then call the methods on the object that manage it:
[void] ([Reflection.Assembly]::LoadWithPartialName("System.ServiceProcess"))
$service = [System.ServiceProcess.ServiceController]::GetServices("LEEDESK") | WhereObject { $_.Name eq "Themes" }
$service.Stop() $service.WaitForStatus("Stopped") StartSleep 2 $service.Start()
Discussion
If you have administrator privileges on a remote machine, the [System. ServiceProcess.ServiceController]::GetServices() method from the .NET Framework lets you control services on that machine.
When doing this, note that both of the examples from the solution require that you first load the assembly that contains the .NET classes that manage services. The *Service cmdlets load this DLL automati
cally.