Skip to content

Instantly share code, notes, and snippets.

@ryanotella
Last active October 30, 2024 14:50
Show Gist options
  • Save ryanotella/161cbeea9fceb679b73f49f7bc95b3d1 to your computer and use it in GitHub Desktop.
Save ryanotella/161cbeea9fceb679b73f49f7bc95b3d1 to your computer and use it in GitHub Desktop.
Azure CLI - Self-Activate PIM Role Assignments
ROLE="User Access Administrator"
SUBSCRIPTION=$(az account show --query id -otsv)
if [ ! -z "$RESOURCE_GROUP" ]; then
SCOPE="/providers/Microsoft.Subscription/subscriptions/$SUBSCRIPTION/resourceGroups/$RESOURCE_GROUP" # Group-scoped
echo "Scope (Group): $SCOPE"
else
SCOPE="/providers/Microsoft.Subscription/subscriptions/$SUBSCRIPTION" # Subscription-scoped
echo "Scope (Subscription): $SCOPE"
fi
echo
URI="$SCOPE/providers/Microsoft.Authorization/roleAssignmentScheduleRequests/$(uuidgen)?api-version=2020-10-01"
read -r -d '' BODY <<EOF
{
"properties": {
"principalId": "$(az ad signed-in-user show --query id --output tsv)",
"roleDefinitionId": "$(az role definition list --query "[?roleName=='$ROLE'].id | [0]" --output tsv)",
"scheduleInfo": {
"startDateTime": null,
"expiration": {
"duration": "PT8H",
"type": "AfterDuration"
}
},
"justification": "Deployment",
"requestType": "SelfActivate"
}
}
EOF
echo "$BODY" | az rest --method PUT --body "@-" --output yaml --uri "$URI"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment