0
votes

Good day,

I am trying to figure out a way to get all the parent objects in my azure subcription to a csv from Azure. By parent object, I am refering to objects like, VMs, Webapps, Kubernetes Clusters, ect. I want to strip away any data that is deemed illrevelant like Nics, PIPs, storage disks, ect. I am not super proficent in powershell. and I am not sure how to tackle this. I have an azure workbook that I created that gives a good overview in a nice format, I would like to export the entire workbook for offline viewing but that doesn't seem to be possible.

Any help would be greatly appreiciated.

1
so ... what have you tried? what failed to work as expected? what errors did you get?Lee_Dailey

1 Answers

0
votes

So, what's an "interesting" resource to you may not be to the next person, and vice versa - in some cases, for example, I may set up NICs independently of my VMs, and want to see them. I don't think there's a way to automatically get just the things you want. What you could do is create a list of resources that are interesting to you (by type), and then use Powershell to create your report:

Get 'em all and filter 'em

$resourceTypes = @(
    'Microsoft.Compute/virtualMachines',
    'Microsoft.Sql/servers',
    'Microsoft.Sql/servers/databases'   
)

$resources = @()
Get-AzResource | ForEach-Object {
    if ($resourceTypes -contains $_.resourceType) {
        $resources += [PSCustomObject] @{
            ResourceGroupName = $_.ResourceGroupName
            ResourceName = $_.ResourceName
            ResourceType = $_.ResourceType
        }
    }
}

$resources | Sort-Object ResourceType, ResourceGroupName, ResourceName |
  Export-Csv -Path <path to>\resources.csv

Get 'em type by type (this one loops through subscriptions to which you have access, will print out a line with the current context on each subscription, will restore context to the current subscription when done)

$resourceTypes = @(
    'Microsoft.Compute/virtualMachines',
    'Microsoft.Sql/servers',
    'Microsoft.Sql/servers/databases'   
)

$resources = @()

$currentContext = Get-AzContext

try {
    Get-AzSubscription | ForEach-Object {
        $_ | Set-AzContext
        $subscriptionName = $_.Name

        $resourceTypes | ForEach-Object {
            Get-AzResource -ResourceType $_ | ForEach-Object {
                $resources += [PSCustomObject] @{
                    SubscriptionName = $subscriptionName
                    ResourceGroupName = $_.ResourceGroupName
                    ResourceName = $_.ResourceName
                    ResourceType = $_.ResourceType
                }
            }
        }
    }
} finally {
    $currentContext | Set-AzContext
}

$resources | Sort-Object ResourceType, SubscriptionName, ResourceGroupName, ResourceName | 
  Export-Csv -Path <path to>\resources.csv

Whichever approach you choose, just customize the $resourceTypes list to contain just the resource types that you want.

To get a list of resource types, I do something like this:

Get-AzResourceProvider -ProviderNamespace Microsoft.Sql | 
  Select ProviderNamespace -Expand ResourceTypes | 
  Select @{ L="Provider"; E={ "$($_.ProviderNameSpace)/$($_.ResourceTypeName)" } }

Leave off the -ProviderNamespace Microsoft.Sql if you want to get all resource types, but that will be a long list.