【问题标题】:How to select an object as array in PowerShell?如何在 PowerShell 中选择对象作为数组?
【发布时间】:2020-05-28 04:18:36
【问题描述】:

我有一个来自 Win32_SystemDriver 输出的文件。我需要选择一个对象。 我用特定字符串映射一个对象,然后选择对象属性。 这是我拥有的文件内容(我删除了一些信息)。

PSComputerName          : MININT-BTE68D2
__GENUS                 : 2
__CLASS                 : Win32_SystemDriver
__SUPERCLASS            : Win32_BaseService
__DYNASTY               : CIM_ManagedSystemElement
__RELPATH               : Win32_SystemDriver.Name="1394ohci"
__PROPERTY_COUNT        : 22
__DERIVATION            : {Win32_BaseService, CIM_Service, CIM_LogicalElement, 
                          CIM_ManagedSystemElement}
__SERVER                : MININT-BTE68D2
__NAMESPACE             : root\cimv2
__PATH                  : \\MININT-BTE68D2\root
DisplayName             : 1394 OHCI Compliant Host Controller
version                 : 10X.0.17763.1 (WinBuild.160101.0800)

PSComputerName          : MININT-BTE68D2
__GENUS                 : 2
__CLASS                 : Win32_SystemDriver
__SUPERCLASS            : Win32_BaseService
__DYNASTY               : CIM_ManagedSystemElement
__RELPATH               : Win32_SystemDriver.Name="1394ohci"
__PROPERTY_COUNT        : 22
__DERIVATION            : {Win32_BaseService, CIM_Service, CIM_LogicalElement, 
                          CIM_ManagedSystemElement}
__SERVER                : MININT-BTE68D2
__NAMESPACE             : root\cimv2
__PATH                  : \\MININT-BTE68D2\root
DisplayName             : 1313 AHCI Compliant Host Controller
version                 : 10A.5.1111.1 (WinBuild.160101.0800)

PSComputerName          : MININT-BTE68D2
__GENUS                 : 2
__CLASS                 : Win32_SystemDriver
__SUPERCLASS            : Win32_BaseService
__DYNASTY               : CIM_ManagedSystemElement
__RELPATH               : Win32_SystemDriver.Name="3ware"
__PROPERTY_COUNT        : 22
__DERIVATION            : {Win32_BaseService, CIM_Service, CIM_LogicalElement, 
DisplayName             : 3ware
version                 : 5.01.00.051

我用字符串10X.映射版本,然后我需要选择一个对象DisplayName。我试过了,但它没有返回任何输出。 我的期望,我可以得到"DisplayNamewhich is1394 OHCI Compliant Host Controller`的对象

我尝试过这种方式:

$Path = "D:\driver.txt"
$Read = Get-Content $Path | Where-Object {$_ -like "*10X.*"} | Select-Object DisplayName
$Read

任何人都可以提供想法。谢谢

【问题讨论】:

  • 当您从文件中读取时,get-content 返回一个字符串类型的对象(或本例中的字符串集合)。您需要使用该字符串,您没有要返回的属性 Displayname!您应该直接使用 Win32_SystemDriver 的输出,或将其导出为 CSV / XML,这样您就可以保留对象属性!
  • 另外,这个属性版本是从哪里出现的?在我的测试中,Get-WMIObject 或 Get-CIMInstance 都不返回版本号 ...

标签: arrays powershell object


【解决方案1】:
Get-wmiobjcet -class win32_pnpentity | where-object {$_.version -like "*10X*"} | select-object DisplayName

【讨论】:

    【解决方案2】:

    我无权访问 wmi 对象。不知道为什么并尝试了 TEXT 方法。 我会这样做:

    Get-Content <infile> | select-string '[^v]10X.*' -AllMatches | %{ $_.Matches } | %{ $_.Value}
    
    10X.0.17763.1 (WinBuild.160101.0800)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-03
      • 1970-01-01
      • 2021-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-27
      相关资源
      最近更新 更多