Forum Discussion
How to list azure resources which have inheriting access and direct access using powershell
- 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:
- Get all az resources
- For each resource run get az role assignments
- Loop trough role assignments and add IsInherited property:
If scope eq resourceid then false otherwise true - Add ResourceName and ID to resulting object
- 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.
AndySvints Thanks a mil for your response.
Yes, that will help to list the resources which have inherited access and direct access.
Just messing around a bit and came up with something if we wish to pull out the exact resource name which has inherited access and exact resource name with direct access.
Procedure 1:
It is partially correct.
Your code is assuming that permissions can be inherited only from Subscription level which is not true.
There also might be assignments on the resource group level which will be inherited also.
As mentioned in List role assignments for a resource to get permissions directly assigned to a resource(storage account):
PS C:\> Get-AzRoleAssignment | Where-Object {$_.Scope -eq "/subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/storage-test-rg/providers/Microsoft.Storage/storageAccounts/storagetest0122"}
If you change Where-Object condition from equal to not equal you will get inherited permissions.
Hope that helps.
- AlphaBetaGammaJan 01, 2021Brass Contributor
AndySvints Thanks for your response. Yes, we can see the inherited access/direct access for each resource with the help of below one,
Get-AzRoleAssignment | Where-Object {$_.Scope -eq "/subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/storage-test-rg/providers/Microsoft.Storage/storageAccounts/storagetest0122"}
But, what if we wish to see a list of all azure resources which have inherited access and a list of azure resources with direct access. Something like this as a sample output
List of azure resources which have inherited accessResource name DisplayName keyvault Tom sql Jeremy List of Azure resources which have direct access
Resource name DisplayName Log analytics jack VM Jones - AndySvintsJan 02, 2021Iron Contributor
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:
- Get all az resources
- For each resource run get az role assignments
- Loop trough role assignments and add IsInherited property:
If scope eq resourceid then false otherwise true - Add ResourceName and ID to resulting object
- 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.