2014-05-03 61 views
0

我想列出驅動器號但只包括驅動器號和文件系統不爲空的驅動器號。什麼,我現在有一個例子是

$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列出這些驅動器號之前?

謝謝!

回答

0

如果我正確理解你的問題,只需循環$ winvolume,而不是讓你訪問這兩個字段;

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

你是絕對正確的。我用這個: foreach($ i in $ winvolume){ if($ i.filesystem -ne $ null -and $ i.driveletter -ne $ null){ $ drive = $ i.driveletter +',' + $驅動器 } } 謝謝! – Pyralix

+0

@Pyralix是的,謝謝,我在構建字符串時錯過了驅動字母部分,用您的實際聲明更新了答案:) –

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