如何将Azure Function(PowerShell)的$value转发至Application Insights并发送通知邮件
解决方案
一、将$value数组追踪到Application Insights
前提条件
确保你的Azure Function应用已启用Application Insights集成(可在函数应用的「配置」页查看/设置APPINSIGHTS_CONNECTIONSTRING环境变量)。
1. 用内置日志快速记录
在PowerShell函数中,将$value转为JSON格式后写入信息日志,Application Insights会自动捕获这些日志:
# 将联系人邮箱数组转为JSON,便于后续解析 $contactJson = $value | ConvertTo-Json -Depth 10 # 强制输出日志,确保被Application Insights捕获 Write-Information "High disk usage contact list: $contactJson" -InformationAction Continue
2. 发送结构化自定义事件
如果需要更精准的结构化追踪,可使用Send-AzApplicationInsightsEvent发送自定义事件:
# 导入Application Insights模块(按需执行) Import-Module Az.ApplicationInsights -ErrorAction SilentlyContinue # 从环境变量获取Application Insights连接字符串 $aiConnString = $env:APPINSIGHTS_CONNECTIONSTRING # 发送带上下文的自定义事件 Send-AzApplicationInsightsEvent -ConnectionString $aiConnString ` -EventName "HighDiskUsageContactAlert" ` -Properties @{ ResourceGroupName = $rgName ContactEmails = $contactJson }
3. 在Application Insights中检索追踪数据
打开Application Insights的「日志」页,用以下Kusto查询提取数据:
- 针对信息日志:
traces | where message contains "High disk usage contact list" | extend ContactEmails = parse_json(substring(message, indexof(message, ':') + 2)) | project timestamp, ResourceGroupName = extract('RG: (\\w+)', 1, message), ContactEmails
- 针对自定义事件:
customEvents | where name == "HighDiskUsageContactAlert" | extend ContactEmails = parse_json(tostring(customDimensions.ContactEmails)) | project timestamp, ResourceGroupName = tostring(customDimensions.ResourceGroupName), ContactEmails
二、结合动作组发送邮件+完整追踪方案
修正并完善动作组逻辑
原代码存在变量未定义的问题,以下是动态创建/更新动作组、发送通知并追踪的完整逻辑:
# 遍历目标资源组 foreach ($rgName in $targetResourceGroups) { # 获取当前资源组的所有者邮箱(替换为你的实际获取逻辑) $emails = (Get-AzResourceGroup -Name $rgName).Tags.OwnerEmails -split ',' $value = $emails # 即需要追踪的联系人数组 # 第一步:追踪$value到Application Insights $contactJson = $value | ConvertTo-Json -Depth 10 Write-Information "RG: $rgName - High disk usage contacts: $contactJson" -InformationAction Continue # 第二步:创建/更新资源组专属动作组 $actionGroupName = "DiskCapacityAlert_$rgName" $existingAG = Get-AzActionGroup -ResourceGroupName $rgName -Name $actionGroupName -ErrorAction SilentlyContinue # 构建邮箱接收器列表 $receivers = @() foreach ($email in $emails) { $receivers += New-AzActionGroupReceiver -EmailAddress $email -Name "RGOwner_$($email.Split('@')[0])" } if ($existingAG) { # 更新现有动作组 Set-AzActionGroup -ResourceGroupName $rgName -Name $actionGroupName -ShortName "DiskAlert" -Receiver $receivers } else { # 新建动作组 New-AzActionGroup -ResourceGroupName $rgName -Name $actionGroupName -ShortName "DiskAlert" -Receiver $receivers } # 第三步:触发动作组发送邮件通知 Invoke-AzActionGroup -ResourceGroupName $rgName -Name $actionGroupName ` -Subject "URGENT: High Disk Usage in Resource Group $rgName" ` -Body "Resources in $rgName have exceeded disk capacity thresholds. Contact owners: $($emails -join ', ')" }
权限要求
确保Azure Function的托管身份(系统/用户分配)拥有以下权限:
Microsoft.Insights/actionGroups/write:创建/更新动作组Microsoft.Insights/actionGroups/invoke/action:触发动作组发送通知Microsoft.Insights/components/telemetry/write:向Application Insights发送追踪数据
三、替代方案:Azure Monitor警报规则联动
如果不需要在函数中直接触发邮件,可结合Azure Monitor实现更自动化的流程:
- 用函数定期检测磁盘使用率,将高使用率资源信息写入Azure Monitor自定义日志/指标
- 创建Azure Monitor警报规则,基于自定义日志/指标触发,关联对应资源组的动作组
- 警报触发时自动发送邮件,所有触发记录会自动存入Application Insights,可直接在AI中查看历史数据
内容的提问来源于stack exchange,提问作者Korman
相关产品推荐
相关产品推荐

