简体   繁体   English

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

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

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: 在PowerShell中,如果命令返回一个包含一个或多个对象的数组,则可以通过以下方法找出对象的类:

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

But, how can I find out the default return type of a command if the command returned nothing (array of 0 values)? 但是,如果命令什么都不返回(值为0的数组),那么如何找出命令的默认返回类型?

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. 注意:例如,我正在使用SCOM PowerShell命令,并且试图找到命令get-scomscadvisoragent的默认返回类类型,但是它没有返回任何内容,因为在我的实验室设置中未配置顾问程序代理。 Hence, I am not able to get the class of returned object. 因此,我无法获得返回对象的类。

If you want to determine the type of objects a given command outputs in principle, use (Get-Command <cmd>).OutputType ; 如果要确定给定命令原则上输出的对象类型,请使用(Get-Command <cmd>).OutputType ; in your case: 在您的情况下:

(Get-Command get-scomscadvisoragent).OutputType

Note, however, that this only works if the target command has explicitly declared its output types via one or more OutputType attributes. 但是请注意,这仅在目标命令已通过一个或多个OutputType属性显式声明其输出类型时才有效。


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). Cmdlet /高级功能可以通过其帮助文本的.OUTPUTS部分(基于注释的帮助)以一种不太正式的方式来替代/附加地描述其输出类型。
Therefore, if the above didn't work, you can also try: 因此,如果上述方法无效,您也可以尝试:

(Get-Help get-scomscadvisoragent).returnvalues

The latter information can also be found in the OUTPUTS section of the output from 后面的信息也可以在以下命令的OUTPUTS部分中找到:
Get-Help -Full <cmd> . Get-Help -Full <cmd>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM