Search code examples
export-to-excelazure-resource-group

Export List of all Associated resources in Azure


Does anyone know of a way to export a list (preferably xlsx) of all resources and their associations.

For example I would like to know in one row on a spreadsheet the name of a VM, virtualNIC, StorageAccount, vNet, ResourceGroup, SecurityGroup and any other resource associated with the VM itself.

The purpose is to see at a glance how all resources are associated so for example if I added port rules to a SecurityGroup I would be able to see all other resources affected.

Cheers


Solution

  • You can use a script like this

    Login-AzureRmAccount
    
    Function Dump-VirtualMachinesV1($outputPath) {
        $VMs = Find-AzureRmResource -ResourceType 'Microsoft.ClassicCompute/virtualMachines' -ExpandProperties | Sort-Object ResourceName
    
        $output = "Name,Location,IP,VNET,OS,Azure Size,Core,RAM,OS Disks,Temp Disk,Data Disks (TB),Resource Groups,C (OS),Temp, Data,Storage Account,Diagnostics Storage Account,Endpoints,Status,VIP`n"
    
        foreach ( $vm in $VMs ) {
            $vmSize = FixSize($vm.Properties.HardwareProfile.Size)
            $vmCores = (GetVmConfig($vm.Properties.HardwareProfile.Size)).Cores
            $vmRAM = (GetVmConfig($vm.Properties.HardwareProfile.Size)).RAM
            $diagnosticsStorageAccount = GetDiagnosticsStorageAccount($VM.Properties)
            $endpoints = GetEndpoints($vm.Properties)
    
            $output += $vm.Name + "," +`
                $vm.Location + "," +`
                $vm.Properties.InstanceView.PrivateIpAddress + "," +`
                $vm.Properties.NetworkProfile.VirtualNetwork.Name + "," +`
                $vm.Properties.StorageProfile.OperatingSystemDisk.OperatingSystem + "," +`
                $vmSize + "," +`
                $vmCores + "," +`
                $vmRAM + "," +`
                "1," +`
                "1," +`
                $vm.Properties.StorageProfile.DataDisks.Count + "," +`
                $vm.ResourceGroupName + "," +`
                $vm.Properties.DomainName.Name + "," +`
                "," +`
                "," +`
                $vm.Properties.StorageProfile.OperatingSystemDisk.StorageAccount.Name + "," +`
                $diagnosticsStorageAccount + "," +`
                $endpoints + "," +`
                $vm.Properties.InstanceView.Status + "," +`
                $vm.Properties.InstanceView.PublicIpAddresses + "`n"
        }
        $output | Out-File -Encoding ascii $outputPath
    }
    
    
    Function Dump-VirtualMachinesV2($outputPath) {
        $VMs = Find-AzureRmResource -ResourceType 'Microsoft.Compute/virtualMachines' -ExpandProperties | Sort-Object ResourceName
    
        $output = "Name,Location,IP,VNET,OS,Azure Size,Core,RAM,OS Disks,Temp Disk,Data Disks (TB),Cloud Services,C (OS),Temp, Data,Storage Account,Diagnostics Storage Account,Endpoints,Status,VIP`n"
    
        foreach ( $vm in $VMs ) {
            $vmSize = FixSize($vm.Properties.HardwareProfile.VMSize)
            $vmCores = (GetVmConfig($vm.Properties.HardwareProfile.VMSize)).Cores
            $vmRAM = (GetVmConfig($vm.Properties.HardwareProfile.VMSize)).RAM
            $diagnosticStorageAccount = GetDiagnosticsStorageAccount($vm.Properties)
            $endpoints = GetEndpoints($vm.Properties)
    
    
            $vm.Properties.StorageProfile.OsDisk.VHD -match "https*://(\w+)"
            $osStorageAccount = $matches[1]
    
            $output += $vm.Name + "," +`
                $vm.Location + "," +`
                $vm.Properties.InstanceView.PrivateIpAddress + "," +`
                $vm.Properties.NetworkProfile.VirtualNetwork.Name + "," +`
                $vm.Properties.StorageProfile.OsDisk.OsType + "," +`
                $vmSize + "," +`
                $vmCores + "," +`
                $vmRAM + "," +`
                "1," + `
                "1," + `
                $vm.Properties.StorageProfile.DataDisks.Count + "," +`
                $vm.Properties.DomainName.Name + "," + `
                "," + `
                "," + `
                "," + `
                $osStorageAccount + "s," +`
                $diagnosticsStorageAccount + "," +`
                $endpoints + "," +`
                $vm.Properties.InstanceView.Status + "," +`
                $vm.Properties.InstanceView.PublicIpAddresses + "`n"
            }
            $output | Out-File -Encoding ascii $outputPath
    }
    
    
    
    Function FixSize ($size) {
        Switch ($size) {
            'Extra Small' {$size = 'Standard_A0'}
            'Small' {$size = 'Standard_A1'}
            'Medium' {$size = 'Standard_A2'}
            'Large' {$size = 'Standard_A3'}
            'Extra Large' {$size = 'Standard_A4'}
        }
        return $size
    }
    
    
    Function GetVmConfig($size) {
        $size = FixSize($size)
        $vmConfig = @{"Cores" = 0; "RAM" = 0}
        switch -Regex ($size) {
            "A0$" {$vmConfig = @{"Cores" = 1; "RAM" = 0.75}}
            "A1$" {$vmConfig = @{"Cores" = 1; "RAM" = 1.75}}
            "A2$" {$vmConfig = @{"Cores" = 2; "RAM" = 3.5}}
            "A3$" {$vmConfig = @{"Cores" = 4; "RAM" = 7}}
            "A4$" {$vmConfig = @{"Cores" = 8; "RAM" = 14}}
            "A5$" {$vmConfig = @{"Cores" = 2; "RAM" = 14}}
            "A6$" {$vmConfig = @{"Cores" = 4; "RAM" = 28}}
            "A7$" {$vmConfig = @{"Cores" = 8; "RAM" = 56}}
            "A8$" {$vmConfig = @{"Cores" = 8; "RAM" = 56}}
            "A9$" {$vmConfig = @{"Cores" = 16; "RAM" = 112}}
            "A10$" {$vmConfig = @{"Cores" = 8; "RAM" = 56}}
            "A11$" {$vmConfig = @{"Cores" = 16; "RAM" = 112}}
            "DS?1(_v2)?$" {$vmConfig = @{"Cores" = 1; "RAM" = 3.5}}
            "DS?2(_v2)?$" {$vmConfig = @{"Cores" = 2; "RAM" = 7}}
            "DS?3(_v2)?$" {$vmConfig = @{"Cores" = 4; "RAM" = 14}}
            "DS?4(_v2)?$" {$vmConfig = @{"Cores" = 8; "RAM" = 28}}
            "DS?5(_v2)?$" {$vmConfig = @{"Cores" = 16; "RAM" = 56}}
            "DS?11(_v2)?$" {$vmConfig = @{"Cores" = 2; "RAM" = 14}}
            "DS?12(_v2)?$" {$vmConfig = @{"Cores" = 4; "RAM" = 28}}
            "DS?13(_v2)?$" {$vmConfig = @{"Cores" = 8; "RAM" = 56}}
            "DS?14(_v2)?$" {$vmConfig = @{"Cores" = 16; "RAM" = 112}}
            "GS?1$" {$vmConfig = @{"Cores" = 2; "RAM" = 28}}
            "GS?2$" {$vmConfig = @{"Cores" = 4; "RAM" = 56}}
            "GS?3$" {$vmConfig = @{"Cores" = 8; "RAM" = 112}}
            "GS?4$" {$vmConfig = @{"Cores" = 16; "RAM" = 224}}
            "GS?5$" {$vmConfig = @{"Cores" = 32; "RAM" = 448}}
        }
        return $vmConfig
    }
    
    
    
    Function GetVip($properties) {
        $vip = ''
        $inputEndpoints = $properties.NetworkProfile.InputEndpoints
        if ($inputEndpoints.Count -gt 0) {
            $vip = $inputEndpoints[0].publicIpAddress
        }
        return $vip
    }
    
    Function GetEndpoints($properties) {
        $endpoints = ''
        $inputEndpoints = $properties.NetworkProfile.InputEndpoints
        if ($inputEndpoints.Count -gt 0) {
            $endpoints += $inputEndpoints.PublicPort + '  '
        }
        return $endpoints
    }
    
    Function GetDiagnosticsStorageAccount($properties) {
        $storageAccount = ''
        foreach ($extension in $properties.Extensions) {
            if ($extension.Extension -eq 'IaaSDiagnostics') {
                $storageAccount = $extension.Parameters.Public.StorageAccount
            }
        }
        return $storageAccount
    }
    
    
    
    $outputPathV1 = "c:\temp\Azure\Q2-VMv1.csv"
    $outputPathV2 = "c:\temp\Azure\Q2-VMv2.csv"
    
    Dump-VirtualMachinesV1($outputPathV1)
    Dump-VirtualMachinesV2($outputPathV2)