Get full stack trace from remote session

I use Invoke-Expression in a remote session, and when it throws an exception, it returns only RemoteException without information about the stack trace. Example:

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

If I exclude a redirection error for output ( 2>&1 ), I get the correct error, but it causes an unwanted debug console (from the $ command), which is hidden using redirection.

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

Using Start-Process I see a complete stack trace, but also have an unwanted debug console.

How can I get the full stack trace and the correct exception from the created exception from the remote session? Thanks.

+6
source share
1 answer

If you are performing a remote session, do not use write-host. Try the following:

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

Another option is not to catch the exception, but that the error is propagating to the local session. But I suspect you want to try to recover?

0
source

Source: https://habr.com/ru/post/970632/


All Articles