Last active
October 28, 2025 21:18
-
-
Save codingfreak/6bf72737a2e65e334de76ee249d2a644 to your computer and use it in GitHub Desktop.
rbac-roles-generator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| param ( | |
| [string]$Description = "Provides a mapping with friendly names resolving to build in Azure RBAC Role Ids. See https://learn.microsoft.com/en-us/azure/role-based-access-control/built-in-roles for reference.", | |
| [string]$OutputFileUri = "$PSScriptRoot/azureBuildInRbacRoleIds.bicep", | |
| [string]$ExportVariableName = "azureBuildInRbacRoleIds" | |
| ) | |
| $prefix = @" | |
| @export() | |
| @description('$Description') | |
| var $ExportVariableName = { | |
| "@ | |
| $suffix = "}" | |
| $definitions = Get-AzRoleDefinition | ` | |
| Select-Object -Property Id, @{Label = "Name"; Expression = { $_.Name -replace '[.,-/()_ ]', '' } } | ` | |
| Sort-Object -Property Name | |
| $sb = New-Object -TypeName "System.Text.StringBuilder" | |
| $spacer = New-Object -TypeName "System.String" -ArgumentList ' ', 4 | |
| [void]$sb.AppendLine($prefix) | |
| foreach ($role in $definitions) { | |
| [void]$sb.AppendFormat("{0}{1}: '{2}'{3}", ` | |
| $spacer, ` | |
| $role.Name, ` | |
| $role.Id, ` | |
| [System.Environment]::NewLine) | |
| } | |
| [void]$sb.AppendLine($suffix) | |
| $sb.ToString() | Set-Content $OutputFileUri | |
| Write-Host "$(($definitions | Measure-Object).Count) RBAC roles detected and written to $($OutputFileUri)." | |
| Write-Host "Usage: import { $ExportVariableName } from $OutputFileUri" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uaage
create.ps1.Get-InstalledPsResource Azreturns something.Connect-AzAccount)../create.ps1../rbac.bicepis created in the same directory as your script.You can change the behavior by overriding the Bicep-description, the result file name and location and the name of the exported variable.