If / Else语句使用Get-WMIObject

时间:2018-02-19 21:57:13

标签: powershell if-statement get-wmiobject

我正在尝试使用“Get-WMIObject”。

如果我运行脚本,它会以红色文本输出,而不是绿色,因为它应该基于if / else语句。此外,在脚本运行后,如果我键入$ PredictFailure,则表明$ PredictFailure为“False”。

屏幕截图显示正在发生的事情。

enter image description here

$PredictFailure = Get-WmiObject -namespace root\wmi –class MSStorageDriver_FailurePredictStatus | Select-Object PredictFailure

Foreach ($D in $PredictFailure)

{

$PredictFailure = $D.PredictFailure

    If ($D.PredictFailure -eq "False")

    {Get-WmiObject -namespace root\wmi –class MSStorageDriver_FailurePredictStatus -ComputerName $env:computername | Select-Object PSComputerName, Active, PredictFailure, Reason | Format-Table -AutoSize | Out-String | Write-Host -ForegroundColor Green}

Else

{Get-WmiObject -namespace root\wmi –class MSStorageDriver_FailurePredictStatus -ComputerName $env:computername | Select-Object PSComputerName, Active, PredictFailure, Reason | Format-Table -AutoSize | Out-String | Write-Host -ForegroundColor Red}

}

1 个答案:

答案 0 :(得分:0)

您正在与字符串" False"进行比较。使用$FALSE引用布尔常量false:

If ($D.PredictFailure -eq $FALSE)

或使用否定

If (-not $D.PredictFailure)