0

我正在编写一个脚本,该脚本将在稍后使用任务计划程序中安排的以下脚本重新启动服务器列表。

该脚本使用 RunSpacePools 处理多个服务器,但似乎发生的是我的脚本未能记录成功重新启动。

我检查了使用此脚本重新启动的服务器,并发现来自 USER32 的事件 ID 1074 记录了 Shutdown/Restart 事件,但我为记录保存而创建的日志显示 Reboot Failed。

我只是不确定如何开始解决这个问题。任何帮助表示赞赏!

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()
4

1 回答 1

0

为了解决这个问题,我在输出中添加了一个为 rebootCheck true 或 rebootCheck false 设置的标志[PSCustomObject]。脚本执行的最后一件事是检查此标志并执行最终检查以确定是否需要重新启动失败电子邮件。

$counter = 0
do{$counter++

Start-Sleep -Seconds 1
 IF($counter -ge 600){#"Waited too long"
        [PsCustomObject]@{
             "Server" =  $server
             "Status" = "FailedToReboot!"
             "LastRebootTime" = "$LastReboot"
             "CurrentRebootTime" = "FailedToReboot!"
             "ReCheckReboot" = $true
          }#end custom logging object
        exit
 }#end if.
}#end do loop. #Break RebootSentWait continues script on next line.

until (-not (Test-Connection -ComputerName $server -Count 1 -Quiet))

ReCheckReboot然后在脚本结束之前使用 IF 语句检查名为的变量的真/假。

于 2016-07-15T18:08:11.707 回答