HomePage > Scripts > Reboot Script

Reboot Script

Tags:  

I didn't write this. But it solves a common sysadmin problem: reboot a remote server and then know exactly when it comes back up.

It's vbscript. I found it here, at StackOverflow.com.

  1. ' Remotely reboot a server and
  2. ' wait for server to come back up.
  3. ' Usage:  cscript /nologo /E:VBScript RebootWait.vbs <Server Name>
  4. ' Shawn Poulson, 2008.09.11
  5. ' http://stackoverflow.com/questions/56644
  6.  
  7. ' Get server name from command line
  8. If WScript.Arguments.Count <> 1 Then
  9.    ShowUsage()
  10.    WScript.Quit(1)
  11. End If
  12. ServerName = WScript.Arguments(0)
  13.  
  14. ' Verify server is currently up
  15. WScript.StdOut.WriteLine Now & ": Verify server '" & ServerName & "' is currently up..."
  16. If Not IsAvailable(ServerName) Then
  17.    WScript.StdOut.WriteLine "Error: Server is down.  Reboot aborted!"
  18.    WScript.Quit(1)
  19. End If
  20. WScript.StdOut.WriteLine Now & ": Server is up."
  21.  
  22. ' Reboot server
  23. WScript.StdOut.WriteLine Now & ": Rebooting server '" & ServerName & "'..."
  24. RebootStatus = RebootServer(ServerName)
  25. If RebootStatus < 0 Then
  26.    WScript.StdOut.WriteLine "Error: Reboot returned error " & RebootStatus
  27.    WScript.Quit(1)
  28. End If
  29. WScript.StdOut.WriteLine Now & ": Reboot command was successful"
  30.  
  31. ' Wait for server to come down
  32. WScript.StdOut.Write Now & ": Waiting for server '" & ServerName & "' to go down..."
  33. WaitCount = 0
  34. Do While IsAvailable(ServerName)
  35.    WaitCount = WaitCount + 1
  36.    If WaitCount > 60 Then ' 5 min timeout
  37.       WScript.StdOut.WriteLine "Error: Timeout waiting for server to come down!"
  38.       WScript.Quit(1)
  39.    End If
  40.    WScript.StdOut.Write(".")
  41.    WScript.Sleep(5000)
  42. Loop
  43. WScript.StdOut.WriteLine "Success!"
  44. WScript.StdOut.WriteLine Now & ": Server is down."
  45.  
  46. ' Wait for server to come back up
  47. WScript.StdOut.Write Now & ": Waiting for server '" & ServerName & "' to come back up..."
  48. WaitCount = 0
  49. Do While Not IsAvailable(ServerName)
  50.    WaitCount = WaitCount + 1
  51.    If WaitCount > 240 Then ' 20 min timeout
  52.       WScript.StdOut.WriteLine "Error: Timeout waiting for server to come back up!"
  53.       WScript.Quit(1)
  54.    End If
  55.    WScript.StdOut.Write(".")
  56.    WScript.Sleep(5000)
  57. Loop
  58. WScript.StdOut.WriteLine "Success!"
  59. WScript.StdOut.WriteLine Now & ": Server is back up after reboot."
  60.  
  61. ' Success!
  62. WScript.Quit(0)
  63.  
  64.  
  65. Sub ShowUsage()
  66.    WScript.Echo "Usage: " & WScript.ScriptName & " <Server name>"
  67. End Sub
  68.  
  69. Function RebootServer(ServerName)
  70. ' Returns:
  71. ' 1 = Successfully issued reboot command
  72. ' -2 = Could not reach server
  73. ' -3 = Reboot command failed
  74.    Dim OpSystem
  75.    On Error Resume Next
  76.    For Each OpSystem in GetObject("winmgmts:{(Shutdown)}!\\" & ServerName &"\root\CIMV2").ExecQuery("select * from Win32_OperatingSystem where Primary=true")
  77.       On Error GoTo 0
  78.  
  79.       If IsObject(OpSystem) Then ' Invoke forced reboot
  80.          If OpSystem.Win32Shutdown(60) = 0 Then ' Success
  81.             RebootServer = 1
  82.          Else ' Command failed
  83.             RebootServer = -3
  84.          End If
  85.       Else
  86.          RebootServer = -2
  87.       End If
  88.    Next
  89. End Function
  90.  
  91. Function IsAvailable(ServerName) ' Return True if available
  92.    ' Use Windows RPC service state as vital sign
  93.    IsAvailable = (GetServiceState(ServerName, "RpcSs") = "Running")
  94. End Function
  95.  
  96. Function GetServiceState(ServerName, ServiceName)
  97. ' Return one of:
  98. '  Stopped, Start Pending, Stop Pending, Running, Continue Pending, Pause Pending, Paused, Unknown
  99.    Dim Service
  100.    On Error Resume Next
  101.    Set Service = GetObject("winmgmts:\\" & ServerName & "\root\CIMV2:Win32_Service='" & ServiceName &"'")
  102.    On Error GoTo 0
  103.    If IsObject(Service) Then GetServiceState = Service.State
  104. End Function
                  

0 Comments  Show recent to old
Post a comment


 RSS of this page