質問

このコードスニペットを持っています。アイデアはリモートPC上でinvokeコマンドを呼び出すことですが、コマンドが失敗した場合は再試行する必要があります。

コードは次のように見えます:

Function Run-Command 
{
    param(

        [Parameter(Mandatory = $true)] 
        [Uri[]] $ConnectionUri,

        [Parameter(Mandatory = $true)] 
        [PSCredential] $Credential,

        [Parameter(Mandatory = $true)] 
        [ScriptBlock] $ScriptBlock,

        [Parameter(Mandatory = $true)] 
        [Int] $Time,

        [Parameter(Mandatory = $false)] 
        [Object[]] $ArgumentList

    )

    for($retry = 0; $retry -le 10; $retry++)
    {
      try
      {
        Invoke-Command -ConnectionUri $ConnectionUri -Credential $Credential -ScriptBlock $ScriptBlock -ArgumentList $ArgumentList #-ErrorAction SilentlyContinue           
        if ($?)
        {
          break
        }

        Write-Verbose "Server is not ready yet . . . sleeping and trying again in $Time seconds."
        Start-Sleep -Seconds $Time
      }
      catch
      {
        Write-Verbose "Server is not ready yet . . . sleeping and trying again in $Time seconds."
        Start-Sleep -Seconds $Time
      }
    }

}
.

この関数をこれとして呼び出すことができるはずです。

Run-Command -ConnectionUri $ConnectionUri -Credential $Credential -ScriptBlock $SomeScriptBlock -Time 30
.

しかし何らかの理由で私はそれが思ったときにうまくいかない。

事前感あり

について マグナス

役に立ちましたか?

解決

私はそれを考え出した。興味のある人のために、ここにコードがあります:

Function Run-Command 
{
    param(

        [Parameter(Mandatory = $true)] 
        [Uri[]] $ConnectionUri,

        [Parameter(Mandatory = $true)] 
        [PSCredential] $Credential,

        [Parameter(Mandatory = $true)] 
        [ScriptBlock] $ScriptBlock,

        [Parameter(Mandatory = $true)] 
        [Int] $Time,

        [Parameter(Mandatory = $false)] 
        $ArgumentList

    )

    for($retry = 0; $retry -le 10; $retry++)
    {
      try
      {

        if ($ArgumentList -eq $null)
        {
            Invoke-Command -ConnectionUri $ConnectionUri -Credential $Credential -ScriptBlock $ScriptBlock -ErrorAction SilentlyContinue
        }
        else
        {
            Invoke-Command -ConnectionUri $ConnectionUri -Credential $Credential -ScriptBlock $ScriptBlock -ArgumentList $ArgumentList -ErrorAction SilentlyContinue
        }

        if ($?)
        {
          break
        }

        Write-Verbose "Server is not ready yet . . . sleeping and trying again in $Time seconds."
        Start-Sleep -Seconds $Time
      }
      catch
      {
        Write-Verbose "Server is not ready yet . . . sleeping and trying again in $Time seconds."
        Start-Sleep -Seconds $Time
      }
    }
}
.

私はそれが機能しない理由を疑っています引数リストが時々空のことであるということです。だから私はどの呼び出しコマンドを実行するかを決定するためのチェックを追加しました。

このMIGTHが誰かに助けてくれることを願っています

/ magnus

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top