Skip to content

Instantly share code, notes, and snippets.

@codingfreak
Last active October 28, 2025 21:18
Show Gist options
  • Save codingfreak/6bf72737a2e65e334de76ee249d2a644 to your computer and use it in GitHub Desktop.
Save codingfreak/6bf72737a2e65e334de76ee249d2a644 to your computer and use it in GitHub Desktop.
rbac-roles-generator
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"
@codingfreak
Copy link
Author

Uaage

  1. Store this file locally. Lets say you named it create.ps1.
  2. Ensure that Get-InstalledPsResource Az returns something.
  3. Ensure that your posh-session is authenticated in Azure (Connect-AzAccount).
  4. Execute ./create.ps1.
  5. A new file ./rbac.bicep is 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment