通过 PowerShell 命令获取返回对象的类 - 确定 cmdlet 的输出数据类型 [英] Get class of returned object by PowerShell command - determine a cmdlet's output data type
问题描述
在PowerShell中,如果一个命令返回一个包含一个或多个对象的数组,我可以通过以下方法找出对象的类:
In PowerShell, if a command returns an array of one or more objects, I can find out the class of the object by the following method:
$ab = SampleCommand
$ab[0].getType()
但是,如果命令什么都不返回(0 值的数组),我如何找出命令的默认返回类型?
But, how can I find out the default return type of a command if the command returned nothing (array of 0 values)?
注意:例如,我正在处理 SCOM PowerShell 命令,我试图找到命令 get-scomscadvisoragent
的默认返回类类型,但它不返回任何内容,因为顾问代理不是在我的实验室设置中配置.因此,我无法获取返回对象的类.
Note: For example, I am working on SCOM PowerShell commands, and I am trying to find the default return class type of command get-scomscadvisoragent
, but it returns nothing as the advisor agent is not configured in my lab setup. Hence, I am not able to get the class of returned object.
推荐答案
原则上如果要确定给定命令输出的对象类型,请使用 (Get-Command
If you want to determine the type of objects a given command outputs in principle, use (Get-Command <cmd>).OutputType
; in your case:
(Get-Command get-scomscadvisoragent).OutputType
但是请注意,这仅在目标命令通过一个或多个OutputType
属性.
Note, however, that this only works if the target command has explicitly declared its output types via one or more OutputType
attributes.
Cmdlet/高级函数可以通过帮助文本的 .OUTPUTS
部分(在基于注释的帮助中)以不太正式的方式替代/额外地描述其输出类型.
因此,如果上述方法不起作用,您也可以尝试:
Cmdlets / advanced functions may alternatively / additionally describe their output types in a less formal way, via the .OUTPUTS
section of their help text (in comment-based help).
Therefore, if the above didn't work, you can also try:
(Get-Help get-scomscadvisoragent).returnvalues
后面的信息也可以在
输出的OUTPUTS
部分找到Get-Help -Full
.
The latter information can also be found in the OUTPUTS
section of the output from
Get-Help -Full <cmd>
.
这篇关于通过 PowerShell 命令获取返回对象的类 - 确定 cmdlet 的输出数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!