Skip to content

Instantly share code, notes, and snippets.

@JustinGrote
Last active June 2, 2023 08:31
Show Gist options
  • Select an option

  • Save JustinGrote/6ae6dbacd2c8248dd839f10793708f3a to your computer and use it in GitHub Desktop.

Select an option

Save JustinGrote/6ae6dbacd2c8248dd839f10793708f3a to your computer and use it in GitHub Desktop.
Be Notified when something changes in Azure
using namespace Microsoft.Azure.Commands.Insights.OutputClasses
$ErrorActionPreference = 'Stop'
[PSEventDataNoDetails]$lastEvent = Get-AzActivityLog -MaxRecord 1 -StartTime (Get-Date).AddDays(-89) -WarningAction SilentlyContinue
if (-not $lastEvent) {throw [NotImplementedException]'You have an empty activity log!'}
while ($true) {
Write-Verbose "Waiting for new events..."
while (-not $newEvent) {
Write-Verbose "No new event detected after $($lastEvent.EventTimestamp.ToLocalTime()), waiting for changes..."
Start-Sleep 0.5
[PSEventDataNoDetails[]]$newEvent = Get-AzActivityLog -StartTime $lastEvent.EventTimestamp.ToLocalTime().AddSeconds(1) -WarningAction SilentlyContinue
}
Write-Verbose "New Event(s) Detected at $($newEvent[-1].EventTimestamp.ToLocalTime())"
foreach ($eventItem in ($newEvent | where {!($_.caller -as [guid])})) {
$EventSummary = [PSCustomObject][Ordered]@{
Timestamp = $eventItem.EventTimestamp.ToLocalTime()
User = $eventItem.Caller
Operation = $eventItem.OperationName.LocalizedValue
Target = ($eventItem.ResourceId -replace '/.+/providers/.+?/(.+)','$1')
Status = $eventItem.Status.LocalizedValue
ResourceGroup = $eventItem.ResourceGroupName
}
New-BurntToastNotification -Text $eventSummary.Target,$eventSummary.User,"$($eventSummary.Operation) $($eventSummary.Status)"
}
$lastEvent = $newEvent[-1]
$newEvent = $null
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment