3

For one reason or another I can't seem to get Invoke-Command to produce an error and go the Catch block of my code. I'm probably missing something obvious here, but I can't see it.

I know that the Try block is always executed until a Terminating error is found. By using the switch -ErrorAction Stop, I expect it to become a Terminating error when the server name doesn't exist and move on to the Catch block to report on this error. But it's not really doing that, it just launches the job with Invoke-Commandon the non-existing server. And when not using the switch -AsJob the error is catched. I'm a bit confused on what the best way would be for error handling here..

$Server = "Wrong"

Try {
         if ($Server -eq "UNC") {
             Write-Host "Running Start-Job" -ForegroundColor Cyan
             Start-Job -ScriptBlock { Write-Host "Foo Start-Job" } -Name DelFiles
         }
         else {
              Write-Host "Running Invoke-Command" -ForegroundColor Cyan        
              Invoke-Command -ScriptBlock { Write-Host "Foo Invoke-Command" } -ComputerName "$Server" -AsJob -JobName DelFiles -ErrorAction Stop
         }  
     }
Catch {
    Write-Host "Error detected"
}

Thank you for your help guys, I learned a lot here on StackOverflow. Once I'm getting better at PowerShell I hope to be able to help others out to.

1
  • 1
    -AsJob is going to create a local background job that will maintain the connection to the remote server. I think the error will actually be recorded there, not in the local session. Commented Jun 15, 2014 at 14:19

1 Answer 1

3

as @mjolinor said. The error will be caught in the job, so you will need get-job or receive-job to see if there was an error.

get-job [id] will display the job for you, and you can check the state to see if it failed or completed or is still running, and receive-job [id] will echo back anything that would have normally been printed to stdout

Here is a bit of a test scenario for you I did on my pc This first one I did on a non-existant PC

PS C:\hht> Invoke-Command -ScriptBlock { echo "hey"} -ComputerName test-pc -AsJob
Id     Name            PSJobTypeName   State         HasMoreData     Location             Command                  
--     ----            -------------   -----         -----------     --------             -------                  
22     Job22           RemoteJob       Running       True            test-pc               echo "hey"              

PS C:\hht> Get-Job 22

Id     Name            PSJobTypeName   State         HasMoreData     Location             Command                  
--     ----            -------------   -----         -----------     --------             -------                  
22     Job22           RemoteJob       Failed        False           test-pc               echo "hey"

PS C:\hht> Receive-Job 22

[test-pc] Connecting to remote server test-pc failed with the following error message : WinRM cannot process the request. The following error occurred while using 
Kerberos authentication: Cannot find the computer test-pc. Verify that the computer exists on the network and that the name provided is spelled correctly. For more 
information, see the about_Remote_Troubleshooting Help topic.
+ CategoryInfo          : OpenError: (test-pc:String) [], PSRemotingTransportException
+ FullyQualifiedErrorId : NetworkPathNotFound,PSSessionStateBroken

As you can see it failed, so you can catch this in your main script with something like:

if((get-job $id).state -eq "Failed")
{
    echo "There was an error running job on $server"
}

And here is an example that works:

PS C:\hht> Invoke-Command -ScriptBlock { echo "hey"} -ComputerName test2-pc -AsJob
Id     Name            PSJobTypeName   State         HasMoreData     Location             Command                  
--     ----            -------------   -----         -----------     --------             -------                  
24     Job22           RemoteJob       Running       True            test-pc               echo "hey"  

PS C:\hht> Get-Job 24

Id     Name            PSJobTypeName   State         HasMoreData     Location             Command                  
--     ----            -------------   -----         -----------     --------             -------                  
24     Job24           RemoteJob       Completed     False           test2-pc       echo "hey" 

PS C:\hht> Receive-Job 24
hey

As you can see this job completed successfully and running receive-job returns the output from that job that was run on the remote PC

So in your code, the try-catch block will only catch any errors on the local session, not from the script block run on the remote pc

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you harry sib, this was exactly what I've been looking for. A bit strange that the Invoke-Commanddoesn't generate an error when the client can't be found, but this is indeed due to the -AsJob switch.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.