Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Powershell to stop a service remotely without WMI or remoting

Tags:

powershell

I came across this one liner that appears to work:

stop-service -inputobject $(get-service -ComputerName remotePC -Name Spooler) 

Can anyone explain why, because I thought stop-service didn't work unless you either used remoting or it occurred on the local host.

like image 523
fenster Avatar asked Apr 26 '12 22:04

fenster


People also ask

How do I stop a remote service in PowerShell?

Method 3: Using PowerShell Get-Service -ComputerName computername -Name servicename | Restart-Service -Force. Get-Service -ComputerName computername -Name servicename | Stop-Service -Force.

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.


1 Answers

The output of Get-Service is a System.ServiceProcess.ServiceController .NET class that can operate on remote computers. How it accomplishes that, I don't know - probably DCOM or WMI. Once you've gotten one of these from Get-Service, it can be passed into Stop-Service which most likely just calls the Stop() method on this object. That stops the service on the remote machine. In fact, you could probably do this as well:

(get-service -ComputerName remotePC -Name Spooler).Stop() 
like image 122
Keith Hill Avatar answered Oct 07 '22 16:10

Keith Hill