In my presales role at AHAED, I focus on the "art of the possible" in customer conversations. We often analyze customers' cloud storage footprints during a FinOps campaign, noting that "lift and shift" usually places on-premises servers on premium storage, regardless of environment. Additionally, customers often lose track of aging snapshots and lack insight into their overall costs.
I developed a quick PowerShell exploratory script for Azure to help make informed decisions very quickly! As I usually outline, these may not feel super sophisticated to gurus in this space. Know I mostly build scripts or queries to grab information quickly (time is of the essence in my role). If you're a PoSH guru, feel free to take these as "starter" scripts/queries and expand to your heart's content! If you do get really sophisticated and savvy, please share what you come up with!
Let's outline the ask, first and foremost. I had a customer who needed to determine what Managed Disks were deployed, the type, the size, what VM they were attached to, and if the disks had aging snapshots. In theory that should be straightforward, right? Wrong. Due to the way this information is captured via the ARM API endpoints, I had iterate through each subscription using a few different PowerShell cmdlets from the Az module: Get-AzVM, Get-AzDisk, and Get-AzSnapshot. Given how helpful this proved, I wanted to share this here in case you are ever tasked with a similar ask and feel confident or comfortable with PowerShell. Note, you can also take a peek at my code on GitHub as well: AzAndPoSHOperations/PoSH/getDiskandSnapshotInfo.ps1 at master · sbkuehn/AzAndPoSHOperations
# Connect to Azure
Connect-AzAccount
# Define the path for the CSV file
$path = Read-Host -Prompt "Please Provide a file path."
# Initialize an array to store combined VM, disk, and snapshot details
$details = @()
# Get all subscriptions in the Azure tenant
$subscriptions = Get-AzSubscription
# Loop through each subscription
foreach ($subscription in $subscriptions) {
# Select the subscription
Select-AzSubscription -SubscriptionId $subscription.Id
# Get all VMs in the subscription
$vms = Get-AzVM
# Loop through each VM
foreach ($vm in $vms) {
$vmName = $vm.Name
$resourceGroup = $vm.ResourceGroupName
# Get all managed disks attached to the VM
$disks = Get-AzDisk -ResourceGroupName $resourceGroup | Where-Object ` {$_.ManagedBy -eq $vm.Id}
foreach ($disk in $disks) {
# Add disk details to the array
$details += [PSCustomObject]@{
Type = "Disk"
VMName = $vmName
Name = $disk.Name
ManagedBy = $disk.ManagedBy
SKU = $disk.Sku.Name
Size = $disk.DiskSizeGB
Id = $disk.UniqueId
ResourceGroup = $disk.ResourceGroupName
SubscriptionName = $subscription.Name
SubscriptionId = $subscription.Id
CreationDate = $null
SourceId = $null
}
}
# Get all snapshots in the subscription
$snapshots = Get-AzSnapshot -ResourceGroupName $resourceGroup
# Loop through each snapshot
foreach ($snapshot in $snapshots) {
# Add snapshot details to the array
$details += [PSCustomObject]@{
Type = "Snapshot"
VMName = $vmName
Name = $snapshot.Name
ManagedBy = $null
SKU = $null
Size = $null
Id = $snapshot.Id
ResourceGroup = $snapshot.ResourceGroupName
SubscriptionName = $subscription.Name
SubscriptionId = $subscription.Id
CreationDate = $snapshot.TimeCreated
SourceId = $snapshot.CreationData.SourceResourceId
}
}
}
}
# Output combined details to CSV file
$details | Export-Csv -Path $path -NoTypeInformation
Write-Output "CSV file with combined VM, disk, and snapshot details has been created at $path."
Happy PowerShellin' out there! Feel free to comment on the post or drop me a line on how you may have taken this script and added more to suit your needs. I always find that 1) insightful and 2) something worthy of exploring or sharing further myself!
Cheers!
Yorumlar