Export RBAC from all subscriptions in Azure AD to one csv

2k Views Asked by At

I have dozen of subscriptions and its very difficult to output all role assignments from all resources from all subscriptions at once. I'm currently using the one below but the output is looped for each subscription per csv file. How do I get output into one sheet preferably?

Get-AzSubscription | foreach-object {

    Write-Verbose -Message "Changing to Subscription $($_.Name)" -Verbose

    Set-AzContext -TenantId $_.TenantId -SubscriptionId $_.Id -Force
    $Name     = $_.Name
    $TenantId = $_.TenantId
    $SubId    = $_.SubscriptionId  

    Get-AzRoleAssignment -IncludeClassicAdministrators | Select-Object RoleDefinitionName,DisplayName,SignInName,ObjectType,Scope,
    @{name="TenantId";expression = {$TenantId}},@{name="SubscriptionName";expression = {$Name}},@{name="SubscriptionId";expression = {$SubId}} -OutVariable ra

    # Also export the individual subscriptions to excel documents on your Desktop.
    # One file per subscription
    $ra | Export-Csv -Path C:\scripts\Export-AzRoleAssignment\$Name.csv -NoTypeInformation

}
1

There are 1 best solutions below

0
On

You can use the -Append parameter for the Export-Csv command.

So, your last line would look something like this:

$ra | Export-Csv -Path C:\scripts\Export-AzRoleAssignment\csv_with_everything.csv -NoTypeInformation -Append

Or you could merge all the csv files into big file after it has run e.g. https://stackoverflow.com/a/43972739/1713589