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.

JPBlanc
  • 70,406
  • 17
  • 130
  • 175
Sergey
  • 180
  • 1
  • 9

1 Answers1

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?

Keith Hill
  • 194,368
  • 42
  • 353
  • 369
  • 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) – Sergey Jun 11 '14 at 14:46
  • Try changing the `fl` to `fc` (Format-Custom). That will follow nested objects (like Exception.InnerException) down something like four levels. – Keith Hill Jun 11 '14 at 16:03