Powershell 代码获取过去 14 天内触发的警报报告

问题描述 投票:0回答:2

我正在尝试通过 PowerShell 从 Azure 中的资源组获取过去 14 天内触发的警报报告,以下代码正在运行,但 excel 的输出为空白。

Get-AzAlert -TargetResourceGroup xxxxxxx | Export-Excel -Path C:\Users\name\Downloads\a.xlsx
$filterDate=(Get-Date).AddDays(-14)

试过上面的代码,但是excel的输出是空白的

azure powershell alerts
2个回答
0
投票

要从 Azure 资源组获取过去 14 天内触发的警报的 PowerShell 报告:

尝试下面的脚本,它对我有用如下:

$filterDate = (Get-Date).AddDays(-14)
$targetresourceid = "/subscriptions/subscriptionID/resourceGroups/resourceGroupName/providers/Microsoft.Compute/virtualMachines/newvm" //(Get-Azresource -Name <resourcename>).ResourceID
$alerts = Get-AzAlert -TargetResourceId $targetresourceid | Where-Object { $_.Properties.TimeCreated -le $filterDate }
$alerts | Export-Csv -Path "C:\<filename>.csv"

enter image description here

enter image description here

输出:

enter image description here

您还可以通过提供

-CustomTimeRange
参数来获取特定时间范围的指标,该参数包括开始时间和结束时间,并且在 MSDoc 中有详细说明。


0
投票

谢谢大家,通过这个查询成功获得了输出。

#获取最近14天的时间格式 $filterDate = (Get-Date).AddDays(-14).tostring("yyyy/MM/dd hh:mm:ss tt")

#Getting the alerts details with filter 14days from 30 days result $result=Get-AzAlert -TargetResourceGroup xxxxxxxx -TimeRange '30d' | Where-Object { $_.StartDateTime -ge $filterDate }

$结果 |选择对象 StartDateTime、名称、MonitorCondition、严重性、TargetResource |导出 csv -路径 C:\Users\xxxxx\Downloads 出口.csv

© www.soinside.com 2019 - 2024. All rights reserved.