Powershell 重写错误消息-为什么在远程会话中运行PS5.1代码时会产生不同的$psitem结果?

Powershell 重写错误消息-为什么在远程会话中运行PS5.1代码时会产生不同的$psitem结果?,powershell,Powershell,我试图在Powershell中通过添加errorrecord属性来重写毫无帮助的$psitem错误消息,它可以在本地工作。然而,当在远程会话中运行时(使用enter-pssession),我得到了一个不同的结果,我不明白为什么或者我是否可以做些什么来解决它 基本上,$psitem在本地运行时返回更改后的错误,但在远程运行时返回原始错误。虽然如果我将$psitem远程输出到verbose,它会显示为已更改 我只是用get Aduser作为下面的一个例子,它并不重要 以下是有效的代码(本地): 下面

我试图在Powershell中通过添加errorrecord属性来重写毫无帮助的$psitem错误消息,它可以在本地工作。然而,当在远程会话中运行时(使用enter-pssession),我得到了一个不同的结果,我不明白为什么或者我是否可以做些什么来解决它

基本上,$psitem在本地运行时返回更改后的错误,但在远程运行时返回原始错误。虽然如果我将$psitem远程输出到verbose,它会显示为已更改

我只是用get Aduser作为下面的一个例子,它并不重要

以下是有效的代码(本地):

下面是返回不同结果的代码-您需要一台联网的计算机通过enter pssession连接到。您也可以运行与上面相同的代码,但在ISE中使用“文件>新Powershell远程选项卡”:

#########################
""
"---------------------"
"DO THIS WITH A REMOTE CONNECTION"

#Change PC to the name of your remote pc
$RemotePC = "PC"
$cred = get-credential

Enter-PSSession -ComputerName $remotePC -Credential $cred

#Below is same code as above:

# Demo of error rewrite issue - please not the command is unimportant - this issue i am demostrating is the different error handling
# Works on local machine but not in remote session (using enter-pssession). 

$upn = "emailaddress@domain.com"

# will error
Try{
get-aduser -Identity $upn
}

#I know that if I replace the error details part of an exception it should overwrite the exception message. And it does - locally.
Catch
{
"This is the error before change"
write-verbose $psitem -Verbose

#Add errordetails to the PSobject
$psitem.errordetails = "Some error has occurred using getaduser to query $upn"

"This is the error after change"
write-verbose $psitem -Verbose

"This is the error details - should point back to original error with changed error"
Throw ($psitem)
}
任何帮助都将不胜感激


结果-本地:

*This is the error before change*
VERBOSE: Cannot find an object with identity: 'emailaddress@domain.com' under: 'DC=adomain,DC=domain,DC=corp'.
*This is the error after change*
VERBOSE: Some error has occurred using getaduser to query emailaddress@domain.com
*This is the error details - should point back to original error with changed error*
get-aduser : Some error has occurred using getaduser to query emailaddress@domain.com
At line:7 char:1
+ get-aduser -Identity $upn
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (emailaddress@domain.com:ADUser) [Get-ADUser], ADIdentityNotFoundException
    + FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException,Microsoft.ActiveDirectory.Management.Commands.GetADUser
远程结果:

*This is the error before change*
VERBOSE: Unable to contact the server. This may be because this server does not exist, it is currently down, or it does not have the Active Directory Web Services running.
*This is the error after change*
VERBOSE: Some error has occurred using getaduser to query emailaddress@domain.com
*This is the error details - should point back to original error with changed error*
Unable to contact the server. This may be because this server does not exist, it is currently down, or it does not have the Active Directory 
Web Services running.
    + CategoryInfo          : ResourceUnavailable: (emailaddress@domain.com:ADUser) [Get-ADUser], ADServerDownException
    + FullyQualifiedErrorId : ActiveDirectoryServer:0,Microsoft.ActiveDirectory.Management.Commands.GetADUser
*This is the error before change*
VERBOSE: Unable to contact the server. This may be because this server does not exist, it is currently down, or it does not have the Active Directory Web Services running.
*This is the error after change*
VERBOSE: Some error has occurred using getaduser to query emailaddress@domain.com
*This is the error details - should point back to original error with changed error*
Unable to contact the server. This may be because this server does not exist, it is currently down, or it does not have the Active Directory 
Web Services running.
    + CategoryInfo          : ResourceUnavailable: (emailaddress@domain.com:ADUser) [Get-ADUser], ADServerDownException
    + FullyQualifiedErrorId : ActiveDirectoryServer:0,Microsoft.ActiveDirectory.Management.Commands.GetADUser