Powershell 仅列出驱动器号和文件系统不为$null的驱动器号

Powershell 仅列出驱动器号和文件系统不为$null的驱动器号,powershell,scripting,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){ $driv

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

$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 } 
}
这将正确输出格式,但仅检查驱动器号是否为null,在列出这些驱动器号之前,如何还检查$winvolume.filesystem


谢谢

如果我正确理解了您的问题,只需循环$winvolume即可,这样您就可以访问这两个字段

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

另一种方法是首先只检索与您的条件匹配的卷,如:

$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 ","

你完全正确。我使用了这个:foreach($winvolume中的I){if($I.filesystem-ne$null-和$I.driveletter-ne$null){$drive=$I.driveletter+','+$drive}}谢谢@Pyralix是的,谢谢,我在构建字符串时错过了driveletter部分,用您的实际陈述更新了答案:)