Azure REST API
Â
Intro
Notes on how to access Azure via its REST API
Documentation
Â
Â
Tips and Tidbits
Â
Get Information About A Disk
Â
Â
Get Activity Logs
Â
#Got the token by running Connect-AzAccount and running the below code. But you can use a better approach
$token = (Get-AzAccessToken).Token
$subscriptionid = "<YourSUBSID>"
#Getting all the disks in your Subscription
$disks = Invoke-RestMethod -Method Get -Uri "https://management.azure.com/subscriptions/$subscriptionid/providers/Microsoft.Compute/disks?api-version=2020-12-01" -Headers @{"Authorization"="Bearer $token"}
#Getting the unattached disks from the above list
$unattacheddisks = $disks.value | ? {$_.managedBy -eq $null }
foreach ($disk in $unattacheddisks)
{
$resourceid = $disk.id
#eventTimestamp is between 2021-05-09 to 2021-06-09 - You can generate a time stamp dynamically for smaller or a larger time span.
$filter = "eventTimestamp ge '2021-05-09T17:59:08Z' and eventTimestamp le '2021-06-09T17:59:08Z' and eventChannels eq 'Admin, Operation' and resourceId eq '$resourceid' and levels eq 'Critical,Error,Warning,Informational'"
#Getting the Activity logs.This is the preview API
$log= Invoke-RestMethod -Uri "https://management.azure.com/subscriptions/ $subscriptionid/providers/microsoft.insights/eventtypes/management/values?api-version=2017-03-01-preview&`$filter=$filter" -Headers @{"Authorization"="Bearer $token"}
#if there is no log returned - no activity
if($log.value.Count -eq 0 )
{
Write-Host $disk.name + " : - There is no activitiy in 30 days" -ForegroundColor Yello
}
else
{
Write-Host $disk.name + " : - There is activitiy in 30 days" -ForegroundColor Green
}
}