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

如何在PowerShell代码中嵌入邮件发送逻辑并解决summary空值问题

PowerShell邮件发送问题及修复方案

问题说明

在现有PowerShell告警代码中嵌入邮件发送逻辑时,遇到两个问题:

  • 尝试用$JSON.Summary引用JSON中的summary字段作为邮件主题,运行时报错“summary cannot be null”
  • 不确定把邮件发送的$MailSplat代码块放在Invoke-WebRequest语句之后的位置是否正确

原有核心代码:

$VIPResults = Get-NSVIPAlertingStatus

if ($null -ne $VIPResults)
{
    foreach ($R in ($VIPResults | Where-Object Status -notmatch "Up|Out Of Service"))
    {
        if ($R.status -eq "Down")
        {
            $Stauts = $R.Status
            $Severity = "5"
        }
        Elseif ($R.status -eq "Resolved")
        {   
            $Stauts = $R.Status
            $Severity = "0"
        }

        Write-Verbose "$(Get-Date): Send Alert: Netscaler $($R.name) on $($R.netscalername) is $($R.Status)" -Verbose
        $JSON = @"
        {
            "summary": "Netscaler $($R.name) on $($R.netscalername) is $($R.Status)",
            "node": "$($R.netscalername)",
            "team": "",
            "severity": $Severity,
            "sop_link": "",
            "category": "Netscaler component $($R.name)",
            "integration": "" 
        }
"@
        
        Write-Verbose "$(Get-Date):se" -Verbose
        $Json
        Invoke-WebRequest -Uri http:\\test.com -Method Post -Body \$Json -ContentType application/json -Verbose
    }
}

尝试添加的邮件发送代码:

Write-Verbose "$(Get-Date):se" -Verbose
    $Json
  Invoke-WebRequest -Uri http:\\test.com -Method Post -Body \$Json -ContentType application/json -Verbose

  $MailSplat = @{
        To         = "test@.com"
        From       = "no-reply@.com"
        Subject    = $JSON.Summary
        Body       = $JSON
        SmtpServer = "hub.test.com"
        BodyAsHtml = $true
    }

    Send-MailMessage @MailSplat
}}

修复方案

1. 解决“summary cannot be null”错误

当前$JSON是字符串类型,不是PowerShell对象,无法直接通过.访问属性。需要先将JSON字符串转换为PS对象:

  • 在生成$JSON字符串后,添加转换代码:$JsonObj = $JSON | ConvertFrom-Json
  • 邮件主题改用$JsonObj.summary引用字段

2. 确认代码位置

$MailSplat放在Invoke-WebRequest之后是完全正确的,只要处于foreach循环内部,就能正常访问到$JSON及转换后的$JsonObj变量。注意删除添加代码末尾多余的}}。

3. 其他细节修正

  • 邮件地址修正:test@.com改为test@test.com,no-reply@.com改为no-reply@test.com
  • Invoke-WebRequest参数修正:http:\\test.com改为http://test.com,Body参数的\$Json去掉反斜杠,改为$Json

修正后的完整代码

$VIPResults = Get-NSVIPAlertingStatus

if ($null -ne $VIPResults)
{
    foreach ($R in ($VIPResults | Where-Object Status -notmatch "Up|Out Of Service"))
    {
        if ($R.status -eq "Down")
        {
            $Status = $R.Status
            $Severity = "5"
        }
        Elseif ($R.status -eq "Resolved")
        {   
            $Status = $R.Status
            $Severity = "0"
        }

        Write-Verbose "$(Get-Date): Send Alert: Netscaler $($R.name) on $($R.netscalername) is $($R.Status)" -Verbose
        $JSON = @"
        {
            "summary": "Netscaler $($R.name) on $($R.netscalername) is $($R.Status)",
            "node": "$($R.netscalername)",
            "team": "",
            "severity": $Severity,
            "sop_link": "",
            "category": "Netscaler component $($R.name)",
            "integration": "" 
        }
"@
        
        Write-Verbose "$(Get-Date):se" -Verbose
        $Json
        Invoke-WebRequest -Uri http://test.com -Method Post -Body $Json -ContentType application/json -Verbose

        # 转换JSON字符串为PowerShell对象
        $JsonObj = $JSON | ConvertFrom-Json

        # 邮件发送配置
        $MailSplat = @{
            To         = "test@test.com"
            From       = "no-reply@test.com"
            Subject    = $JsonObj.summary
            Body       = $JSON
            SmtpServer = "hub.test.com"
            BodyAsHtml = $true
        }

        Send-MailMessage @MailSplat
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.16 09:33:57