List all your resources in Azure

I want to share a handy script that lists out all the resources in your Azure subscriptions and exports that data to an excel workbook.

It really lists out every single Azure resource (classic and ARM) from virtual machines and NICS to automation accounts and alerts. There are also a few tricks in there to decorate the data with some extra information. e.g. whether a vm is powered on, what vnet it is in etc. You can use a similar approach to modify the script to output whatever data you are interested in.

The script is very useful at giving a quick overview of what your current azure usage is and is good for flagging up anything that shouldn’t be there.

If you want to skip the walk through the full script is at the end of the post.

Firstly, before running the script be sure to authenticate to Azure in both classic and ARM modes

Add-AzureAccount
Login-AzureRmAccount

Then we define a powershell class to help store and manipulate the data, classes are new in powershell 5

Class AzureResource
{
	[string]$Subscription
	[string]$ResourceType
	[string]$ResourceId
	[string]$ResourceGroup
	[string]$Location
	[string]$Name
	[string]$ServiceName
	[string]$VNet
	[string]$Subnet
	[string]$Size
	[string]$Status
}

Iterating through all the specified subscriptions we run

$vms = Get-AzureVM 
$armVMs = Get-AzureRmVM 
$resources = Get-AzureRmResource

This will give us lists of all vms in the subscription and we will use them later but more importantly the command Get-AzureRmResource will list all resources (classic and ARM) in the subscription and provide the majority of the data we need.

Once we have the $resources array we iterate through it and instantiate a new AzureResource object for each one.

foreach($resource in $resources)
{
	$azureResource = New-Object AzureResource
	$azureResource.Subscription = $Subscription
	$azureResource.ResourceType = $resource.ResourceType
	$azureResource.Location = $resource.Location
	$azureResource.ResourceId = $resource.ResourceId
	$azureResource.ResourceGroup = $resource.ResourceGroupName
        $azureResource.Name = $resource.Name
}

As I talked about later it is possible to decorate this data with even more info. I’ve chosen to add in some virtual machine specific data to any virtual machines present (vnet, subnet etc) but you can use the same approach to add in custom data for any Azure resource type you are interested in, say PaaS SQL databases or storage accounts.

Dealing with classic vms first

if($resource.ResourceType -eq 'Microsoft.ClassicCompute/virtualMachines')
{
        # Classic VM
        $vm = $vms | Where-Object { $_.Name -eq $azureResource.Name} 
	$azureResource.VNet = $vm.VirtualNetworkName
	$azureResource.Subnet = Get-AzureSubnet -VM $vm
	$azureResource.Size = $vm.InstanceSize
	$azureResource.Status = $vm.InstanceStatus
	$azureResource.ServiceName = $vm.ServiceName
}

A bit more work is needed to get the same data for the ARM vms

if($resource.ResourceType -eq 'Microsoft.Compute/virtualMachines')
{
	# ARM VM
	$armVM = $armVMs | Where-Object { $_.Name -eq $azureResource.Name} 
	$azureResource.Size = $armVM.HardwareProfile.VirtualMachineSize
	$vmstatus = Get-AzurermVM -Name $armVM.Name -ResourceGroupName $armVM.ResourceGroupName -Status
	$azureResource.Status = (get-culture).TextInfo.ToTitleCase(($vmstatus.statuses)[1].code.split("/")[1])
        $nicName = ($armVM.NetworkInterfaceIDs[0] -split '/')[-1]
	$nic = Get-AzureRmNetworkInterface -Name $nicName -ResourceGroupName $azureResource.ResourceGroup
	$IpConfig = ConvertFrom-Json -InputObject $nic.IpConfigurationsText
	$subnet = $IpConfig.Subnet.Id			
	$azureResource.VNet = ($subnet -split '/')[-3]
	$azureResource.Subnet = ($subnet -split '/')[-1]		
}

Finally, we write the data to a csv file and that’s it

$allResources += $azureResource | Select-Object `
			Subscription, `
			ResourceType, `
			Name, `
			ServiceName, `
			Size, `
			Status, `
			VNet, `
			Subnet, `
			Location, `
			ResourceGroup, `
                        ResourceId 
}
$allResources = $allResources | Sort-Object Subscription, ResourceType
$allResources | Export-Csv -Path $file -Force

Here is the final script hosted on gist

Advertisements

One thought on “List all your resources in Azure”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s