Skip to content

Instantly share code, notes, and snippets.

@jrdtechnologies
Created April 16, 2019 07:36
Show Gist options
  • Select an option

  • Save jrdtechnologies/99ef11dbd70268da1d0457510856ebd4 to your computer and use it in GitHub Desktop.

Select an option

Save jrdtechnologies/99ef11dbd70268da1d0457510856ebd4 to your computer and use it in GitHub Desktop.
param (
[string]$service = $(throw "-service is required.")
)
write-output 'AutoStopStartService()-> START, svc is $service'
# get service
$ServiceName = $service
$arrService = Get-Service -Name $ServiceName
# get start mode
write-output $arrService.Status
$stu = Get-WmiObject -Class Win32_Service -Property StartMode -Filter "Name='$service'" | Select StartMode;
# if not Disabled, Disable it
if ($stu -NotMatch 'Disabled')
{
write-output 'AutoStopStartService()-> DISABLE it!'
Start-Sleep -seconds 10
set-service $ServiceName -startuptype disabled
}
# if Running, Stop it
if ($arrService.Status -ne 'Running')
{
write-output 'AutoStopStartService()-> Service already stopped!'
}
else
{
write-output 'AutoStopStartService()-> STOP IT!'
Stop-Service $ServiceName
Start-Sleep -seconds 10
$arrService.Refresh()
Write-Host 'Service NEW STATUS-> ' $arrService.Status
}
# done
write-output 'AutoStopStartService()-> DONE'
Exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment