Skip to content

Instantly share code, notes, and snippets.

@JayDoubleu
Created January 25, 2024 14:28
Show Gist options
  • Save JayDoubleu/85676073a43aa518cae7d3d8ae65b3cf to your computer and use it in GitHub Desktop.
Save JayDoubleu/85676073a43aa518cae7d3d8ae65b3cf to your computer and use it in GitHub Desktop.
Bash script to create Azure DevOps PAT token programmatically. Note, creation of PATs under service principals is NOT supported
#!/bin/bash
set -x
patName="nameOfThePatToken"
adoOrgName="yourOrgName"
apiVersion="7.1-preview.1"
apiUri="https://vssps.dev.azure.com/$adoOrgName/_apis/tokens/pats?api-version=$apiVersion"
validTo=$(date -d '+90 days' +'%Y-%m-%dT%H:%M:%S.%3NZ')
jsonString=$(cat <<EOF
{
"displayName": "$patName",
"scope": "vso.code",
"validTo": "$validTo",
"allOrgs": true
}
EOF
)
echo "Display all existing PATs"
az rest --method GET --resource "https://management.core.windows.net/" --uri $apiUri
echo "Create a new PAT"
set +x
PAT=$(az rest --method POST --resource "https://management.core.windows.net/" --uri $apiUri --body "$jsonString" | jq -r .patToken.token)
set +x
echo "##vso[task.setvariable variable=ADO_PAT_TOKEN;issecret=true]$PAT"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment