【问题标题】:Pull Single Item From Array从数组中拉出单个项目
【发布时间】:2014-01-26 06:25:33
【问题描述】:

我正在尝试为从$Win32_OperatingSystem.SystemDrive 变量中提取的系统驱动器提取存储在$Win32_LogicalDisk 中的信息。

$Temp_SystemDrive = $Win32_logicalDisk |
    Select DeviceID, FreeSpace, Size |
    Where-Object (($_.DeviceID) -like ($Win32_OperatingSystem.SystemDrive));
$Temp_SystemDrive

【问题讨论】:

    标签: powershell powershell-2.0


    【解决方案1】:

    Win32_LogicalDiskWin32_OperatingSystem 不是变量,它们是 WMI 类。您需要使用Get-WmiObject 来访问它们。试试这个:

    #Save OS-information early to clean up your code
    $os = (Get-WmiObject Win32_OperatingSystem)
    
    #Save the ORIGINAL object for disk in the case you need more info later
    $Temp_SystemDrive = Get-WmiObject Win32_LogicalDisk | Where-Object { $_.DeviceID -eq $os.SystemDrive }
    
    #Get the properties you need
    $Temp_SystemDrive | Select DeviceID, FreeSpace, Size
    

    【讨论】:

    • Get-WmiObject可以自己做过滤:Get-WmiObject Win32_LogicalDisk -Filter "DeviceID='$($os.SystemDrive)'"
    【解决方案2】:

    为了获得最佳性能,只提取您需要的字段,并在查询级别应用过滤器,而不是之后:

    $os = Get-WmiObject -Query "SELECT SystemDrive FROM Win32_OperatingSystem"
    $wmiQuery = "SELECT DeviceID, FreeSpace, Size " + 
                  "FROM Win32_LogicalDisk " +
                 "WHERE DeviceID='$($os.SystemDrive)'"
    Get-WmiObject -Query $wmiQuery
    

    【讨论】:

      猜你喜欢
      • 2021-10-10
      • 2017-06-23
      • 1970-01-01
      • 2015-10-29
      • 1970-01-01
      • 2019-05-17
      • 2016-06-09
      • 1970-01-01
      • 2022-08-24
      相关资源
      最近更新 更多