0
votes

I am trying to execute below powershell script to get list of all Azure subscriptions and resource details,

Get-AzSubscription | ForEach-Object {
    $subscriptionName = $_.Name
    Set-AzContext -SubscriptionId $_.SubscriptionId
    Get-AzResource | Select Name,ResourceGroupName,Location,Type
    } | Export-Csv azureinventory.csv

However, its just dumping the data in one column in mentioned CSV file. I need each column to display Subscription name, Subscription ID, associated resource name, resource groupname, location, Type.

Please help to update the script accordingly.

1
Please see my answer here: stackoverflow.com/questions/69223504/…. HTH.Gaurav Mantri

1 Answers

0
votes

I have updated the script and it is not tested. the parameter value can be different here. Kindly change it by verifying it from Get-Azresource.

$resources = @()
Get-AzSubscription | ForEach-Object {
    $_ | Set-AzContext
    $subscriptionName = $_.Name
    $subscriptionId = $_.SubscriptionId
    Get-AzResource | ForEach-Object {
        $resources += [PSCustomObject]@{
            SubscriptionName  = $subscriptionName
            SubscriptionId    = $subscriptionId
            ResourceGroupName = $_.ResourceGroupName
            ResourceName      = $_.ResourceName
            ResourceType      = $_.ResourceType
            Location          = $_.Location
        }
        
    }
}
$resources | Export-csv c:\sompeath\somename.csv