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

如何修改PowerShell脚本实现NetBackup队列数超50时发送告警邮件?

适配两种NetBackup输出格式的队列告警PowerShell脚本

Hey there! Let's tweak your PowerShell script to handle both NetBackup job summary formats and only fire an alert when queued jobs go over 50. Here's the breakdown and updated code:

核心调整思路

  1. 兼容两种输出格式:我们会先尝试匹配Queued: X的格式,如果没找到,再自动切换到解析表格样式的输出。
  2. 空格问题无需担心:格式一中Queued:后面的大量空格完全不会影响脚本执行——我们用的正则\s+可以匹配任意数量的空白字符(空格、制表符都算),完美适配这种情况。

更新后的完整脚本

$Output = 'D:\test.data\QueuedJobss.txt' 
# 切换到NetBackup admincmd目录(路径无效时直接终止脚本)
Set-Location -Path 'D:\program files\veritas\netbackup\bin\admincmd' -ErrorAction Stop
# 导出作业汇总到文件
.\bpdbjobs -summary -L | Out-File -Path $Output -Encoding ASCII

# 提取队列作业数,同时支持两种输出格式
$queuedCount = $null
# 先尝试匹配格式一:"Queued: [数字]"(自动兼容任意多空格)
$format1Match = Select-String -Path $Output -Pattern '(?<=Queued:\s+)\d+' -ErrorAction SilentlyContinue
if ($format1Match) {
    $queuedCount = [int]$format1Match.Matches.Value
} else {
    # 匹配格式二:表格行,捕获第二个数值(对应QUEUED列)
    $format2Match = Select-String -Path $Output -Pattern '^\w+\s+(\d+)\s+\d+\s+\d+' -ErrorAction SilentlyContinue
    if ($format2Match) {
        $queuedCount = [int]$format2Match.Matches.Groups[1].Value
    }
}

# 仅当队列数大于50时触发告警邮件
if ($queuedCount -and $queuedCount -gt 50) {
    $MailArgs = @{ 
        To          = 'ab2112@test.com' 
        From        = 'netbackup@test.com' 
        Subject     = "⚠️ NetBackup Queued Jobs Alert: $queuedCount Jobs Pending"
        Attachments = $Output 
        Body        = @"
Warning: The number of queued jobs on ustestnbma01 has exceeded the threshold of 50.
Current queued job count: $queuedCount

Please check the NetBackup console immediately to investigate backlogs.
"@
        SmtpServer  = 'smtp.test.worldwide.com' 
    } 
    Send-MailMessage @MailArgs
    Write-Host "Alert email sent successfully - Queued jobs: $queuedCount"
} else {
    $statusMessage = if ($queuedCount) {
        "No alert required - Queued jobs count: $queuedCount"
    } else {
        "Failed to extract queued job count from output file."
    }
    Write-Host $statusMessage
}

关键修改说明

  • 双格式解析逻辑:脚本优先匹配格式一的Queued: X模式,失败则自动切换到解析表格行的第二个数值(对应QUEUED列),确保两种格式都能正常提取数据。
  • 阈值触发机制:只有成功提取到有效数值且数值大于50时才发送邮件,避免无效告警。
  • 优化邮件内容:主题和正文都加入了具体的队列数值,让接收者能快速判断问题严重程度。
  • 错误容错:添加了ErrorAction参数处理路径无效或匹配失败的情况,同时输出清晰的控制台日志方便调试。

内容的提问来源于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:22:23