skip to Main Content

Am trying to get report of alerts fired in past 14 days from a resource group in Azure through PowerShell, The below code is running but the output of excel is blank.

Get-AzAlert -TargetResourceGroup xxxxxxx | Export-Excel -Path C:UsersnameDownloadsa.xlsx
$filterDate=(Get-Date).AddDays(-14)

Tried the above code, but the output of excel is blank.

2

Answers


  1. Chosen as BEST ANSWER

    Thanks guys, got the output successfully with this query:

    #Get the date for last 14 days with time format
    $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 }
    
    $result | Select-Object StartDateTime, Name, MonitorCondition, Severity, TargetResource | export-csv -path C:UsersxxxxxDownloadsreport.csv
    

  2. To get a PowerShell report of alerts fired in the last 14 days from an Azure resource group:

    Try below script and it worked for me as follows:

    $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

    Output:

    enter image description here

    You can also obtain metrics for a particular time range by providing -CustomTimeRange parameter which includes starttime and endtime and clearly detailed in MSDoc.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search