Skip to content

Instantly share code, notes, and snippets.

@tobyqin
Created December 22, 2015 08:55
Show Gist options
  • Save tobyqin/d2e9ef631634123d5089 to your computer and use it in GitHub Desktop.
Save tobyqin/d2e9ef631634123d5089 to your computer and use it in GitHub Desktop.
function Start-TheService([string]$svc)
{
$STOP = "Stopped"
$RUN = "Running"
"Starting $svc..."
$s = get-service -name $svc
"Original status is : " + $s.Status
if($s.Status -eq $STOP)
{
Start-service -name $s.name
}
$s = get-service -name $svc
"Now status is : " + $s.Status
"Checking ..."
$s = get-service -name $svc
if($s.Status -eq $STOP)
{
throw "Exception: $svc cannot be started!!!"
}
"OK, all good."
}
function Stop-TheService([string]$svc)
{
$STOP = "Stopped"
$RUN = "Running"
"Stopping $svc..."
$s = get-service -name $svc
"Original status is : " + $s.Status
if($s.Status -eq $RUN)
{
stop-service -name $s.name
}
$s = get-service -name $svc
"Now status is : " + $s.Status
"Checking ..."
$s = get-service -name $svc
if($s.Status -eq $RUN)
{
throw "Exception: $svc cannot be stopped!!!"
}
"OK, all good."
}
function Restart-TheService([string]$svc)
{
Stop-TheService $svc
Start-TheService $svc
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment