skip to Main Content

I need help to create a script.

The task is to in Azure to export Subscriptions and owners of these subscriptions to CSV file. I assume I will be using powershell for this task

Please help!

2

Answers


  1. Chosen as BEST ANSWER

    I figure out the solution. Please see below.

    $subs = Get-AzSubscription
    $filename = "Subs_CoAdmins"+(Get-Date -UFormat "%Y-%m-%d_%H-%m-%S")+".xlsx"
    $allRBACs = @()
    foreach ($sub in $subs) {
        if (Select-AzSubscription -SubscriptionName $sub.Name) {
            if ($rbacs = Get-AzRoleAssignment -IncludeClassicAdministrators) {
                foreach ($rbac in $rbacs) {
                    if (($rbac.Scope -like "/subscriptions/*") -and ($rbac.ObjectType -eq "Microsoft.Authorization/classicAdministrators") -and ($sub.SubscriptionPolicies.QuotaId -like "*MSDN*")) {
                        $rbacObj = New-Object -TypeName psobject
                        $rbacObj | Add-Member -MemberType NoteProperty -Name Subscription -Value $sub.Name
                        $rbacObj | Add-Member -MemberType NoteProperty -Name SubscriptionId -Value $sub.Id
                        $rbacObj | Add-Member -MemberType NoteProperty -Name Scope -Value $rbac.Scope
                        $rbacObj | Add-Member -MemberType NoteProperty -Name RoleDefinitionName -Value $rbac.RoleDefinitionName
                        $rbacObj | Add-Member -MemberType NoteProperty -Name RoleDefinitionId -Value $rbac.RoleDefinitionId
                        $rbacObj | Add-Member -MemberType NoteProperty -Name DisplayName -Value $rbac.DisplayName
                        $rbacObj | Add-Member -MemberType NoteProperty -Name SignInName -Value $rbac.SignInName
                        $rbacObj | Add-Member -MemberType NoteProperty -Name ObjectType -Value $rbac.ObjectType
                        $rbacObj | Add-Member -MemberType NoteProperty -Name SubQuotaId -Value $sub.SubscriptionPolicies.QuotaId
                        $allRBACs += $rbacObj
                    }
                }
            }
        }
    }
    $allRBACs | Export-Excel ./$filename -AutoSize -AutoFilter
    

  2. You can use the below PowerShell script to pull the list of users which has the owner access to the subscription.

    Connect-AzAccount
    
    $sublist= Get-AzSubscription 
    foreach ($item in $sublist){
    
        $scopeappend= "/subscriptions/"+$item.Id
    $export=(Get-AzRoleAssignment -RoleDefinitionId "8e3af657-a8ff-443c-a75c-2fe8c4bcb635" -Scope $tdt  | where {($_.ObjectType -EQ "user") -and ($_.Scope -EQ $scopeappend) }  ) | select DisplayName,SignInName
    }
    
    $export|Export-Csv -Path C:test.csv
    

    I have tested the above PowerShell script and it is working fine for me.

    You can refer to this documentation for For-each loop syntax as well.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search