0

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? Commented Mar 5, 2021 at 17:36

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

7 Comments

Yeah i tried to add clarification but, i guess it was missed, where would I get those resourcetypes? also how would i export to csv?
Added Export-Csv to resource scripts, sample script to get resource types.
Get-AzSubscription | ForEach-Object{ $subscriptionId = $_.Id $subscriptionName = $_.Name } Would i put all the code into this array for all found subcriptions? sorry Im new here and i dont know how to label it as code.
Are you saying you want the subscription associated with all the resources? Or a separate list of subscriptions on your account? Or something different ?
Well the command above is great, what im trying to add is that it queries all my subscriptions and adds the resources to the same csv output. as of now, it's only adding the last subscription that runs.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.