Get full stack trace from remote session
Asked Answered
H

1

6

I'm using Invoke-Expression under remote session and when throws exception - it returns just RemoteException without any stack trace information. Example:

try
{
    Invoke-Expression "$command 2>&1"
}    
catch
{
    Write-Host $_    
}

If I exclude redirect error to output(2>&1) - I'm getting proper error but it call unwanted debug console(from $command), which is hidden using redirection.

Start-Process -NoNewWindow -FilePath $CmdExe -ArgumentList $Arguments

Using Start-Process I can see full stack trace but also have unwanted debug console.

How can I get a full stack trace and proper Exception from thrown exception under remote session? Thanks.

Hallett answered 10/6, 2014 at 15:49 Comment(0)
W
0

If you're doing a remote session, don't use write-host. Try this:

catch { 
    Write-Error ($_ | fl * -force | out-string)
}

The other option is not to catch the exception and the let error propagate back to the local session. But I suspect you want to attempt to recover?

Wentz answered 10/6, 2014 at 19:45 Comment(2)
Thanks Keith, no I don't want to recover, I just need to get an error to define true reason. In your example I see full information about remote exception and nothing about the real exception(should be info that xml structure is broken)Hallett
Try changing the fl to fc (Format-Custom). That will follow nested objects (like Exception.InnerException) down something like four levels.Wentz

© 2022 - 2024 — McMap. All rights reserved.