Start & Stop Remote Service with WPS [Static Version]

These scripts remotely start and stop a set service with WMI through PowerShell. All you have to do is change the service from 'Your Service' to the display name of the service you want to halt, then chnage the 'Your Computer' blank to the hostname or IP of the computer the service is running on. The 'Static Version' tag above means that this service is specified within the script, and not by the user each time.

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.

Start-Remote-Service.ps1

#SETTINGS
$service = "Your Service"
$computer = "Your Computer"

$state = (gwmi -query "select * from win32_service where DisplayName='$service'" -computer $computer).State
$trigger = 0

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

while ($state -eq "Start Pending") {
write-host "`r$service is $state" -nonewline -foregroundcolor yellow
sleep -milliseconds 100
$state = (gwmi -query "select * from win32_service where DisplayName='$service'" -computer $computer).State
}

#SETTINGS
$service = "Servers Alive"
$computer = "Vesuvius"

$state = (gwmi -query "select * from win32_service where DisplayName='$service'" -computer $computer).State
$trigger = 0

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

while ($state -eq "Start Pending") {
write-host "`r$service is $state" -nonewline -foregroundcolor yellow
sleep -milliseconds 100
$state = (gwmi -query "select * from win32_service where DisplayName='$service'" -computer $computer).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
}

Stop-Remote-Service.ps1

#SETTINGS
$service = "Your Service"
$computer = "Your Computer"

$state = (gwmi -query "select * from win32_service where DisplayName='$service'" -computer $computer).State
$trigger = 0

while ($state -eq "Running") {
write-host "`r$service is $state" -nonewline -foregroundcolor green
sleep -milliseconds 100
if ($trigger -lt 1) {
(gwmi -query "select * from win32_service where DisplayName='$service'" -computer $computer).stopservice() | out-null
$trigger = 1
}
$state = (gwmi -query "select * from win32_service where DisplayName='$service'" -computer $computer).State
}

while ($state -eq "Stop Pending") {
write-host "`r$service is $state" -nonewline -foregroundcolor yellow
sleep -milliseconds 100
$state = (gwmi -query "select * from win32_service where DisplayName='$service'" -computer $computer).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
}

You can rename this script to anything you like once you set the service name and target computer, effectively giving you control over all your most important services. Works like a charm for monitoring software.