Search code examples
powershellpowershell-3.0invoke-command

Using Invoke-Command within a function


I have this code snippet. The idea is calling an invoke command on a remote pc, but if the command fails, then it should retry.

The code looks like this:

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

}

I should then be able to call the function as this:

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

But for some reason it does not work as I thought it would.

Thanks in advance

Regards Magnus


Solution

  • I figured it out. For anyone interested, here is the code:

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

    I suspsect the reason for it not working was that the argumentlist was sometimes empty. So I added a check to decide which Invoke-command to run.

    Hope this migth help someone

    /Magnus