[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Restart multiple Windows servers simultaneously

Posted on 2011-10-26
3
Medium Priority
?
340 Views
Last Modified: 2012-05-12
I currently use a script to restart Windows servers in the order they are listed in a text file.  How can the following script be modified to allow for the servers to be restarted simultaneously instead of in orders?
Set objShell = CreateObject("WScript.Shell")
If LCase(Right(Wscript.FullName, 11)) = "wscript.exe" Then
    strPath = Wscript.ScriptFullName
    strCommand = "%comspec% /c cscript  """ & strPath & """"
    objShell.Run(strCommand), 1, True
    Wscript.Quit
End If

strLogFile = "ServerRebootLog.txt"

'create an array for the computers
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objInput = objFSO.OpenTextFile("computers.txt", 1, False)
arrComputers = Split(objInput.ReadAll, vbCrLf)
objInput.Close

intMaxWaitTimeInSeconds = 300

Set objFSO = CreateObject("Scripting.FileSystemObject")
Const intForAppending = 8
Set objLog = objFSO.CreateTextFile(strLogFile, intForAppending, True)
objLog.WriteLine VbCrLf & String(80, "=") & VbCrLf & "Server reboot script run " & Now & VbCrLf & String(80, "=")

blnContinue = True
For Each strComputer In arrComputers
	WScript.Echo "Restarting " & strComputer & "..."
	objLog.WriteLine "Restarting " & strComputer & "..."
	If blnContinue = False Then Exit For
	strShutdown = "shutdown -r -t 0 -f -m \\" & strComputer
	objShell.Run strShutdown, 0, True
	' First wait for ping response to fail, meaning the computer has shut down
	While Ping(strComputer) = True
		WScript.Sleep 1000
	Wend
	WScript.Echo strComputer & " is now offline...waiting for restart..."
	objLog.WriteLine strComputer & " is now offline...waiting for restart..."
	' Next wait for ping response to succeed
	intTime = 0
	While Ping(strComputer) = False And intTime < intMaxWaitTimeInSeconds
		WScript.Sleep 1000
		intTime = intTime + 1
	Wend
	If intTime >= intMaxWaitTimeInSeconds Then
		WScript.Echo strComputer & " has not restarted in " & intMaxWaitTimeInSeconds & " seconds."
		objLog.WriteLine strComputer & " has not restarted in " & intMaxWaitTimeInSeconds & " seconds."
		blnContinue = False
	Else
		WScript.Echo strComputer & " is now online."
		objLog.WriteLine strComputer & " is now online."
	End If
Next

If blnContinue = False Then
	WScript.Echo "A computer has not restarted. Please check the systems."
	objLog.WriteLine "A computer has not restarted. Please check the systems."
Else
	WScript.Echo "All computers have restarted successfully."
	objLog.WriteLine "All computers have restarted successfully."
End If
objLog.Close

Function Ping(strComputer)
	Dim objShell, boolCode
	Set objShell = CreateObject("WScript.Shell")
	boolCode = objShell.Run("Ping -n 1 -w 300 " & strComputer, 0, True)
	If boolCode = 0 Then
		Ping = True
	Else
		Ping = False
	End If
End Function

Open in new window

0
Comment
Question by:Bud
3 Comments
 
LVL 34

Expert Comment

by:Paul MacDonald
ID: 37030591
If you take out the whole "wait for reboot" section, they'll still shutdown in order, but you won't wait for one to come back up before the next one goes down.

Comment out this part:
     ' First wait for ping response to fail, meaning the computer has shut down
      While Ping(strComputer) = True
            WScript.Sleep 1000
      Wend
      WScript.Echo strComputer & " is now offline...waiting for restart..."
      objLog.WriteLine strComputer & " is now offline...waiting for restart..."
      ' Next wait for ping response to succeed
      intTime = 0
      While Ping(strComputer) = False And intTime < intMaxWaitTimeInSeconds
            WScript.Sleep 1000
            intTime = intTime + 1
      Wend
      If intTime >= intMaxWaitTimeInSeconds Then
            WScript.Echo strComputer & " has not restarted in " & intMaxWaitTimeInSeconds & " seconds."
            objLog.WriteLine strComputer & " has not restarted in " & intMaxWaitTimeInSeconds & " seconds."
            blnContinue = False
      Else
            WScript.Echo strComputer & " is now online."
            objLog.WriteLine strComputer & " is now online."
      End If
0
 

Author Comment

by:Bud
ID: 37030731
I'd like to retain that aspect of the script.  Multiple windows opening up would be acceptable.  Also want to retain the logging.
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 2000 total points
ID: 37042899
Hi, this script does a fair bit of waiting, and VBScript is not capable of running multi-threaded operations.

Your best workaround would be to create a wrapper script, that would launch this script once per computer, and have each script instance only look after one computer.  This way, you could have many scripts run simultaneously.

Regards,

Rob.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A safe way to clean winsxs folder from your windows server 2008 R2 editions
This article provides a convenient collection of links to Microsoft provided Security Patches for operating systems that have reached their End of Life support cycle. Included operating systems covered by this article are Windows XP,  Windows Server…
This tutorial will give a short introduction and overview of Backup Exec 2012 and how to navigate and perform basic functions. Click on the Backup Exec button in the upper left corner. From here, are global settings for the application such as conne…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Suggested Courses

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question