i am running a powershell script that will connect to azure. How can I tell what azure roles and permissions will be required for the script to complete properly?
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Below is a pragmatic “recipe” you can reuse every time you (or a colleague) need to figure out what Azure role(s) and RBAC permissions a PowerShell script really needs.
Feel free to cherry-pick the pieces that fit your situation.
🏁 0. Identify who will run the script
• User account?
• Automation account?
• GitHub/DevOps pipeline service-principal?
• Managed identity?
(The effective permissions are attached to that identity.)
🔍 1. Take inventory of what the script actually does
A. Pull out every Az-cmdlet (static)
powershell
# crude but fast
$ScriptPath = ".\DeployStuff.ps1"
$cmdletsUsed = Select-String -Path $ScriptPath -Pattern '\w+-Az\w+' |
Select-Object -ExpandProperty Matches |
Select-Object -ExpandProperty Value -Unique
$cmdletsUsed
B. Or run the script with “-WhatIf”/“-Confirm:$false” flags where possible, or simply in a dry-run subscription.
The Az module already knows this mapping; you can query it:
powershell
# list the underlying provider operations for a cmdlet keyword
$ops = Get-AzProviderOperation -OperationSearchString "New-AzVm" |
Select-Object OperationName
$ops
Run the script; anything that fails with 401 / 403 shows up in:
• The script’s own error output (most Az cmdlets bubble up the REST URI)
• Azure Activity Log (filter by your caller and status=“Failed” or “Denied”)
The Activity Log line literally tells you:
Authorization failed. The client 'XYZ' with object id '...' does not have authorization to perform action 'Microsoft.Storage/storageAccounts/listKeys/action' over resource ...
Copy that action string into your “neededOps” list.
Iterate until no 403s remain. (Only takes a few minutes in practice.)
🛠 4. Decide on the best RBAC option
• Everything satisfied by “Contributor”? Easy but over-privileged.
• Small set of actions? Create a custom role:
powershell
$custom = @{
Name = "Script-Deployment-Role"
Description = "Least-privilege role for DeployStuff.ps1"
Actions = $neededOps # ⇐ list you built above
NotActions = @()
AssignableScopes = @("/subscriptions/<subId>")
}
$custom | ConvertTo-Json -Depth 5 | Out-File ScriptRole.json
az role definition create --role-definition ScriptRole.json
• Script also manipulates AAD/MSGraph (service principals, groups, etc.)?
Add the matching Azure AD roles (e.g. “Application Administrator”, “Directory Reader”).
Those are separate from ARM-RBAC.
📚 5. Reference cheat-sheet (handy for ~80 % of scripts)
✅ Script reviewed for every Az-cmdlet it calls
✅ Underlying provider operations enumerated
✅ 403 “access denied” messages resolved
✅ Custom or built-in role chosen
✅ Role assigned to the identity at the correct scope (resource group / subscription)
✅ Principle of Least Privilege documented next to the script (README or header)
Tips & Tools
• VS Code extension “Azure Resource Manager (ARM) Tools” highlights provider operations in templates; good companion for Bicep/ARM bits inside scripts.
• Community module Az.PermisisonInsights (find on GitHub/PSGallery) automates a lot of step 2.
• For pipelines, attach the role to a Federated Workload Identity (OIDC) for password-less auth.
• Keep the role definition in source control; treat it like code.
That’s it—run the recipe once and you’ll always be able to articulate exactly which Azure roles a PowerShell script requires, and nothing more. Happy scripting!