从数组中拉出单个项目

时间:2014-01-26 06:25:34

标签: powershell powershell-v2.0

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

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

2 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:1)

要获得最佳性能,请仅提取所需字段,然后在查询级别应用过滤器,而不是在以下情况之后:

$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