Azure VM PowerShell audit VHD disk information

I was having trouble getting a list of VHDs that are in use by my Azure VMs for OS and Data disks. I wanted to list them in a single line CSV to audit each VMs configuration. The issue was I had 3 commands with different objects:

  • Get-AzureVM
  • Get-AzureOSDisk
  • Get-AzureDataDisk

I wanted each output on the same line. I was able to rename the conflicting properties and output them to a single CSV, with each VM on a new line.

Script: (download)

$vms = Get-AzureVM -ServiceName mycloudservicenamehere
foreach($vm in $vms) {

#get-azurevm
$obj1 = $vm | Select-Object Name,InstanceSize,AvailabilitySetName,DNSName,deploymentname
#get VM Data disk, rename same properties to prevent output conflicts
$obj2 = $vm | Get-AzureDataDisk | Select-Object @{Name=’DataDiskName’; Expression={$_.DiskName}},@{Name=’DataDiskLabel’; Expression={$_.DiskLabel}},@{Name=’DataMediaLink’; Expression={$_.MediaLink}},@{Name=’DataDiskSizeGB’; Expression={$_.LogicalDiskSizeInGB}},@{Name=’DataLUN’; Expression={$_.Lun}}
#same as above VM data disk, but with OS disk
$obj3 = $vm | Get-AzureOSDisk | Select-Object  @{Name=’OSDiskName’; Expression={$_.DiskName}},@{Name=’OSMediaLink’; Expression={$_.MediaLink}},@{Name=’OSDiskLabel’; Expression={$_.DiskLabel}}, OS

#combine these outputs into a single row CSV per VM
$combined = New-Object -TypeName PSObject
$obj1,$obj2,$obj3|%{
$CurObj = $_;
$_|gm|?{$_.MemberType -match “NoteProperty”}|%{
$NewMember = $_.Name;
$Combined|Add-Member -MemberType NoteProperty -Name $NewMember -Value $CurObj.$NewMember
}
}
#write each VM output to CSV
$Combined|Export-CSV -Path c:\Users\administrator\Desktop\output.csv -NoTypeInfo -Append -Force
}

Output csv:

Output properties: Notice how I renamed conflicting disk properties to be prefixed with “Data” or “OS”.

  1. AvailabilitySetName
  2. DeploymentName
  3. DNSName
  4. InstanceSize
  5. Name
  6. DataDiskLabel
  7. DataDiskName
  8. DataDiskSizeGB
  9. DataLUN
  10. DataMediaLink
  11. OS
  12. OSDiskLabel
  13. OSDiskName
  14. OSMediaLink

Now I can look at VM Instance Size, Labels, OS/Data disk information, etc. and make any changes. Our team has about 40+ SharePoint development VMs in multiple cloud services, so provisioning/management can be a bit of work. In a perfect world I would use PowerShell DSC or Azure Automation, but that is quite a bit of work to setup for VMs we scrap every few months once projects are complete.

Next steps:

  • Split VMs into unique cloud services so multiple developers can start/stop VMs at the same time (worker process conflict) (using same site-to-site VPN)
  • Audit OS/Data disk configuration and possible move between storage accounts
  • Set affinity groups for disks/VMs

Links: