PowerShell大于比较失效求助:仅阈值为0时脚本正常运行
Let’s break down what’s going wrong here and fix it step by step. The core problem is your script isn’t reliably extracting the Queued job count from the output file—so when you set a threshold higher than 0, $Queued ends up as 0 (since $null converts to 0 when cast to [int]), making your condition $Queued -ge 10 evaluate to false.
Why This Happens
Your current regex (?<=Queued:\s+)\d+ should work on paper, but two common pitfalls are breaking it:
- Fragile result extraction:
(Select-String ...).Matches.Valueassumes a single, successful match. IfSelect-Stringfails to find a match (due to encoding quirks, leading spaces in the line, or unexpected whitespace like tabs), this returns$null, which becomes 0 when cast to[int]. - Unaccounted-for output variability: The
bpdbjobsoutput might have leading spaces beforeQueued:or non-space whitespace betweenQueued:and the number, which your regex doesn’t handle consistently.
Fixes You Can Implement
1. Use a Robust Regex with Capture Groups
Instead of relying on positive lookbehind, use a capture group to explicitly target the number. This makes it easier to validate the match and extract the value correctly:
$Output = 'D:\test.data\QueuedJobs.txt' Set-Location -Path 'D:\program files\veritas\netbackup\bin\admincmd' .\bpdbjobs -summary -L > $Output # Extract Queued count with explicit capture group $matchResult = Select-String -Path $Output -Pattern 'Queued:\s+(\d+)' if ($matchResult.Success) { [int]$Queued = $matchResult.Matches.Groups[1].Value } else { # Handle missing match (optional: log error or set default) $Queued = 0 Write-Warning "Failed to extract Queued job count from output file." } # Check threshold and trigger action if ($Queued -ge 10) { "Queued is {0}" -f $Queued # Add your email alert logic here }
2. Avoid Regex Entirely (Simpler Approach)
If regex feels too fragile, split the output string directly to get the number:
$Output = 'D:\test.data\QueuedJobs.txt' Set-Location -Path 'D:\program files\veritas\netbackup\bin\admincmd' .\bpdbjobs -summary -L > $Output # Read the entire file and extract the first numeric value after "Queued:" $content = Get-Content -Path $Output -Raw $queuedCount = ($content -split 'Queued:\s+' | Select-Object -Index 1) -split '\s+' | Select-Object -Index 0 [int]$Queued = $queuedCount if ($Queued -ge 10) { "Queued is {0}" -f $Queued # Add your email alert logic here }
3. Fix File Encoding Issues
If bpdbjobs writes output in a non-default encoding (like UTF-16), Select-String might read it incorrectly. Specify the encoding explicitly:
$matchResult = Select-String -Path $Output -Pattern 'Queued:\s+(\d+)' -Encoding Default
Key Takeaways
- Always validate that your match was successful before casting to
[int]—this avoids silent failures where$nullbecomes 0. - Capture groups are more reliable than lookbehind assertions for extracting specific values from text.
- Account for variability in command output (leading spaces, different whitespace types) to make your script robust.
内容的提问来源于stack exchange,提问作者AB2112

