0

現在、次のコードを使用してサーバーの再起動をスケジュールしています。これは少数のサーバーではうまく機能しますが、サーバーが多数 (80 以上) ある場合Register-ScheduledJobは、サーバーごとに時間がかかるため問題になります。

$user = Get-Credential -UserName $env:USERNAME -Message "UserName/password for scheduled Reboot"
$trigger = New-JobTrigger -once -at $date
$script = [ScriptBlock]::Create("D:\Scripts\Scheduled-Reboot-Single.ps1 -server $server")
Register-ScheduledJob -Name $server -Credential $user -Trigger $trigger -ScriptBlock $script

workflow私の調査では、との使用が指摘されましたforeach -parallel

私が遭遇する問題は、正確なロギングです。ログ ファイルは作成されましたが、列の順序が正しくありません。

workflow Do-ScheduledReboot{
  Param([string[]]$servers)

  foreach -parallel($server in $servers) {

    InlineScript {
      try {
        $LastReboot = Get-EventLog -ComputerName $using:server -LogName system |
                      Where-Object {$_.EventID -eq '6005'} |
                      Select -ExpandProperty TimeGenerated |
                      select -first 1

        #New loop with counter, exit script if server did not reboot.
        $max = 20; $i = 0
        do {
          if ($i -gt $max) {
            $hash = @{
              "Server" =  $using:server
              "Status" = "FailedToReboot!"
              "LastRebootTime" = "$LastReboot"
              "CurrentRebootTime" = "FailedToReboot!"
            }
            $newRow = New-Object PsObject -Property $hash
            $rnd = Get-Random -Minimum 5 -Maximum 40
            Start-Sleep -Seconds $rnd
            Export-Csv D:\workflow-results.csv -InputObject $newrow -Append -Force

            exit
          }#exit script and log failed to reboot.
          $i++

          Start-Sleep -Seconds 15
        } while (Test-path "\\$using:server\c$")
        $max = 20; $i = 0
        do {
          if ($i -gt $max) {
            $hash = @{
              "Server" =  $using:server
              "Status" = "FailedToComeOnline!"
              "LastRebootTime" = "$LastReboot"
              "CurrentRebootTime" = "FailedToReboot!"
            }
            $newRow = New-Object PsObject -Property $hash
            $rnd = Get-Random -Minimum 5 -Maximum 40
            Start-Sleep -Seconds $rnd
            Export-Csv D:\workflow-results.csv -InputObject $newrow -Append -Force

            exit
          }#exit script and log failed to come online.
          $i++

          Start-Sleep -Seconds 15
        } while (-not(Test-path "\\$using:server\c$"))

        $CurrentReboot = Get-EventLog -ComputerName $using:server -LogName system | Where-Object {$_.EventID -eq '6005'} | Select -ExpandProperty TimeGenerated | select -first 1
        $hash = @{
          "Server" =  $using:server
          "Status" = "RebootSuccessful"
          "LastRebootTime" = $LastReboot
          "CurrentRebootTime" = "$CurrentReboot"
        }

        $newRow = New-Object PsObject -Property $hash
        $rnd = Get-Random -Minimum 5 -Maximum 40
        Start-Sleep -Seconds $rnd
        Export-Csv D:\workflow-results.csv -InputObject $newrow -Append -Force
      } catch {
        $errMsg = $_.Exception
        "Failed with $errMsg"
      }#end catch
    }#end inline script
  }#end foreach parallel
}#end workflow

$mylist = gc D:\Servers.txt
Do-ScheduledReboot -servers $mylist
4

1 に答える 1

1

順序付けられたハッシュ テーブルを作成します。

$hash = [ordered]@{
    'Server'            =  $using:server
    'Status'            = ...
    "LastRebootTime"    = ...
    'CurrentRebootTime' = ...
}
于 2016-02-29T01:48:36.820 に答える