0

I am attempting to use “Get-WMIObject”.

If I run the script it outputs in a red text, not green as it should based on the if/else statement. Furthermore, after the script has run if I type $PredictFailure it shows that $PredictFailure is “False”.

Screenshot to show what is going on.

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}

}
Community
  • 1
  • 1
RoadGlide
  • 5
  • 3

1 Answers1

0

You are comparing with the string "False". Use $FALSE to reference the boolean constant false:

If ($D.PredictFailure -eq $FALSE)

or use negation

If (-not $D.PredictFailure)
krisz
  • 2,686
  • 2
  • 11
  • 18
  • Thank you - I new it had to be an easy fix. Never thought of that because I have nearly identical scripts that work without the Boolean reference. Am 60 years old and just decided to start learning PowerShell last week (it's snowing like heck where I live in Colorado and it's to cold to go skiing so I thought I'd try something new). Thanks again and I will of course accept your answer so you get some points – RoadGlide Feb 19 '18 at 22:29
  • Glad I could help and thank you for the points. So you are new to programming as well? – krisz Feb 19 '18 at 22:55
  • Yeah to PowerShell - I learned "Basic" back in college in the late 70's. First time I opened up a PowerShell console was a little of a week ago. With the help of you and one other coder here I finally finished a script that outputs different foreground colors based on disk free space, health status, potential failure and a few other things. It's been fun and frustrating but thanks to you guys I accomplished what I set out to do. – RoadGlide Feb 20 '18 at 00:00