通过 PowerShell 命令获取返回对象的 class - 确定 cmdlet 的输出数据类型

Get class of returned object by PowerShell command - determine a cmdlet's output data type

在PowerShell中,如果一个命令return是一个或多个对象的数组,我可以通过以下方法找出对象的class:

$ab = SampleCommand
$ab[0].getType()

但是,如果命令 return 没有任何内容(0 值数组),我如何找出命令的默认 return 类型?

注意:例如,我正在处理 SCOM PowerShell 命令,我试图找到默认的 return class 命令类型 get-scomscadvisoragent,但它 return 没什么,因为在我的实验室设置中没有配置顾问代理。因此,我无法获得 returned 对象的 class。

如果原则上要确定给定命令输出的对象类型,请使用(Get-Command <cmd>).OutputType;在你的情况下:

(Get-Command get-scomscadvisoragent).OutputType

但是请注意,这仅在目标命令已通过一个或多个 OutputType 属性明确声明其输出类型时才有效。


Cmdlets/高级函数可以选择/另外通过其帮助文本的 .OUTPUTS 部分(在基于注释的帮助中)以不太正式的方式描述它们的输出类型。
因此,如果上述方法无效,您还可以尝试:

(Get-Help get-scomscadvisoragent).returnvalues

后面的信息也可以在
输出的OUTPUTS部分找到 Get-Help -Full <cmd>.