Start & Stop Remote Service with WPS [Dynamic Version]

Check out my other version of this, in which the service is directly specified in the script here.

This script is used to stop a remote service on your network via WMI and PowerShell. Much like the other version on this blog where you must specify the service and computer to target in the code, this script does not require that. This script is given two variables, the first being the service name as it shows up in (get-wmiobject win32_service).Name and the second being the hostname of the remote computer you want to target. You could then make a third script to Restart-Remote-Service that would just simply call these one at a time, with the two specified variables.

That code looks like this:

stop-remote-service.ps1 $args[0] $args[1]
start-remote-service.ps1 $args[0] $args[1]
The script will give you a constant status of the service as it switches from running (in green) to stop pending (in yellow) and finally stopped (in red). I like the interactive status updating a lot.

Here's how you use them:

./Start-Remote-Service.ps1 helpsvc IISServ

./Stop-Remote-Service.ps1 helpsvc IISServ
Here's a practical example:

PS C:\Scripts> get-wmiobject win32_service -computer IISServ | fw
Then, you find the name of the service you want in the list and do this:

PS C:\Scripts> Stop-Remote-Service.ps1 helpsvc IISServ
The help service on computer IISServ will halt.

Enjoy!

Stop-Remote-Service.ps1

#ericgreer@gmail.com
#stop remote service script

$service = $args[0]
$computer = $args[1]

$query = (gwmi win32_service -computer $computer | where-object {$_.Name -eq "$service"})

$state = $query.State

if (((($query).name).length) -gt 0){

$trigger = 0

while ($state -eq “Running”) {
write-host “`r$service is $state” -nonewline -foregroundcolor green
sleep -milliseconds 100
if ($trigger -lt 1) {
$query.stopservice() | out-null
$trigger = 1
}
$state = (gwmi win32_service -computer $computer | where-object {$_.Name -eq "$service"}).State
}

while ($state -eq “Stop Pending”) {
write-host “`r$service is $state” -nonewline -foregroundcolor yellow
sleep -milliseconds 100
$state = (gwmi win32_service -computer $computer | where-object {$_.Name -eq "$service"}).State
}

if ($state -eq “Stopped”) {
write-host "`r " -nonewline
write-host “`r$service is $state`n” -nonewline -foregroundcolor red
}

if ($state -eq “Running”) {
write-host "`r " -nonewline
write-host “`r$service is $state (Failed to Stop)`n” -nonewline -foregroundcolor Green
}
}

else {
write-host "$service not found on $computer" -foregroundcolor Red
write-host "Try: gwmi win32_service -computer $computer | ft" -foregroundcolor green
}

Start-Remote-Service.ps1

#ericgreer@gmail.com
#start remote service script

$service = $args[0]
$computer = $args[1]

$query = (gwmi win32_service -computer $computer | where-object {$_.Name -eq "$service"})

$state = $query.State

if (((($query).name).length) -gt 0){

$trigger = 0

while ($state -eq "Stopped") {
write-host "`r$service is $state" -nonewline -foregroundcolor red
sleep -milliseconds 100
if ($trigger -lt 1) {
$query.startservice() | out-null
$trigger = 1
}
$state = (gwmi win32_service -computer $computer | where-object {$_.Name -eq "$service"}).State
}

while ($state -eq "Start Pending") {
write-host "`r$service is $state" -nonewline -foregroundcolor yellow
sleep -milliseconds 100
$state = (gwmi win32_service -computer $computer | where-object {$_.Name -eq "$service"}).State
}

if ($state -eq "Running") {
write-host "`r " -nonewline
write-host "`r$service is $state`n" -nonewline -foregroundcolor green
}

if ($state -eq "Stopped") {
write-host "`r " -nonewline
write-host "`r$service is $state (Failed to Start)`n" -nonewline -foregroundcolor red
}
}

else {
write-host "$service not found on $computer" -foregroundcolor Red
write-host "Try: gwmi win32_service -computer $computer | ft" -foregroundcolor green
}