且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

本地语句输出与调用命令输出非常不同

更新时间:2022-11-08 21:12:13

在您的特定情况下枚举值的实例(System.Enum-派生类型):

In your specific case of an instance of an enum value (an instance of a System.Enum-derived type):

  • 使用[int] $hello 获取原始枚举的数值(System.Enum-衍生)值,没有额外的 NoteProperty 成员,例如远程处理基础结构添加的 PSComputerName(见下文).

  • Use [int] $hello to get the numeric value of the original, enum (System.Enum-derived) value, without the extra NoteProperty members such as PSComputerName that the remoting infrastructure adds (see below).

使用 $hello.Value 来获取枚举值的 string 表示(它的符号 name 而不是它的 >编号).

Use $hello.Value to get the string representation of the enum value (its symbolic name rather than its number).

如果您知道原始的 System.Enum 派生类型,并且该类型在您的本地会话中也可用,您可以将反序列化的对象转换回到其原始类型;例如:
[Microsoft.Foo.Bar.ClusterRole] $hello

If you know the original System.Enum-derived type, and that type is also available in your local session, you can cast the deserialized object back to its original type; e.g.:
[Microsoft.Foo.Bar.ClusterRole] $hello

$hello 在技术上是一个 [int],但是用额外的属性修饰,并且关于 原始 类型的信息记录在隐藏的 .pstypenames 数组,它反映了原始类型的继承层次结构,类型名称以 Deserialized. 为前缀;例如Deserialized.Microsoft.Foo.Bar.ClusterRole;PowerShell 的输出格式化系统会导致通过(隐式应用)Format-Table 对此类对象进行格式化,在这种情况下,它显示了所有实际的[int] 值 - 仅显示 NoteProperty 成员.

$hello is technically an [int], but decorated with extra properties, and information about the original type recorded in the hidden .pstypenames array, which reflects the original type's inheritance hierarchy with the type names prefixed with Deserialized.; e.g. Deserialized.Microsoft.Foo.Bar.ClusterRole; PowerShell's output formatting system causes such an object to be formatted via (implicitly applied) Format-Table, which in this case shows everything but the actual [int] value - only the NoteProperty members are shown.

通常,您可以排除不需要的属性,如下所示:

  • 对于反序列化类型忠实的对象,包括字符串.NET基本类型(例如[int][long]、...) 加上 更多(请参阅 MS-PSRP,PowerShell 远程处理协议规范),您可以访问 $hello.psobject.BaseObject 获取没有任何 NoteProperty 成员的底层对象.

  • For objects of types that deserialize type-faithfully, which includes strings and .NET primitive types (such as [int], [long], ...) plus a few more (see MS-PSRP, the PowerShell Remoting Protocol specification), you can access $hello.psobject.BaseObject to get the underlying object without any NoteProperty members.

对于其他人,您可以创建一个 new 对象(类型总是 [pscustomobject]),通过管道传输到 Select-Object> 将不需要的属性排除,正如 Lee Dailey 所建议的那样):

For others, you can create a new object (invariably of type [pscustomobject]), by piping to Select-Object with the unwanted properties excluded, as suggested by Lee Dailey):

  • $hello |Select-Object * -Exclude PSComputerName, PSShowComputerName, RunspaceId

或者,您可以专注于选择您确实想要的属性.

Alternatively, you can focus on selecting just the properties you do want.

继续阅读,了解为什么需要这样做.

Read on for why that is necessary.

PowerShell 的远程处理基础结构将以下属性添加到从远程调用返回的每个 对象中,作为 NoteProperty 成员:

PowerShell's remoting infrastructure adds the following properties to every object returned from a remote invocation, as NoteProperty members:

  • PSComputerName ...远程计算机的名称

  • PSComputerName ... the name of the remote computer

RunspaceId ... 执行远程命令的运行空间的 ID.

RunspaceId ... the ID of the runspace in which the remote command executed.

PSShowComputerName ... 一个 隐藏 属性,当在通过 Invoke 返回的所有对象上设置为 $true 时-Command-HideComputerName 开关,在默认输出中抑制 PSComputerName 属性的显示(但该属性仍然存在);如果您将远程接收到的对象通过管道传输到 Get-Member -Force,则您只能看到 PSShowComputerName 本身.

PSShowComputerName ... a hidden property that, when set to $true on all objects returned via Invoke-Command's -HideComputerName switch, suppresses display of the PSComputerName property in the default output (but the property is still there); you can only see the PSShowComputerName itself if you pipe a remotely received object to Get-Member -Force.

此外,System.Enum-派生类型,作为 [int] 实例返回,一个 [string]-typed Value 属性 NoteProperty 成员被添加,包含枚举值的符号名称.

Additionally, System.Enum-derived types, which are returned as [int] instances, a [string]-typed Value property NoteProperty member is added that contains the enum value's symbolic name.

PSComputerNameRunspaceId 属性可用于同时针对 多台 计算机的远程命令:假设接收输出的顺序不能保证,这些属性会告诉您给定输出对象的来源.

The PSComputerName and RunspaceId properties are useful in remoting commands that target multiple computers at once: given that the order in which output is received is not guaranteed, these properties tell you where a given output object originated from.

PSShowComputerName 属性允许您控制默认的显示行为 - 但奇怪的是,它对是否显示 RunspaceId 没有影响.

The PSShowComputerName property allows you to control default display behavior - though, curiously, it has no effect on whether RunspaceId is displayed.

System.Enum 派生类型的 Value 属性补偿了通常发生在远程命令(和后台作业)中的类型保真度损失 - 只有有限的一组已知类型的反序列化类型保真度 - 请参阅此答案.

The Value property for System.Enum-derived types compensates for the loss in type fidelity that typically occurs in remoting commands (and background jobs) - only a limited set of known types deserialize with type fidelity - see this answer.

虽然这些属性总是存在,但它们是否默认显示取决于返回对象的特定类型以及与它们关联或默认应用的格式数据通过 PowerShell.

While these properties always exist, whether they show by default depends on the specific types of the object returned and either what formatting data is associated with them or applied by default by PowerShell.

此外,它们可能会在您明确地通过管道传输到 Format-* cmdlet 时以及在序列化期间显示,例如使用 ConvertTo-Json.

Also, they may show when you pipe to Format-* cmdlets explicitly, and during serialization, such as with ConvertTo-Json.