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

PowerShell脚本监控NetBackup队列任务报错:无法索引空数组

Fixing "Cannot index into a null array" Error in Your NetBackup Queued Jobs Alert Script

Let's break down why you're hitting that error and fix it step by step:

Root Causes of the Error

  1. Possible Typo: Looking at your error message, it shows you're accessing .Match (singular) instead of .Matches (plural) on the Select-String result. That typo would return $null, leading to the null array index error when trying to access .Groups[1].
  2. No Match Handling: Even if you fix the typo, if Select-String fails to find the "Queued:" line for any reason (e.g., command output changes, permission issues reading the file), .Matches will be $null, triggering the same error.
  3. Fragile Regex: Your current regex works for the sample output, but we can make it more robust to handle extra trailing spaces or minor formatting changes in NetBackup's output.

Fixed Script

Here's the revised version with all issues addressed, plus some efficiency improvements:

# Skip writing to a file - capture command output directly
$netBackupPath = "D:\program files\veritas\netbackup\bin\admincmd"
$jobSummary = & "$netBackupPath\bpdbjobs" -summary -L

# Use a more robust regex and handle potential null matches
$queuedMatch = $jobSummary | Select-String -Pattern "^Queued:\s*(\d+)"
if ($queuedMatch -and $queuedMatch.Matches.Groups[1].Success) {
    $Queued = [int]$queuedMatch.Matches.Groups[1].Value
    if ($Queued -gt 100) {
        $MailArgs = @{
            To          = 'ab2112@test.com'
            From        = 'netbackup@test.com'
            Subject     = 'Over 100 Queued Jobs!'
            Body        = "Current Queued Jobs Count: $Queued`nPlease check the NetBackup environment immediately."
            SmtpServer  = 'smtp.us.test.com'
            # Optional: If you still want to attach the summary, capture it to a file first
            # Attachments = "D:\test.data\QueuedJobss.txt"
        }
        Send-MailMessage @MailArgs
    }
} else {
    # Optional: Add logging or alert if we couldn't parse the queued count
    Write-Warning "Failed to retrieve Queued jobs count from NetBackup output."
}

Key Improvements

  • Avoid File I/O: Instead of writing output to a file and reading it back, we capture bpdbjobs output directly into a variable. This is faster and reduces disk clutter.
  • Robust Regex: The regex ^Queued:\s*(\d+) ensures we only match lines starting with "Queued:", followed by any number of spaces (including none), then capture the numeric value. This handles extra trailing spaces in the line perfectly.
  • Null Safety Checks: We first verify $queuedMatch exists, then check if the group capture was successful before accessing the value. This prevents the null array error entirely.
  • Better Error Handling: Added a warning if we can't parse the queued count, so you know something's wrong instead of the script silently failing.
  • Clearer Email Body: Updated the body to include the actual queued count, making the alert more actionable.

Additional Notes

If you still need to attach the full job summary to the email, you can add back the file write step, but make sure to handle cases where the file might not be created properly (e.g., permission denied).

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 06:21:25