【问题标题】:How to list all properties of a PowerShell object如何列出 PowerShell 对象的所有属性
【发布时间】:2011-11-07 17:13:52
【问题描述】:

当我查看 Win32_ComputerSystem class 时,它会显示大量属性,例如 StatusPowerManagementCapabilities 等。但是,当我在 PowerShell 中执行以下操作时,我只能返回几个:

PS C:\Windows\System32\drivers> Get-WmiObject -Class "Win32_computersystem"

Domain              : YYY.com
Manufacturer        : VMware, Inc.
Model               : VMware Virtual Platform
Name                : LONINEGFQEF58
PrimaryOwnerName    : Authorised User
TotalPhysicalMemory : 2147016704

如何查看所有属性?

【问题讨论】:

    标签: powershell


    【解决方案1】:

    试试这个:

    Get-WmiObject -Class "Win32_computersystem" | Format-List *
    Get-WmiObject -Class "Win32_computersystem" | Format-List -Property *
    

    对于某些对象,PowerShell 提供了一组可以影响表格或列表格式的格式说明。这些通常旨在将大量属性的显示限制为仅显示基本属性。然而,有时你真的想看到一切。在这些情况下,Format-List * 将显示所有属性。请注意,如果您尝试查看PowerShell错误记录,则需要使用“Format-List * -Force”才能真正看到所有错误信息,例如,

    $error[0] | Format-List * -force
    

    请注意,通配符可以像传统通配符一样使用:

    Get-WmiObject -Class "Win32_computersystem" | Format-List M*
    

    【讨论】:

    • 我更喜欢Get-WmiObject -Class win32_computersystem -Property *。又短又甜
    【解决方案2】:

    如果你想知道有哪些属性(和方法):

    Get-WmiObject -Class "Win32_computersystem" | Get-Member
    

    【讨论】:

    • 我不会因为原始问题的措辞而给你打分。但值得指出的是,Get-Member 并未列出属性及其值,而仅列出了属性/方法名称和类型。
    • 这在编写脚本以了解每个字段包含的数据类型并在不获取数据的情况下列出所有可用字段名称时非常有用。谢谢!
    【解决方案3】:

    你也可以使用:

    Get-WmiObject -Class "Win32_computersystem" | Select *
    

    这将显示与此处其他答案中使用的 Format-List * 相同的结果。

    【讨论】:

    • 这实际上比接受答案中的方法更好,因为使用此方法您仍然拥有丰富的对象,而使用 Format-List 将破坏管道中的所有对象。
    【解决方案4】:

    我喜欢

     Get-WmiObject Win32_computersystem | format-custom *
    

    这似乎扩展了一切。

    PowerShellCookbook 模块中还有一个 show-object 命令,可在 GUI 中执行此操作。 PowerShell 创建者 Jeffrey Snover 在他的不插电视频中使用它(推荐)。

    虽然我经常使用

    Get-WmiObject Win32_computersystem | fl *
    

    它避免了为对象类型定义表或列表视图的 .format.ps1xml 文件(如果有)。格式文件甚至可以定义不匹配任何属性名称的列标题。

    【讨论】:

    • format-custom * 似乎是真的显示一切的答案
    【解决方案5】:

    最简洁的方法是:

    Get-WmiObject -Class win32_computersystem -Property *
    

    【讨论】:

    • 简洁,也许,但不完整。这仅列出了最初看到的 (5) 属性与使用时显示的 83 个属性:Get-WmiObject -Class "Win32_computersystem" |选择 *
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-11
    • 2015-07-21
    • 1970-01-01
    • 2013-07-31
    • 1970-01-01
    相关资源
    最近更新 更多