PowerShell 命令元数据

PowerShell command metadata

我是 PowerShell 新手。我知道当一个人运行 PowerShell 命令时,它会发送一个对象流作为输出。

例如:

PS C:\Users\JGodse\scripts> Get-WmiObject -Class win32_Product


IdentifyingNumber : {90150000-008C-0000-0000-0000000FF1CE}
Name              : Office 15 Click-to-Run Extensibility Component
Vendor            : Microsoft Corporation
Version           : 15.0.4867.1003
Caption           : Office 15 Click-to-Run Extensibility Component

IdentifyingNumber : {90150000-008C-0409-0000-0000000FF1CE}
Name              : Office 15 Click-to-Run Localization Component
Vendor            : Microsoft Corporation
Version           : 15.0.4867.1003 
Caption           : Office 15 Click-to-Run Localization Component

IdentifyingNumber : {90150000-008F-0000-1000-0000000FF1CE}
Name              : Office 15 Click-to-Run Licensing Component
Vendor            : Microsoft Corporation
Version           : 15.0.4867.1003
Caption           : Office 15 Click-to-Run Licensing Component


....... (and many more such objects)......

对象具有属性(IdentifyingNumber、Name、Vendor、Version、Caption)。从这里我可以将对象通过管道传递给这样的东西 select 名称:

PS C:\Users\JGodse\scripts> Get-WmiObject -Class win32_Product | select name

name
----
Office 15 Click-to-Run Extensibility Component
Office 15 Click-to-Run Localization Component
Office 15 Click-to-Run Licensing Component
Microsoft .NET Framework 4.5.1 Multi-Targeting Pack

有没有办法在不使用 运行 命令并直观地解析输出的情况下获取命令返回的对象的属性名称列表?也许像神话般的命令 Get-Attributes:

PS C:\> Get-Attributes Get-WmiObject

attributes
----------
IdentifyingNumber, Name, Vendor, Version, Caption

您正在寻找的 cmdlet 是 get-member,但是它会为您提供对象上可用的属性,而不是 cmdlet 可能产生的属性。这是因为根据您提供的参数,您将返回具有不同结果的对象。 (例如 get-wmiobject returns 不同的 classes 的不同对象)。你会像下面这样使用它。

Get-wmiobject win32_operatingsystem | Get-member

这将为您提供代表计算机 win32_operatingsystem WMI class 的对象可用的所有属性和方法的列表。 Link 下面有更多信息和示例。

https://technet.microsoft.com/en-us/library/ee176854.aspx