In short: Azure’s Activity Log records every delete in your subscription. This script pulls out the successful ones and shows who did it, when, and from which IP address.
Hello all! The real-world scenario: you come in on Monday morning, a VM (or a storage account, or a whole resource group) is gone, and the Teams channel is full of “wasn’t me”. You could click through the Activity Log blade in the portal one filter at a time, or you could run this.
The script
#Requires -Modules Az.Accounts, Az.Monitor
param(
[Parameter(Mandatory)][string]$SubscriptionId,
[int]$DaysBack = 7 # Activity Log keeps 90 days
)
Connect-AzAccount -Subscription $SubscriptionId | Out-Null
Get-AzActivityLog -StartTime (Get-Date).AddDays(-$DaysBack) -MaxRecord 100000 |
ForEach-Object {
# Az.Monitor 5+ uses flat names (OperationNameValue), older versions use OperationName.Value
$op = if ($_.OperationNameValue) { $_.OperationNameValue } else { $_.OperationName.Value }
$status = if ($_.StatusValue) { $_.StatusValue } else { $_.Status.Value }
$ip = if ($_.HttpRequestClientIPAddress) { $_.HttpRequestClientIPAddress } else { $_.HttpRequest.ClientIpAddress }
if ($op -like '*/delete' -and $status -eq 'Succeeded') {
[pscustomobject]@{
Time = $_.EventTimestamp
Caller = $_.Caller
Operation = $op
ResourceGroup = $_.ResourceGroupName
Resource = ($_.ResourceId -split '/')[-1]
ClientIP = $ip
}
}
} |
Sort-Object Time -Descending |
Format-Table -AutoSize
Run it like this:
.\Find-AzureDeletes.ps1 -SubscriptionId "00000000-0000-0000-0000-000000000000" -DaysBack 3
Quick explanation
Get-AzActivityLogreads the subscription’s Activity Log, which records every create, change and delete made through the portal, PowerShell, the CLI or the API.- The filter keeps only operations ending in
/deletethat succeeded, so failed attempts don’t clutter the results. - Caller is the user’s UPN, or an app/managed identity ID if automation did it. That is often the real answer: “it was the cleanup pipeline!”
- ClientIP tells you whether it came from the office, a VPN or somewhere unexpected.
Good to know
- The Activity Log only keeps 90 days. If you need longer (and for audits you do), send it to a Log Analytics workspace using the subscription’s Diagnostic settings.
- Looking for one resource group only? Add
-ResourceGroupName rg-nametoGet-AzActivityLog. - To stop it happening again, put a Delete lock on important resource groups:
New-AzResourceLock -LockName NoDelete -LockLevel CanNotDelete -ResourceGroupName rg-prod
Mystery solved, coffee earned. Hope this helps!
