PoshCode Archive  Hex Artifact Content

Artifact b69dc729f3869c7a5fadf4c0685a9c8e827386cf7f7126c42cc4481ec48b4c65:

  • File Stop-service-and-wait.ps1 — part of check-in [3da99d740c] at 2018-06-10 14:19:06 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 every reboot or can be run manually to reboot. (user: AdrianWoodrup size: 1319)

0000: 23 20 65 6e 63 6f 64 69 6e 67 3a 20 61 73 63 69  # encoding: asci
0010: 69 0d 0a 23 20 61 70 69 3a 20 70 6f 77 65 72 73  i..# api: powers
0020: 68 65 6c 6c 0d 0a 23 20 74 69 74 6c 65 3a 20 53  hell..# title: S
0030: 74 6f 70 20 73 65 72 76 69 63 65 20 61 6e 64 20  top service and 
0040: 77 61 69 74 2e 2e 2e 0d 0a 23 20 64 65 73 63 72  wait.....# descr
0050: 69 70 74 69 6f 6e 3a 20 56 65 72 79 20 73 69 6d  iption: Very sim
0060: 70 6c 65 20 73 63 72 69 70 74 20 74 68 61 74 20  ple script that 
0070: 73 74 6f 70 73 20 61 20 73 65 72 76 69 63 65 20  stops a service 
0080: 61 6e 64 20 77 61 69 74 73 20 66 6f 72 20 69 74  and waits for it
0090: 20 74 6f 20 73 74 6f 70 20 62 65 66 6f 72 65 20   to stop before 
00a0: 72 65 62 6f 6f 74 69 6e 67 2e 20 54 68 69 73 20  rebooting. This 
00b0: 73 63 72 69 70 74 20 63 61 6e 20 62 65 20 65 64  script can be ed
00c0: 69 74 65 64 20 73 6f 20 69 74 20 72 75 6e 73 20  ited so it runs 
00d0: 6f 6e 20 65 76 65 72 79 20 72 65 62 6f 6f 74 20  on every reboot 
00e0: 6f 72 20 63 61 6e 20 62 65 20 72 75 6e 20 6d 61  or can be run ma
00f0: 6e 75 61 6c 6c 79 20 74 6f 20 72 65 62 6f 6f 74  nually to reboot
0100: 2e 0d 0a 23 20 76 65 72 73 69 6f 6e 3a 20 30 2e  ...# version: 0.
0110: 31 0d 0a 23 20 74 79 70 65 3a 20 73 63 72 69 70  1..# type: scrip
0120: 74 0d 0a 23 20 61 75 74 68 6f 72 3a 20 41 64 72  t..# author: Adr
0130: 69 61 6e 57 6f 6f 64 72 75 70 0d 0a 23 20 6c 69  ianWoodrup..# li
0140: 63 65 6e 73 65 3a 20 43 43 30 0d 0a 23 20 78 2d  cense: CC0..# x-
0150: 70 6f 73 68 63 6f 64 65 2d 69 64 3a 20 36 36 35  poshcode-id: 665
0160: 37 0d 0a 23 20 78 2d 61 72 63 68 69 76 65 64 3a  7..# x-archived:
0170: 20 32 30 31 36 2d 31 32 2d 33 30 54 30 33 3a 30   2016-12-30T03:0
0180: 33 3a 35 32 0d 0a 23 20 78 2d 70 75 62 6c 69 73  3:52..# x-publis
0190: 68 65 64 3a 20 32 30 31 36 2d 31 32 2d 32 32 54  hed: 2016-12-22T
01a0: 31 34 3a 32 30 3a 30 30 0d 0a 23 0d 0a 23 0d 0a  14:20:00..#..#..
01b0: 3c 23 20 0d 0a 54 68 69 73 20 73 63 72 69 70 74  <# ..This script
01c0: 20 73 74 6f 70 73 20 74 68 65 20 73 65 72 76 69   stops the servi
01d0: 63 65 2c 20 74 68 65 6e 20 77 61 69 74 73 20 66  ce, then waits f
01e0: 6f 72 20 74 68 65 20 73 65 72 76 69 63 65 20 74  or the service t
01f0: 6f 20 73 74 6f 70 20 62 65 66 6f 72 65 20 63 6f  o stop before co
0200: 6e 74 69 6e 75 69 6e 67 20 77 69 74 68 20 74 68  ntinuing with th
0210: 65 20 72 65 62 6f 6f 74 2f 73 68 75 74 64 6f 77  e reboot/shutdow
0220: 6e 20 0d 0a 54 68 65 20 73 63 72 69 74 70 20 63  n ..The scritp c
0230: 61 6e 20 62 65 20 70 75 73 68 65 64 20 74 6f 20  an be pushed to 
0240: 61 20 73 65 72 76 65 72 2f 50 63 20 75 73 69 6e  a server/Pc usin
0250: 67 20 47 72 6f 75 70 20 50 6f 6c 69 63 79 20 6f  g Group Policy o
0260: 72 20 52 65 67 69 73 74 72 79 20 6f 72 20 72 75  r Registry or ru
0270: 6e 20 6d 61 6e 75 61 6c 6c 79 2e 0d 0a 54 68 65  n manually...The
0280: 20 73 68 75 74 64 6f 77 6e 20 73 63 72 69 70 74   shutdown script
0290: 20 52 65 67 69 73 74 72 79 20 6b 65 79 20 69 73   Registry key is
02a0: 3a 0d 0a 09 48 4b 4c 4d 3a 5c 53 4f 46 54 57 41  :...HKLM:\SOFTWA
02b0: 52 45 5c 4d 69 63 72 6f 73 6f 66 74 5c 57 69 6e  RE\Microsoft\Win
02c0: 64 6f 77 73 5c 43 75 72 72 65 6e 74 56 65 72 73  dows\CurrentVers
02d0: 69 6f 6e 5c 47 72 6f 75 70 20 50 6f 6c 69 63 79  ion\Group Policy
02e0: 5c 53 74 61 74 65 5c 4d 61 63 68 69 6e 65 5c 53  \State\Machine\S
02f0: 63 72 69 70 74 73 5c 53 68 75 74 64 6f 77 6e 5c  cripts\Shutdown\
0300: 0d 0a 0d 0a 23 3e 0d 0a 23 20 74 79 70 65 20 74  ....#>..# type t
0310: 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 73  he name of the s
0320: 65 72 76 69 63 65 20 69 6e 20 74 68 65 20 71 75  ervice in the qu
0330: 6f 74 65 73 20 68 65 72 65 0d 0a 24 53 65 72 76  otes here..$Serv
0340: 69 63 65 4e 61 6d 65 20 3d 20 27 53 65 72 76 69  iceName = 'Servi
0350: 63 65 20 4e 61 6d 65 27 0d 0a 0d 0a 53 74 6f 70  ce Name'....Stop
0360: 2d 53 65 72 76 69 63 65 20 24 53 65 72 76 69 63  -Service $Servic
0370: 65 4e 61 6d 65 0d 0a 77 72 69 74 65 2d 68 6f 73  eName..write-hos
0380: 74 20 24 53 65 72 76 69 63 65 4e 61 6d 65 27 2e  t $ServiceName'.
0390: 2e 2e 27 20 2d 4e 6f 4e 65 77 4c 69 6e 65 0d 0a  ..' -NoNewLine..
03a0: 24 54 65 73 74 53 65 72 76 69 63 65 20 3d 20 47  $TestService = G
03b0: 65 74 2d 53 65 72 76 69 63 65 20 20 24 53 65 72  et-Service  $Ser
03c0: 76 69 63 65 20 7c 20 53 65 6c 65 63 74 2d 4f 62  vice | Select-Ob
03d0: 6a 65 63 74 20 27 53 74 61 74 75 73 27 0d 0a 57  ject 'Status'..W
03e0: 68 69 6c 65 28 24 54 65 73 74 53 65 72 76 69 63  hile($TestServic
03f0: 65 20 7c 20 77 68 65 72 65 20 7b 24 5f 2e 53 74  e | where {$_.St
0400: 61 74 75 73 20 2d 65 71 20 27 52 75 6e 6e 69 6e  atus -eq 'Runnin
0410: 67 27 7d 29 7b 09 0d 0a 09 57 72 69 74 65 2d 48  g'}){....Write-H
0420: 6f 73 74 20 27 2e 27 2d 4e 6f 4e 65 77 4c 69 6e  ost '.'-NoNewLin
0430: 65 20 0d 0a 09 53 6c 65 65 70 20 32 09 0d 0a 09  e ...Sleep 2....
0440: 7d 0d 0a 09 0d 0a 23 20 49 66 20 79 6f 75 20 77  }.....# If you w
0450: 61 6e 74 20 74 6f 20 73 68 75 74 64 6f 77 6e 20  ant to shutdown 
0460: 74 68 65 20 63 6f 6d 70 75 74 65 72 20 61 64 64  the computer add
0470: 20 74 68 65 20 63 6f 6d 6d 61 6e 64 20 22 53 68   the command "Sh
0480: 75 74 64 6f 77 6e 20 2f 74 20 30 22 20 28 77 69  utdown /t 0" (wi
0490: 74 68 6f 75 74 20 71 75 6f 75 74 65 73 29 6f 6e  thout quoutes)on
04a0: 74 6f 20 74 68 65 20 62 6f 74 74 6f 6d 20 6c 69  to the bottom li
04b0: 6e 65 2e 0d 0a 23 20 49 66 20 79 6f 75 20 77 61  ne...# If you wa
04c0: 6e 74 20 74 6f 20 52 65 62 6f 6f 74 20 74 68 65  nt to Reboot the
04d0: 20 63 6f 6d 70 75 74 65 72 20 74 68 65 6e 20 61   computer then a
04e0: 64 64 20 74 68 65 20 63 6f 6d 6d 61 6e 64 20 22  dd the command "
04f0: 52 65 73 74 61 72 74 2d 63 6f 6d 70 75 74 65 72  Restart-computer
0500: 22 20 28 77 69 74 68 6f 75 74 20 71 75 6f 74 65  " (without quote
0510: 73 29 20 6f 6e 20 74 68 65 20 6c 69 6e 65 20 62  s) on the line b
0520: 65 6c 6f 77 2e 0d 0a                             elow...