Azure PIM: activating eligible roles from PowerShell
Why just-in-time access matters for a DBA and how to activate an eligible role from PowerShell before admin work.
A DBA needs elevated rights for some tasks: creating a user, changing a resource, reading a sensitive setting. Keeping them permanently hands a powerful account to anyone who compromises it. Azure Privileged Identity Management (PIM) offers the opposite: eligible roles that you activate for a limited time, with a justification. Here is why it matters and how I activate roles from PowerShell before admin work.
Why just-in-time matters for a DBA
- Smaller attack surface: outside an activation, my account cannot change anything. A token stolen on a Tuesday morning does not open admin rights.
- Traceability: each activation has a written reason, a start time and a duration. You know who had which rights, when and why.
- Healthy habit: having to justify pushes you to ask for the right duration and scope instead of an
Owner"just in case". - Compliance: audits ask for exactly this, temporary privileged access that is motivated or approved.
The flow
flowchart LR
A["Eligible role (PIM)"] --> B["Script : Connect-AzAccount"]
B --> C["List eligibilities"]
C --> D["SelfActivate request + justification + duration"]
D --> E["Active role (temporary)"]
E --> F["Admin work"]
F --> G["Automatic expiry"]
List my eligible roles
Azure resource roles go through the Azure Resource Manager API, via the Az.Resources module. No Microsoft Graph permission is needed: only your RBAC eligibility counts.
Connect-AzAccount
Get-AzRoleEligibilityScheduleInstance -Scope '/' -Filter 'asTarget()' |
Select-Object RoleDefinitionDisplayName, ScopeDisplayName, Scope, RoleDefinitionId
The asTarget() filter returns the signed-in user's eligibilities across all scopes, from the root. The subscription chosen at sign-in does not change the result.
Activate a role
My deliberately short example: activate Contributor on a resource group for four hours.
param(
[string]$RoleName = 'Contributor',
[string]$Scope = '/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg-dba-prd',
[string]$Justification = 'Planned maintenance on elastic pool',
[int]$Hours = 4
)
$me = (Get-AzADUser -SignedIn).Id
$elig = Get-AzRoleEligibilityScheduleInstance -Scope $Scope -Filter 'asTarget()' |
Where-Object { $_.RoleDefinitionDisplayName -eq $RoleName } | Select-Object -First 1
if (-not $elig) { throw "No eligible '$RoleName' on $Scope" }
# Already active? Do not ask again.
$active = Get-AzRoleAssignmentScheduleInstance -Scope $Scope -Filter 'asTarget()' |
Where-Object { $_.RoleDefinitionDisplayName -eq $RoleName -and $_.AssignmentType -eq 'Activated' }
if ($active) { Write-Output 'Role already active.'; return }
New-AzRoleAssignmentScheduleRequest -Name (New-Guid).Guid -Scope $Scope `
-PrincipalId $me -RoleDefinitionId $elig.RoleDefinitionId `
-RequestType SelfActivate -Justification $Justification `
-ScheduleInfoStartDateTime (Get-Date).ToUniversalTime().ToString('o') `
-ExpirationType AfterDuration -ExpirationDuration "PT${Hours}H"
I first check whether the role is already active, then request activation with a mandatory justification and a short duration. The role's PIM policy may require approval, multifactor authentication or cap the maximum duration: the script does not bypass any of it.
Lessons learned
- Check the state before activating: requesting an already active role gives a policy error rather than a silent success.
- Right-sized duration: two to four hours for an intervention, not eight "to be safe". Every activation is audited.
- An existing token can be stale: a cached
Azcontext may hold an expired token (MFA, conditional access). Sign in again rather than troubleshooting for an hour. - The subscription picker shows up on every interactive sign-in:
Update-AzConfig -LoginExperienceV2 Offremoves it. - Tokens do not refresh with the role: after activation, wait a moment, or sign in again, for the new role to appear in the token.
- Entra ID directory roles (as opposed to Azure resource roles) go through Microsoft Graph, with other modules and other permissions: do not mix them with resource roles.
- Store nothing: the script keeps no credential, it relies on the
Connect-AzAccountsession.