PWSH: Reboot servers from a file
-
A little script I wrote to reboot a bunch of servers at a specific, calculated time. You can adjust the filename or time in the header. Default time is 730 pm. You need winrm enabled on all the target systems, and I don't know what the limit would be on server count. If you run a NOC, warn them before all the servers reboot at exactly the same time. lol!
###########
#Boot systems in a file at 730 pm$file = "$($ENV:USERPROFILE)\desktop\systems.txt"
$target = Get-Date -Hour 19 -Minute 30 -Second 0#don't change anything below here
$systems = get-content $file
$utz = Get-Date -Hour 19 -Minute 30 -Second 0 -UFormat "%Z"
$now = $(Get-Date)
$serverTZ = (get-timezone).id
$seconds = ($target - $now).TotalSeconds
$final = [math]::Round($seconds)
$final = $final - 15
Write-Host -ForegroundColor cyan -NoNewline "nSeconds until $target $serverTZ ($utz UTC)
:"
write-host -ForegroundColor Yellow " $final"
$final |clip
Write-Host -ForegroundColor Cyan "15 second delay to cancel a scheduled reboot for $($($systems).count) systems:`n$systems"
#Write-Progress -SecondsRemaining 15 -Activity "Reboot schedule countdown."
Start-Sleep -Seconds 15
Invoke-Command -ComputerName $Systems -ScriptBlock {shutdown -r -t $using:final}
Write-Host -ForegroundColor Cyan "End of Line"