I'm working on a script that will reboot a list of servers at a later time using the below script scheduled in Task Scheduler.
The script uses RunSpacePools to process multiple servers but what seems to happen is my script fails to record the successful reboot.
I checked a server rebooted using this script, and found an event ID 1074 from USER32 which logs the Shutdown/Restart event but the log I create for record keeping says Reboot Failed.
I'm just not sure how to begin troubleshooting this problem. Any assistance is appreciated!
param([Parameter(Mandatory=$true)][string]$InputServerList)
#Script block for runspaces
$ScriptBlock = {
Param([Parameter(Mandatory=$true)][string]$server)
$ErrorActionPreference = "SilentlyContinue"
Try{
$LastReboot = Get-EventLog -ComputerName $server -LogName system | Where-Object {$_.EventID -eq '6005'} | Select -ExpandProperty TimeGenerated | select -first 1
Restart-Computer -ComputerName $server -Force
#New loop with counter, exit script if server did not reboot.
$max = 60;$i = 0
DO{
IF($i -gt $max){
[PsCustomObject]@{
"Server" = $server
"Status" = "FailedToReboot!"
"LastRebootTime" = "$LastReboot"
"CurrentRebootTime" = "FailedToReboot!"
}#end custom object
$subject="Warning: $server Did not Reboot! Please Check!"
#$SendTo = <Email List>
$body = @"
LastRebootTime = $LastReboot
Script waited 10 minutes for $server to reboot.
"@
#Send-MailMessage <details>
exit}#exit script and log failed to reboot.
$i++
Start-Sleep -Seconds 10
}#end DO
#At this point, server went offline. Script waits 10 minutes for server to come online.
While (Test-path "\\$server\c$")
$max = 60;$i = 0
DO{
IF($i -gt $max){
[PsCustomObject]@{
"Server" = $server
"Status" = "FailedToComeOnline!"
"LastRebootTime" = "$LastReboot"
"CurrentRebootTime" = "FailedToReboot!"
}
$body = @"
LastRebootTime = $LastReboot
Script waited 10 minutes for $server to come back online, please investigate.
"@
$subject="Warning!!!!: $server Did not Come Online! Please Check!"
#Send-MailMessage <details>
exit}#exit script and log failed to come online.
$i++
Start-Sleep -Seconds 10
}#end DO
While (-not(Test-path "\\$server\c$"))
$CurrentReboot = Get-EventLog -ComputerName $server -LogName system | Where-Object {$_.EventID -eq '6005'} | Select -ExpandProperty TimeGenerated | select -first 1
[PsCustomObject]@{
"Server" = $server
"Status" = "RebootSuccessful"
"LastRebootTime" = $LastReboot
"CurrentRebootTime" = "$CurrentReboot"
}
}#End Try.
Catch{
$errMsg = $_.Exception.Message.ToString().Trim()
#"$server : Failed with $errMsg"
[PsCustomObject]@{
"Server" = $server
"Status" = "FailedToReboot!"
"LastRebootTime" = "$errMsg"
"CurrentRebootTime" = "FailedToReboot!"
}#end custom object
}
}#end script block
#Define computers to work with.
$Computers = Get-Content $InputServerList
#Create Runspace pool
$RunspacePool = [RunspaceFactory]::CreateRunspacePool(30,30)
$RunspacePool.Open()
$Jobs =
foreach ( $Computer in $Computers )
{
$Job = [powershell]::Create().
AddScript($ScriptBlock).
AddArgument($Computer)
$Job.RunspacePool = $RunspacePool
[PSCustomObject]@{
Pipe = $Job
Result = $Job.BeginInvoke()
}
}
Write-Host 'Working..' -NoNewline
Do {
Start-Sleep -Seconds 5
} While ( $Jobs.Result.IsCompleted -contains $false)
Write-Host "Finished."
$(ForEach ($Job in $Jobs)
{ $Job.Pipe.EndInvoke($Job.Result) }) |
Export-Csv -Path "D:\Scripts\Reboot-Async-Results.csv" -NoTypeInformation -Append
#clean up runspace
$RunspacePool.Close()
$RunspacePool.Dispose()