Forum Discussion

PeteMitch99's avatar
PeteMitch99
Copper Contributor
Apr 14, 2021

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

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

  • farismalaeb's avatar
    farismalaeb
    Steel Contributor

    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
    
    }

Resources