Azure (ASM) Classic IaaS inventory

If you ever wanted to capture a full inventory of an Azure Classic ASM IaaS based environment, using the script below is how you can do it – run it for ‘each’ Cloud Service.

This will create two .json files on the desktop, one for the Cloud Service containing all the VMs (along with detailed information) and another for the vNet which is used.


### Log into Azure ARM
Login-AzureRmAccount
### Choose Subscription
$subscription = (Get-AzureRmSubscription | Out-GridView -Title "Select the Azure subscription that you want to use …" -PassThru).SubscriptionName
Select-AzureRmSubscription -SubscriptionName $subscription
##########################################################################################
########################## Select source Virtual Machine ###########################
##########################################################################################
#region Virtual Machines | @marckean
cls
Write-Host "`nGathering a list of all virtual machines in the 'old' ASM based Azure as well as their location and status…" -ForegroundColor Cyan
$VMs = Find-AzureRmResource -ExpandProperties | ? {$_.ResourceType -eq 'Microsoft.ClassicCompute/virtualMachines'} | `
select Name,Location,`
@{Name='CloudService';Expression={$_.ResourceGroupName}},`
@{Name='VirtualNetworkName';Expression={$_.properties.networkprofile.virtualnetwork.name}},`
@{Name='PowerState';Expression={$_.properties.instanceview.powerstate}}
$SourceVM = ($VMs | Out-GridView -Title "Select a VM in a Cloud Service – this script will gather information about the entire cloud service…" -PassThru)
$SourceCS = $SourceVM.CloudService
write-host -nonewline "`n`tThe virtual machine you selected is: " -ForegroundColor Yellow; `
write-host -nonewline $SourceVM.name`n -ForegroundColor Green; `
start-sleep -seconds 3
#endregion
##########################################################################################
#################### Capture source Cloud Service information ######################
##########################################################################################
#region Other details of the cloud service & virtual machine | @marckean
cls
Write-Host "`n`tGetting other details of the cloud service & virtual machine/s…" -ForegroundColor Cyan
####################### | Source Virtual Machine & Cloud Service information | ####################### | @marckean
$SourceResources = Find-AzureRmResource -ExpandProperties
$CSVMResources = $SourceResources | ? {$_.ResourceGroupName -eq $SourceCS -and $_.ResourceType -eq 'Microsoft.ClassicCompute/virtualMachines'}
$vNetName = ($CSVMResources.properties.networkprofile.virtualnetwork.name | select -Index 0)
$CSvNetResource = $SourceResources | ? {$_.ResourceName -eq $vNetName -and $_.ResourceType -eq 'Microsoft.ClassicNetwork/virtualNetworks'}
#endregion
#region Export results to files
$ClassicVMs = @()
$ClassicVMsPath = "$env:USERPROFILE\Desktop\AzureASMClassicVMsPath_{0}.json" -f $SourceCS
$ClassicvNetPath = "$env:USERPROFILE\Desktop\AzureASMClassicvNetPath_{0}.json" -f $SourceCS
foreach($CSVMResource in $CSVMResources){
$ClassicVMs += $CSVMResource | ConvertTo-Json -Depth 50
}
$CSvNetResource | ConvertTo-Json -Depth 50 | Out-File -FilePath $ClassicvNetPath
$ClassicVMs | Out-File -FilePath $ClassicVMsPath
#endregion

Leave a comment