문제

이 코드 스 니펫이 있습니다.이 아이디어는 원격 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
.

그러나 어떤 이유로, 내가 생각할 때 그것이 효과가 없을 것입니다.

미리 감사드립니다

조사 Magnus

도움이 되었습니까?

해결책

나는 그것을 알아 냈다.관심있는 사람을 위해 다음 코드가 있습니다.

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
      }
    }
}
.

나는 그것이 작동하지 않는 이유를 주한다. 인수 목록이 때때로 비워 졌다는 것이 었습니다.그래서 어떤 invoke-command-vounce를 결정하는지 확인하십시오.

이 migth가 누군가를 돕기를 바랍니다

/ magnus

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top