Forum Discussion

AlphaBetaGamma's avatar
AlphaBetaGamma
Brass Contributor
Dec 30, 2020

How to list azure resources which have inheriting access and direct access using powershell

Hi,   Is it really possible to list azure resources which have inherited access and direct access using Powershell? I couldn't find any azure PowerShell command which shows at least any column whic...
  • AndySvints's avatar
    AndySvints
    Jan 02, 2021

    Hello AlphaBetaGamma,

    Here is quick and dirty way to get what you need:

    $Resource=Get-AzResource
    $RoleAssignments=New-Object System.Collections.Generic.List[PSObject]
    foreach($r in $Resource){
        $Assignment=Get-AzRoleAssignment -ResourceName $r.Name -ResourceGroupName $r.ResourceGroupName -ResourceType $r.ResourceType
        foreach($a in $Assignment){
            $IsInherited=if($r.ResourceId -eq $a.Scope){$false}else{$true}
    
           $a | Add-member -NotePropertyName ResourceName -NotePropertyValue $r.Name
            $a | Add-member -NotePropertyName ResourceId -NotePropertyValue $r.ResourceId
           $a | Add-member -NotePropertyName IsInherited -NotePropertyValue $IsInherited
           $RoleAssignments.Add($a)
        }
    }
    $RoleAssignments

     

    Pseudo code:

    1. Get all az resources
    2. For each resource run get az role assignments
    3. Loop trough role assignments and add IsInherited property:
      If scope eq resourceid then false otherwise true
    4. Add ResourceName and ID to resulting object
    5. Add results to List

    At the end you will get list of all Role Assignments with additional info(ResourceName, ResourceID and IsInherited flag).

    Then you can easily rotate data as needed.

    List Direct Assignments:

    #Direct Assignments
    $RoleAssignments  |where {$_.isinherited -eq $false} |select ResourceName, DisplayName, RoleDefinitionName, IsInherited

     

    List Inherited Assignments:

    #Inherited ones
    $RoleAssignments  |where {$_.isinherited -ne $false} |select ResourceName, DisplayName, RoleDefinitionName, IsInherited

     

    General stats:

    $RoleAssignments.isinherited  |group

     

     

    Hope that helps.

     

     

Resources