Share via


Returning -ErrorVariable defined in remote session invoked with invoke-command

Question

Wednesday, November 27, 2013 2:26 PM

Hi fellow PowerShellers,

I'm trying to find a solution on how to get error variables defined in remote sessions, back to the local session. Let's say I do:

Invoke-command -ComputerName testServer -Scriptblock {   Get-process -Name svchost -errorvariable cmdExecutionStatus}

I would like to send the cmdExecutionStatus variable back to the local session. I have tried various stuff like:

- assigning the entire invoke-command to a variable, but that of course only gives me the objects returned from the cmdlet inside the scriptblock, in a deserialized form

- setting $lastexitcode = cmdExecutionStatus and then calling the remote server again to assign a new variable to $lastExitCode

- searched the interwebs in general, this page is great btw - http://powershell.com/cs/media/p/7257.aspx :-D

// Do anyone know how, or if it is possible at all, to get an errorvariable defined in a remote session, back to the local session?

Thank you in advance.

Red Baron

All replies (9)

Thursday, November 28, 2013 4:21 PM âś…Answered

You'd get all of the errors from the entire script block in the local ErrorVariable, as written.  You can keep that performance hit of calling Invoke-Command multiple times to a minimum by using New-PSSession at first, and passing the session object to each call of Invoke-Command.  For example:

$session = New-PSSession -ComputerName testServer

Invoke-command -ErrorVariable cmdExecutionStatus -Session $session -Scriptblock {
    Get-process -Name svchost
}

# When you're done, close the session.
$session | Remove-PSSession 

You could also look into other means of telling which errors came from which commands.  ErrorRecord objects have CategoryInfo.Activity and InvocationInfo properties which can help with that. Try entering this command after you generate an error, and you'll see what I mean; there's a lot of information there for you to use:

$error[0] | Format-Custom * -Force -Depth 1

Wednesday, November 27, 2013 2:32 PM | 1 vote

How about writing the ErrorVariable to the Pipeline 

Invoke-command -ComputerName testServer -Scriptblock {   Get-process -Name svchost -errorvariable cmdExecutionStatus; Write-Output $cmdExecutionStatus}

 Haven't tried this but it might work

EDIT: Tried it and it works am getting the Deserialized.System.Management.Automation.ErrorRecord object back

Knowledge is Power{Shell}.


Wednesday, November 27, 2013 4:41 PM

Did you mean Write-Output?  I don't have a cmdlet called Write-Object.

If you do it that way, you're putting error objects on the output stream, which would require code to separate them on the local computer.  When errors occur in the remote command, they get sent via the Error stream to the local computer already; you can use a local ErrorVariable instead, like this:

Invoke-command -ErrorVariable cmdExecutionStatus -ComputerName testServer -Scriptblock {
    Get-process -Name svchost
}

You should only need to define an ErrorVariable in the remote script block if you intend to actually handle the error there.


Wednesday, November 27, 2013 6:06 PM

Yes David Sir.....meant Write-Output .
Lol I posted this from while at work....too many things in my mind.

Edited my answer

Knowledge is Power{Shell}.


Thursday, November 28, 2013 9:35 AM

Hi Red Baron,

Thanks for your posting.

To get the variable value from a remote session, you can also refer to the method in this forum:

Howto get an variable output from remote pssession

I hope this helps.

We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time.
Thanks for helping make community forums a great place.


Thursday, November 28, 2013 1:37 PM

Hi AnnaWY,

Thank you for your suggestion. I tried that and I cannot see how that gives me the errorvariable. That would give me the output (if any) of the cmdlet being run in the remote session. I'm specifically trying only to get the errorvariable cmdExecutionStatus defined on the cmdlet in the remote session with the -ErrorVariable parameter.

Thank you.

Red Baron


Thursday, November 28, 2013 2:06 PM

Hi DexterPOSH,

It sure Works but I think I agree with David Wyatt. It makes sense to define the -errorvariable on the invoke-command. I should have seen that all along :-)

Thank you, I'll hand you a vote.

Red Baron


Thursday, November 28, 2013 2:10 PM

Hi David,

You got it :-) - It Works. Thank you. Now what if I have several cmdlets running in the remote session? Potentially all of them could fail. Do I get the errors collected on the invoke-command -errorvariable? Or is there a way to define an errorvariable for each cmdlet in the code being invoked on the remote server?

- I guess I for example could split the code I want to run on the remote server, so that I do a call to the remote server per. cmdlet....that would just slow things down a bit.

What do you say? :-D

Red Baron


Thursday, December 5, 2013 8:42 AM

Thank you to you all.

Regards.

Red Baron