PoshCode Archive  Artifact Content

Artifact 5a5ceca800c2f5d2ef4435c97b0e9e22c6d925c13e0f4cf62f9897419d12353c:

  • File Stop-service-and-wait.ps1 — part of check-in [f174b5db95] at 2018-06-10 13:33:39 on branch trunk — Very simple script that stops a service and waits for it to stop before rebooting. This script can be edited so it runs on ever reboot or its run to reboot. (user: AdrianWoodrup size: 1306)

# encoding: ascii
# api: powershell
# title: Stop service and wait...
# description: Very simple script that stops a service and waits for it to stop before rebooting. This script can be edited so it runs on ever reboot or its run to reboot.
# version: 0.1
# type: script
# author: AdrianWoodrup
# license: CC0
# x-poshcode-id: 3962
# x-archived: 2015-03-15T07:26:08
# x-published: 2015-02-19T11:09:00
#
#
<# 
This script stops the service, then waits for the service to stop before continuing with the reboot/shutdown 
The scritp can be pushed to a server/Pc using Group Policy or Registry or run manually.
The shutdown script Registry key is:
	HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\State\Machine\Scripts\Shutdown\

#>
# type the name of the service in the quotes here
$ServiceName = 'Service Name'

Stop-Service $ServiceName
write-host $ServiceName'...' -NoNewLine
$TestService = Get-Service  $Service | Select-Object 'Status'
While($TestService | where {$_.Status -eq 'Running'}){	
	Write-Host '.'-NoNewLine 
	Sleep 2	
	}
	
# If you want to shutdown the computer add the command "Shutdown /t 0" (without quoutes)onto the bottom line.
# If you want to Reboot the computer then add the command "Restart-computer" (without quotes) on the line below.