2017-01-09 10 views
0

tryブロックのコード最後に3つのコマンドを複数回実行してから、catchfinallyブロックを実行します。この5番、6番、7番のラインを置くことができれば、一種の再試行を意味します。 5行目が3回実行され、失敗した場合はcatchfinallyに進みます。 3回第五行をしようとするTRYブロックで複数回コマンドを再試行するには

try { 
    $hostcomputer = hostname 
    $IP = "10.x.x.x" 
    $pso = New-PSSessionOption -SkipCACheck -SkipRevocationCheck -SkipCNCheck:$TRUE -ErrorAction Stop 
    $session = New-PSSession -Authentication Negotiate -ConnectionUri https://mail.test.com/powershell/?ExchClientVer=15.1 -ConfigurationName microsoft.exchange -SessionOption $pso -ErrorAction Stop 
    Import-PSSession $session -AllowClobber -ErrorAction Stop 
} catch { 
    $ErrorMessage = $_.Exception.Message 
    $FailedItem = $Error 
    Send-MailMessage -From [email protected] -To "[email protected]" -Subject "DC2 - RPS Not Working" -SmtpServer smtp.test.net -Body "Error generated on $hostcomputer = $IP. The Error Message was:- $ErrorMessage." 
    $Text = "Connection Failed" 
    # You have to create .csv file manually and name the column as 'DC2' 
    $Text | select @{l='DC2';e={$_}} | Export-Csv D:\DC2.csv -Append 
} finally { 
    $Time=Get-Date 
    if (!$Error) { 
     $Time | select @{l='DC2';e={$_.DateTime}} | Export-Csv D:\DC2.csv -Append 
    } 
} 
+0

ここで「リトライ」機能の非常にエレガントなバージョンがあります:[このような何かリンク](https://blogs.endjin.com/2014/07/how-to-retry-commands-in-powershell/)。私は答えとしてそれを掲示するだろうが、私はその人の仕事のために信用を得たくない。自分の "再試行"関数を書いていたのですが、文字列ではなくスクリプトブロックとしてコードを受け入れ、チェックする独自のテストスクリプトを指定する場合は "成功条件"型パラメータを追加しますそれが成功したかどうか。 –

答えて

0

一つの方法は、以下のような機能するDo Untilを使用することです:どのようにtry..catch作品

Try 
{ 
$hostcomputer = hostname 
$IP = "10.x.x.x" 
$pso = New-PSSessionOption -SkipCACheck -SkipRevocationCheck -SkipCNCheck:$TRUE -ErrorAction Stop 
$session = New-PSSession -Authentication Negotiate -ConnectionUri https://mail.test.com/powershell/?ExchClientVer=15.1 -ConfigurationName microsoft.exchange -SessionOption $pso -ErrorAction Stop 


     [int]$retryCount = 0; 
     Do{ 


     try{ 
      $retryCount++; 
      import-pssession $session -allowclobber -ErrorAction Stop 


     } catch [Exception]{ 

       Write-Warning "Try Number $retryCount" 

       if($retryCount -eq 3){ 
        $_ 
        $_.GetType() 
        $_.Exception 
        $_.Exception.StackTrace 
        throw 

       } 
       } 

     } #End of Do 
     Until($retryCount -eq 3) 


} 
Catch 
{ 
$ErrorMessage = $_.Exception.Message 
$FailedItem = $Error 
Send-MailMessage -From [email protected] -To "[email protected]" -Subject "DC2 - RPS Not Working" -SmtpServer smtp.test.net -Body "Error generated on $hostcomputer = $IP. The Error Message was:- $ErrorMessage." 
$Text = "Connection Failed" 
###You have to create .csv file manually and name the column as 'DC2' 
$Text | select @{l='DC2';e={$_}} | Export-Csv D:\DC2.csv -append 
} 

Finally 
{ 

$Time=Get-Date 
if (!$Error) { 
    $Time | select @{l='DC2';e={$_.DateTime}} | Export-Csv D:\DC2.csv -append 
} 

} 
+0

それまで私の研究室でこれをチェックする。私はまた、$ _ pso _と_ $ _セッションを再試行したいと思っています。_上記は1行だけで動作するかもしれません。_import-pssession _..... – Jeetcu

3

ないことを。そのようなもののためには、コマンドを使ってtry..catchブロックの回りにループを張る必要があります。エラー処理を遅らせて、「最終的に」自分で管理してください。

$attempt = 3 
$success = $false 
while ($attempt -gt 0 -and -not $success) { 
    try { 
    $pso = New-PSSessionOption ... 
    $success = $true 
    } catch { 
    # remember error information 
    $ErrorMessage = $_.Exception.Message 
    $FailedItem = $Error 

    $attempt-- 
    } 
} 

... 

# error processing 
if (-not $success) { 
    $Text = "Connection Failed" 
    Send-MailMessage -From ... 
} else { 
    $Text = Get-Date 
} 

# "finally" 
$Text | select @{l='DC2';e={$_}} | Export-Csv D:\DC2.csv -append 

たぶん、あなたはこのような関数(未テスト)でコマンドを繰り返すためのコードをラップすることができます::

function Repeat-Command { 
    [CmdletBinding()] 
    Param(
    [Parameter(Mandatory=$true)] 
    [scriptblock]$Scriptblock, 

    [Parameter(Mandatory=$false)] 
    [int]$Count = 1 
) 

    Begin { 
    $attempt = $Count 
    $success = $false 
    } 
    Process { 
    while ($attempt -gt 0 -and -not $success) { 
     try { 
     $res = Invoke-Command -ScriptBlock $Scriptblock -ErrorAction Stop 
     $success = $true 
     } catch { 
     $ex = $_ # remember error information 
     $attempt-- 
     } 
    } 
    } 
    End { 
    if ($success) { 
     return ,$res 
    } else { 
     throw $ex 
    } 
    } 
} 

$pso = Repeat-Command -Scriptblock { New-PSSessionOption ... } -Count 3 
... 
+0

これをテストして、あなたにすべて戻ってきます応答で。 – Jeetcu

関連する問題