仅列出driveletter和filesystem不是$ null的驱动器号

时间:2014-05-03 16:56:10

标签: powershell scripting

我正在尝试列出驱动器号,但仅包括驱动器号和文件系统不为空的驱动器号。我现在拥有的一个例子是

$winvolume = Get-WmiObject -computername $a -class win32_volume | Select-Object -Property driveletter, filesystem, capacity, freespace

foreach ($i in $winvolume.driveletter) { 
    if ($i -ne $null){ $drive = $i + ',' + $drive } 
}

这会正确输出格式,但只检查驱动器是否为空,如何在列出这些驱动器号之前检查$ winvolume.filesystem?

谢谢!

2 个答案:

答案 0 :(得分:0)

如果我理解你的问题,只需循环遍及$ winvolume,即可访问这两个字段;

foreach($i in $winvolume) {
   if ($i.filesystem -ne $null -and $i.driveletter -ne $null) {
      $drive = $i.driveletter + ',' + $drive 
   }
}

答案 1 :(得分:0)

另一种方法是首先只检索符合条件的卷,例如:

$winvolume = Get-WmiObject -Class Win32_Volume -Filter "DriveLetter IS NOT NULL AND FileSystem IS NOT NULL"

$drive = ($winvolume | Select-Object -ExpandProperty DriveLetter) -join ","

或合并:

$drive = (Get-WmiObject -Class Win32_Volume -Filter "DriveLetter IS NOT NULL AND FileSystem IS NOT NULL" | Select-Object -ExpandProperty DriveLetter) -join ","