使用PowerShell获取单个特定驱动器状态?

时间:2018-03-14 20:16:59

标签: powershell

运行此:

$WMI = Get-WMIObject -Class Win32_DiskDrive
ForEach ($Drive in $WMI){
     $Drive.DeviceID + ": " + $Drive.Status
}

返回如下结果:

\\.\PHYSICALDRIVE1: OK
\\.\PHYSICALDRIVE0: OK
\\.\PHYSICALDRIVE2: OK
\\.\PHYSICALDRIVE3: OK

是否有一种简单的方法可以将结果限制为\。\ PHYSICALDRIVE0?

基本上,我试图回复一个简单的" OK"对于物理驱动0的健康。

我尝试了以下内容:

$WMI = Get-WMIObject -Class Win32_DiskDrive
ForEach ($Drive in $WMI | Where $Drive.DeviceID -Contains "\\.\PHYSICALDRIVE0"){
 $Drive.Status
}

但是没有输出(根本没有)。如何将状态输出限制为DeviceID中的物理驱动器0?

2 个答案:

答案 0 :(得分:1)

我会这样做:

$WMI = Get-WMIObject -Class Win32_DiskDrive
ForEach ($Drive in $WMI) {
if ($Drive.DeviceID -contains "\\.\PHYSICALDRIVE0") { 
    $Drive.Status
    }
}

您通过$wmi查看并检查每个值是否包含此特定值。 如果匹配,则会显示磁盘状态。

答案 1 :(得分:0)

有很多方法,具体取决于您希望的具体方式:

Get-WMIObject -Class Win32_DiskDrive | Where-Object DeviceID -eq '\\.\PHYSICALDRIVE0'
Get-WMIObject -Class Win32_DiskDrive | Where-Object DeviceID -match 'E0$'
Get-WMIObject -Class Win32_DiskDrive | Where-Object Index    -eq 0

如果您希望这些版本在旧版本的PowerShell上运行,那么请使用旧的语法,方法是放置每个" test"进入一个scriptblock:

Get-WMIObject -Class Win32_DiskDrive | Where-Object { $_.DeviceID -eq '\\.\PHYSICALDRIVE0' }