Search code examples
azurepowershellcommand-line-interfacesubscription

Azure CLI/ Powershell to get all my Azure subscriptions and export the RoleAssignment


Good day,

I am trying to get all my Azure subscriptions for my tenant and export a list of all the Role Assignments in the Access Control (IAM) panel.

The end goal is to check all the Role Types and check if any Users have access without being in a Group. Once I am done with Subscriptions I also want to check all resource groups.

I will manually populate all CSV's into one, but if there is a way for all data to go into one CSV it would be much easier.

Connect-AzAccount

$subscriptions = Get-AzSubscription

foreach ($subscription in $subscriptions) {
    Set-AzContext -SubscriptionId $subscription.Id
    $roleAssignments = Get-AzRoleAssignment
    $filePath = "C:\path\to\your\folder\RoleAssignments_$($subscription.Name).csv"
    $roleAssignments | Export-Csv -Path $filePath -NoTypeInformation
}

Solution

  • you could first get all CSVs and then use import-csv and export-csv

       get-childitem -path "C:\path\to\your\folder\" -filter "*.csv"  |%{
     import-csv -path $_.FullName | export-csv -Path "c:\test\newfile.csv" -Append -Force
     }
    

    or else, you could use directly like below,file name is constant here

    $roleAssignments | Export-Csv -Path "c:\test\somestaticfilename.csv" -append -NoTypeInformation