Last active
October 30, 2024 14:50
-
-
Save ryanotella/161cbeea9fceb679b73f49f7bc95b3d1 to your computer and use it in GitHub Desktop.
Azure CLI - Self-Activate PIM Role Assignments
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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