使用Get-WMIObject的If/Else语句异常:输出颜色与变量值不符
Hey there! Let's work through the two problems you're facing: the unexpected red output and the $PredictFailure variable staying False.
Why is your output showing up red?
Red text in PowerShell almost always means the output is being sent to the error stream instead of the standard output stream. Here are the most likely causes:
- Your
Get-WMIObjectquery is returning a warning or error (like no matching WMI class/instances found), which PowerShell displays in red by default. - You might be accidentally using
Write-Errorinstead ofWrite-Host/Write-Outputfor your intended status messages. - The older
Get-WMIObjectcmdlet can be finicky with error handling compared to its modern replacement,Get-CimInstance.
Why is $PredictFailure stuck as False?
This tells us your If condition never evaluates to $true. Common reasons for this:
- You're referencing the wrong property name (double-check the WMI class's actual property—maybe it's
PredictedFailureinstead ofPredictFailure?). - Your comparison logic is off (e.g., using
=instead of PowerShell's equality operator-eq, or checking for a value that doesn't match the actual WMI result). - The
Get-WMIObjectquery isn't returning the object you expect, so the property you're checking doesn't exist or has a default$falsevalue.
Step-by-Step Fixes
1. Verify Your WMI Query Results First
Before adjusting the If/Else logic, confirm what data you're actually pulling from WMI. Run this to inspect the full output:
# Replace with your actual Get-WMIObject command $wmiOutput = Get-WMIObject -Class [YourWmiClass] -Filter [YourFilter] # View all properties of the returned object to check values $wmiOutput | Select-Object *
Check the property you're using in your condition (like PredictFailure)—does it exist? What value does it hold? This will immediately tell you if your condition is targeting the right thing.
2. Fix the Output Color Issue
To ensure your intended messages show up green, use Write-Host with the -ForegroundColor parameter (this directly writes to the console with your chosen color):
if ($wmiOutput.PredictFailure) { Write-Host "Failure predicted!" -ForegroundColor Green $PredictFailure = $true } else { Write-Host "No failure detected." -ForegroundColor Green $PredictFailure = $false }
If red error messages still pop up from the WMI query itself, suppress them (or handle them properly) with the -ErrorAction parameter:
$wmiOutput = Get-WMIObject -Class [YourWmiClass] -Filter [YourFilter] -ErrorAction SilentlyContinue
3. Update to Modern CIM Cmdlets (Recommended)
Get-WMIObject is deprecated—switch to Get-CimInstance for better reliability and error handling:
$cimOutput = Get-CimInstance -Class [YourWmiClass] -Filter [YourFilter] # Use the same If/Else logic with $cimOutput instead
4. Double-Check Your Condition Logic
Make sure you're using PowerShell's correct comparison operators:
- Use
-eqfor equality (not=—that's for variable assignment) - For boolean properties, you can directly check them like
if ($object.Property)instead ofif ($object.Property -eq $true)
内容的提问来源于stack exchange,提问作者RoadGlide

