-ExpandProperty 不通过远程 PowerShell 显示所有属性 [英] -ExpandProperty doesn't show all the properties via Remote PowerShell

查看:75
本文介绍了-ExpandProperty 不通过远程 PowerShell 显示所有属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在 Exchange 服务器上的 Exchange PowerShell 中运行以下代码时,它会显示所有属性:

<前>PS> Get-Mailbox Testeria |选择 -ExpandProperty 电子邮件地址Smtp 地址:Tester_IA@contoso.com地址字符串:Tester_IA@contoso.com代理地址字符串:smtp:Tester_IA@contoso.com前缀:SMTPIsPrimaryAddress : 假前缀字符串:smtpSmtp 地址:TesterIA@contoso.com地址字符串:TesterIA@contoso.comProxyAddressString : SMTP:TesterIA@contoso.com前缀:SMTPIsPrimaryAddress : 真前缀字符串:SMTPSmtp 地址:TesterIA@outlook.contoso.com地址字符串:TesterIA@outlook.contoso.com代理地址字符串:smtp:TesterIA@outlook.contoso.com前缀:SMTPIsPrimaryAddress : 假前缀字符串:smtp

但是当我尝试通过

在本地机器上使用远程PowerShell时

$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri ("http://" + $Server + "/PowerShell/") -Authentication Kerberos导入 PSSession $Session

并运行相同的代码,它只显示这个:

<前>PS> Get-Mailbox Testeria |选择 -ExpandProperty 电子邮件地址smtp:Tester_IA@contoso.comSMTP:TesterIA@contoso.comsmtp:TesterIA@outlook.contoso.com

如何理解这种行为?如何通过远程 PowerShell 获取所有属性?

本地机器上的 PSVersion 是 5.1.14409.1005

Exchange Server 上的 PSVersion 为 4.0

解决方案

这可能是因为当您通过 PSRemoting 访问对象时,结果会被反序列化.您可以通过将结果对象的 TypeName 传送到 Get-Member 来查看它的情况.您将看到反序列化以类型为前缀:

<块引用>

具有反序列化"的对象.类型名称中的前缀是包含 public 的反序列化表示的属性包相应远程活动对象的属性.如你看到的在 Get-Member 的输出中,这些财产包没有暴露任何除了 ToString() 之外的方法,因为通常方法不能被调用在远程会话中(例如 System.Diagnostics.Process.Kill()不能作用于远程进程).同样设置和获取属性属性包的值不执行任何代码(例如的 WorkingSet 属性Deserialized.System.Diagnostics.Process.WorkingSet 只是一个快照并且不会在远程进程使用更多内存时更新).

我的假设是 EmailAddresses 属性是一个脚本属性,这意味着它在被调用以获取其子属性时执行一个脚本.当您通过 Remoting 检索对象时,您将失去执行此脚本的能力.

很遗憾,我目前没有 Exchange 系统来验证这一点.

When I run the following code in Exchange PowerShell on an Exchange server it shows all the properties:

PS> Get-Mailbox Testeria | select -ExpandProperty EmailAddresses

SmtpAddress        : Tester_IA@contoso.com
AddressString      : Tester_IA@contoso.com
ProxyAddressString : smtp:Tester_IA@contoso.com
Prefix             : SMTP
IsPrimaryAddress   : False
PrefixString       : smtp

SmtpAddress        : TesterIA@contoso.com
AddressString      : TesterIA@contoso.com
ProxyAddressString : SMTP:TesterIA@contoso.com
Prefix             : SMTP
IsPrimaryAddress   : True
PrefixString       : SMTP

SmtpAddress        : TesterIA@outlook.contoso.com
AddressString      : TesterIA@outlook.contoso.com
ProxyAddressString : smtp:TesterIA@outlook.contoso.com
Prefix             : SMTP
IsPrimaryAddress   : False
PrefixString       : smtp

But when I try to use Remote PowerShell on the local machine via

$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri ("http://" + $Server + "/PowerShell/") -Authentication Kerberos
Import-PSSession $Session

and run the same code it show only this:

PS> Get-Mailbox Testeria | select -ExpandProperty EmailAddresses

smtp:Tester_IA@contoso.com
SMTP:TesterIA@contoso.com
smtp:TesterIA@outlook.contoso.com

How to understand this behaviour? How to get all the properties via Remote PowerShell?

PSVersion on the local machine is 5.1.14409.1005

PSVersion on the Exchange Server is 4.0

解决方案

This probably occurs because when you access objects via PSRemoting the results are deserialized. You can see this is the case by looking at the TypeName of the resulting object by pipling it to Get-Member. You will see Deserialized prefixed to the Type:

Objects that have the "Deserialized." prefix in their type names are property bags that contain a deserialized representation of public properties of the corresponding remote, live objects. As you can see in the output of Get-Member those property bags don’t expose any methods except ToString(), because usually methods cannot be invoked in the remote session (for example, System.Diagnostics.Process.Kill() can’t act on a remote process). Similarly setting and getting property values of the property bags doesn’t execute any code (for example WorkingSet property of Deserialized.System.Diagnostics.Process.WorkingSet is only a snapshot and doesn’t get updated when the remote process uses more memory).

My assumption is that the EmailAddresses property is a Script Property, which means it executes a script when called to get its sub properties. When you retrieve the object via Remoting you lose the ability to execute this script.

Unfortunately I don't have an Exchange system to verify this on at the moment.

这篇关于-ExpandProperty 不通过远程 PowerShell 显示所有属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆