Created
April 16, 2019 07:36
-
-
Save jrdtechnologies/99ef11dbd70268da1d0457510856ebd4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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