I needed to remotely reboot some servers in batches. Here is some quick code to reboot the servers. The script will issue the first reboot using the server names in the first .txt file. it will then pause and display a popup, wait 8 minutes and then fire off the second .txt file. This repeats until the end. You will need to change “Domain\account” to your domain and an account that has admin rights. Also note, that Powershell does not have a pause function like cmd.exe does. The pause function was created after finding some examples from around the web. Enjoy!
$Cred = Get-Credential -credential Domain\account
$servgroup = “”
Function Pause ($Message = “Press any key to continue . . . “) {
If ($psISE) {
# The “ReadKey” functionality is not supported in Windows PowerShell ISE.
$alert = $servgroup + “Server Group Rebooting. Waiting 8 Minutes.”
$Shell = New-Object -ComObject “WScript.Shell”
$Button = $Shell.Popup($alert,480, “Servers Reboot, Script Paused”, 64)
Return
}
Write-Host -NoNewline $Message
$Ignore =
16, # Shift (left or right)
17, # Ctrl (left or right)
18, # Alt (left or right)
20, # Caps lock
91, # Windows key (left)
92, # Windows key (right)
93, # Menu key
144, # Num lock
145, # Scroll lock
166, # Back
167, # Forward
168, # Refresh
169, # Stop
170, # Search
171, # Favorites
172, # Start/Home
173, # Mute
174, # Volume Down
175, # Volume Up
176, # Next Track
177, # Previous Track
178, # Stop Media
179, # Play
180, # Mail
181, # Select Media
182, # Application 1
183 # Application 2
While ($KeyInfo.VirtualKeyCode -Eq $Null -Or $Ignore -Contains $KeyInfo.VirtualKeyCode) {
$KeyInfo = $Host.UI.RawUI.ReadKey(“NoEcho, IncludeKeyDown”)
}
Write-Host
}
restart-computer -computername (Get-Content C:\batch1.txt) -force -credential $Cred
$servgroup = “Batch1″
pause
restart-computer -computername (get-content C:\batch2.txt) -force -credential $Cred
restart-computer -computername (get-content c:\batch3.txt) -force -credential $cred
$servgroup = “Batch2 & Batch3 ”
pause
restart-computer -computername (get-content c:\batch4.txt) -force -credential $Cred
$servgroup = “Batch4 ”
pause