Error Handling for Windows Powershell Remote Sessions

  • I am doing some SQL Admin with Powershell. I am using Invoke-Command and a remote session to execute a command on a remote server, like so:

    Invoke-Command `

    -Session $TestServer `

    -ArgumentList $ParticipantSurvey_CopySource `

    ,$ParticipantSurvey_CopyDestination `

    -ScriptBlock `

    { param( $ParticipantSurvey_CopySource `

    ,$ParticipantSurvey_CopyDestination `

    ) `

    Copy-Item `

    -Verbose `

    -Path $ParticipantSurvey_CopySource `

    -Destination $ParticipantSurvey_CopyDestination `

    -ErrorAction Stop `

    -ErrorVariable ParticipantSurvey_CopyError; `

    };

    If the Copy-Item command throws an error, it raises the error within the remote session. Invoke-Command does not throw an error in this case. (As far as Invoke-Command is concerned, it did its job properly.)

    However, I want to capture any errors from the remote session and use them in the outer, calling session as well. For example, if I put this code snippet in a Try-Catch block, I want an error within the remote session to trigger the Catch block in the outer/local/calling session.

    How can I do this?

Viewing post 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply