LLI Wiki — My Digital Knowledge Base

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

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

Mastodon LLIMastodon SocialFosstodon