You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Get-WMIObject的If/Else语句异常:输出颜色与变量值不符

Troubleshooting Your PowerShell WMI If/Else Script Issues

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-WMIObject query 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-Error instead of Write-Host/Write-Output for your intended status messages.
  • The older Get-WMIObject cmdlet 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 PredictedFailure instead of PredictFailure?).
  • 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-WMIObject query isn't returning the object you expect, so the property you're checking doesn't exist or has a default $false value.

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

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 -eq for equality (not =—that's for variable assignment)
  • For boolean properties, you can directly check them like if ($object.Property) instead of if ($object.Property -eq $true)

内容的提问来源于stack exchange,提问作者RoadGlide

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 09:08:53