SHARE
TWEET

Reboot.bat

sweenig Jan 14th, 2014 (edited) 2,843 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. @echo off
  2. if "%1"=="" goto:HELP
  3. ::shutdown the server
  4. echo.
  5. echo %1 rebooting...
  6. shutdown /r /d p:4:1 /m \\%1 /t 0 /c "Remote reboot requested"
  7. if errorlevel 1 GOTO:EOF
  8. ::Ping until unsuccessful then successful
  9. set pingfailyet=FALSE
  10. set pingwaittime=3
  11. echo.
  12. echo Pinging %1...
  13. :startping
  14. ping -n 1 %1 | find "Reply"
  15. if %errorlevel%==0 (
  16.  if %pingfailyet%==FALSE (
  17. ::echo %1 hasn't gone down yet.  Pinging again in %pingwaittime% seconds...
  18.   CHOICE /C x /N /T %pingwaittime% /D x > NUL
  19.   goto startping
  20.  ) else (
  21.   echo Successfully pinged %1.
  22.   goto endping
  23.  )
  24. ) else (
  25.  set pingfailyet==TRUE
  26.  echo No reply from %1.
  27.  CHOICE /C x /N /T %pingwaittime% /D x > NUL
  28.  goto startping
  29. )
  30. :endping
  31. ::use tcping to check when RDP becomes available
  32. echo.
  33. echo Waiting for RDP on %1 to become available...
  34. if not exist tcping.exe call:gatherer
  35. tcping.exe -t -i %pingwaittime% -s %1 3389
  36. if %errorlevel%==1 GOTO:EOF
  37. ::launch RDP
  38. echo.
  39. if NOT "%2"=="NOCONNECT" (
  40. echo RDP is available on %1.  Connecting...
  41. start mstsc /v:%1 /f
  42. ) ELSE (
  43. echo RDP is available on %1.
  44. )
  45. goto:EOF
  46. :HELP
  47. echo ERROR: Missing server name, exiting...
  48. echo.
  49. echo This script reboots a server, pings until it doesn't respond,
  50. echo pings until it responds, then waits for RDP to come up, then
  51. echo launches the RDP client and connects to the server.  If your
  52. echo password is saved and you don't have a welcome message before
  53. echo logon, you should be brought directly to the desktop of the
  54. echo server after rebooting.
  55. echo.
  56. echo Usage: reboot server_name [NOCONNECT]
  57. echo.
  58. echo Options:
  59. echo     NOCONNECT          Skip launching of mstsc.exe to reconnecto to the server
  60. echo.
  61. echo The first argument is the name of the server to reboot.
  62. echo The second argument is optional. If you specify NOCONNECT,
  63. echo reboot.bat will skip the last section that launches the RDP client.
  64. echo.
  65. echo (C) 2013-2014 Stuart Weenig stuart.weenig.com
  66. echo version 2.3 Last Updated Jan 14, 2014.
  67. echo.
  68. echo Use a command like the following to run this for a list of servers:
  69. echo     FOR %%A in (server1 server2 server3) DO (start reboot.bat %%A [NOCONNECT])
  70. goto:EOF
  71. :gatherer
  72. echo TCPing missing. Downloading...
  73. (
  74. echo dim xHttp
  75. echo set xHttp = createobject^("Microsoft.XMLHTTP"^)
  76. echo dim bStrm
  77. echo set bStrm = createobject^("Adodb.Stream"^) 'create bitstream object
  78. echo dim targetURL
  79. echo targetURL = "http://www.elifulkerson.com/projects/downloads/tcping-0.31/tcping.exe"
  80. echo WScript.echo "Downloading TCPing from " ^& targetURL ^& "..."
  81. echo xHttp.Open "GET", targetURL ,False
  82. echo xHttp.Send
  83. echo WScript.echo "HTTP Response Code: " ^& xHttp.status
  84. echo dim fso
  85. echo dim curDir
  86. echo set fso = CreateObject^("Scripting.FileSystemObject"^)
  87. echo curDir = fso.GetAbsolutePathName^("."^)
  88. echo set fso = nothing
  89. echo if xHttp.status^>=400 and xHttp.status ^<=599 then
  90. echo    WScript.echo targetURL ^& " was not found."
  91. echo    WScript.Quit 1
  92. echo Else
  93. echo    with bStrm
  94. echo        .type = 1
  95. echo        .open
  96. echo        .write xHttp.responseBody
  97. echo        WScript.echo "Saving to " ^& curDir ^& "\tcping.exe..."
  98. echo        .savetofile curDir ^& "\tcping.exe", 2
  99. echo    end with
  100. echo End If
  101. ) > gatherer.vbs
  102. cscript gatherer.vbs
  103. del /q gatherer.vbs
  104. GOTO:EOF
RAW Paste Data
Top