Is there a way to start or stop a service from powershell and block until the service starts?
Solution 1
Actually, it is possible to use both the Start-Service
and Stop-Service
PowerShell commands on a remote machine (discussion on this here). I have tested this and confirmed that it works:
Start-Service -inputobject $(get-service -ComputerName SERVER_NAME -Name SERVICE_NAME)
Also, you could try wmic
to stop the service. It does appear to wait for the service to stop before continuing, although I have not thoroughly tested this.
wmic /node:"SERVER_NAME" service where name="SERVICE_NAME" call stopservice
Solution 2
The PowerShell cmdlets Stop-Service
and Start-Service
will wait until the services are fully stopped and started respectively.
You can use the -Force
switch for Stop-Service
to make it also stop services that depend on the service you are trying to stop.
Also if you want to get rid of the Warning message saying the cmdlet is waiting for the service to finish stopping/starting you can add the switch -WarningAction SilentlyContinue
.
Related videos on Youtube
Justin Dearing
Updated on September 18, 2022Comments
-
Justin Dearing over 1 year
I have to reset a sequence of services on a group of servers in a certain order.
sc.exe
is asynchronous, it will return when the service is in the START_PENDING or STOP_PENDING state.Start-Service
/Stop-Service
will wait for the service to start or stop, but it only works on the local machine, and I don't have remoting enabled in my environment.Is there an alternative exe or CmdLet to do this? Looks like I'm going to have to use the .NET API.
-
Justin Dearing about 12 yearsI'm I am retarded. However, those seem to be local operations, and I don't want to turn on powershell remoting in this instance. Perhaps I need to revisit my decision not to use powershell remoting.
-
Andy Arismendi about 12 years@JustinDearing Oh didn't know you were doing this remotely...