Different $error results in a ISE 5.1 remote session and a local session

%3CLINGO-SUB%20id%3D%22lingo-sub-2272806%22%20slang%3D%22en-US%22%3EDifferent%20%24error%20results%20in%20a%20ISE%205.1%20remote%20session%20and%20a%20local%20session%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2272806%22%20slang%3D%22en-US%22%3E%3CP%3EHi%2C%20I%20am%20getting%20different%20results%20rewriting%20an%20error%20object%20in%20a%20local%20and%20remote%20pssession%20and%20I%20am%20wondering%20if%20anyone%20can%20explain%20why%3F%20Here%20is%20the%20simple%20code%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3EFunction%20PSitemChangeError%20(%24Errorobj%2C%24CustomErrorMessage)%0A%7B%0A%24newline%20%3D%20%22%60r%60n%22%0A%24OrginalExceptionMessage%20%3D%20%24Errorobj.exception.message%0A%24Errorobj.errordetails%20%3D%20%22%5BCustom%20Message%5D%20%24CustomErrorMessage%20%24newline%60%5BOrginal%20Message%5D%20%24OrginalExceptionMessage%22%0Awrite-verbose%20%24Errorobj%20-verbose%0Areturn%20%24Errorobj%0A%7D%0A%0ATry%0A%7B1%2F0%7D%0Acatch%0A%7B%0AThrow%20(PSitemChangeError%20-Errorobj%20%24psitem%20-CustomErrorMessage%20%22Some%20Custom%20Error%22)%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ELocally%20it%20works%20and%20I%20get%3A%3C%2FP%3E%3CP%3E%3CFONT%20color%3D%22%233366FF%22%3EVERBOSE%3A%20%5BCustom%20Message%5D%20Some%20Custom%20Error%20%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%233366FF%22%3E%5BOrginal%20Message%5D%20Attempted%20to%20divide%20by%20zero.%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%23FF0000%22%3E%5BCustom%20Message%5D%20Some%20Custom%20Error%20%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%23FF0000%22%3E%5BOrginal%20Message%5D%20Attempted%20to%20divide%20by%20zero.%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%23FF0000%22%3EAt%20C%3A%5CUsers%5CPete.Mitchell%5CDropbox%20(Take-Two)%5CPete%5CT2%5Czz-Old-Desktop-to-sort%5CChangeErrorFunction%20-%20Works%20local%20does%20not%20work%20on%20remote%20session.ps1%3A11%20char%3A2%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%23FF0000%22%3E%2B%20%7B1%2F0%7D%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%23FF0000%22%3E%2B%20~~~%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%23FF0000%22%3E%2B%20CategoryInfo%20%3A%20NotSpecified%3A%20(%3A)%20%5B%5D%2C%20RuntimeException%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%23FF0000%22%3E%2B%20FullyQualifiedErrorId%20%3A%20RuntimeException%3C%2FFONT%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CFONT%20color%3D%22%23000000%22%3EBut%20on%20a%20remote%20session%20I%20just%20get%20the%20original%20message%20-%20even%20though%20the%20verbose%20shows%20its%20changed%20the%20object.%20Its%20driving%20me%20insane%20so%20any%20suggestions%20appreciated!%3C%2FFONT%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CFONT%20color%3D%22%233366FF%22%3EVERBOSE%3A%20%5BCustom%20Message%5D%20Some%20Custom%20Error%20%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%233366FF%22%3E%5BOrginal%20Message%5D%20Attempted%20to%20divide%20by%20zero.%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%23FF0000%22%3EAttempted%20to%20divide%20by%20zero.%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%23FF0000%22%3E%2B%20CategoryInfo%20%3A%20NotSpecified%3A%20(%3A)%20%5B%5D%2C%20RuntimeException%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20color%3D%22%23FF0000%22%3E%2B%20FullyQualifiedErrorId%20%3A%20RuntimeException%3C%2FFONT%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2272806%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EWindows%20PowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Senior Member

Hi, I am getting different results rewriting an error object in a local and remote pssession and I am wondering if anyone can explain why? Here is the simple code:

 

 

Function PSitemChangeError ($Errorobj,$CustomErrorMessage)
{
$newline = "`r`n"
$OrginalExceptionMessage = $Errorobj.exception.message
$Errorobj.errordetails = "[Custom Message] $CustomErrorMessage $newline`[Orginal Message] $OrginalExceptionMessage"
write-verbose $Errorobj -verbose
return $Errorobj
}

Try
{1/0}
catch
{
Throw (PSitemChangeError -Errorobj $psitem -CustomErrorMessage "Some Custom Error")
}

 

 

Locally it works and I get:

VERBOSE: [Custom Message] Some Custom Error
[Orginal Message] Attempted to divide by zero.
[Custom Message] Some Custom Error
[Orginal Message] Attempted to divide by zero.
At %%\ChangeErrorFunction - Works local does not work on remote session.ps1:11 char:2
+ {1/0}
+ ~~~
+ CategoryInfo : NotSpecified: (:) [], RuntimeException
+ FullyQualifiedErrorId : RuntimeException

 

But on a remote session - Using Enter-PSSession -computername, or ISE > File > 'New Remote Powershell Tab'. I just get the original message - even though the verbose shows its changed the object. Its driving me insane so any suggestions appreciated!

 

VERBOSE: [Custom Message] Some Custom Error
[Orginal Message] Attempted to divide by zero.
Attempted to divide by zero.
+ CategoryInfo : NotSpecified: (:) [], RuntimeException
+ FullyQualifiedErrorId : RuntimeException

1 Reply

@PeteMitch99 

HI

It seems that Throw behavior change when being on a remote or locally.

I did this small change and based on what I see, it's working. can you try and let me know?

Function PSitemChangeError ($Errorobj,$CustomErrorMessage)
{
$newline = "`r`n"
$OrginalExceptionMessage = $Errorobj.exception.message
$Errorobj.errordetails = "[Custom Message] $CustomErrorMessage $newline`[Orginal Message] $OrginalExceptionMessage"
write-verbose $Errorobj -verbose
return $Errorobj
}

Try
{1/0}
catch
{
$MyError=(PSitemChangeError -Errorobj $psitem -CustomErrorMessage "Some Custom Error")
$MyError
Throw $MyError.ErrorDetails

}