문제

I am trying to make a list of drive letters but include only the ones which the driveletter and the filesystem are not null. An example of what I have now is

$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 } 
}

this outputs the format correctly, but only checks if the driveletter is null or not, how can I also check against $winvolume.filesystem before listing these drive letters?

Thanks!

도움이 되었습니까?

해결책

If I understand your question correctly, just loop over $winvolume instead, that will give you access to both fields;

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

다른 팁

An alternative would be to only retrieve the volumes matching your criteria in the first place, like:

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

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

Or combined:

$drive = (Get-WmiObject -Class Win32_Volume -Filter "DriveLetter IS NOT NULL AND FileSystem IS NOT NULL" | Select-Object -ExpandProperty DriveLetter) -join ","
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top