I’m writing a powershell script to rotate my PAT in my devbox. Here’s what I’ve done:
PS> .Rotate-Pat.ps1
Here’s the code in Check-PatValidity.ps1:
$env:PAT | az devops login --organization "https://dev.azure.com/$organization"
$body = @{
displayName = $organization
scope = "vso.build vso.code_full vso.tokens vso.profile"
validTo = (Get-Date).AddDays(7).ToString("yyyy-MM-ddTHH:mm:ssZ")
allOrgs = $false
} | ConvertTo-Json
$response = Invoke-RestMethod -Uri "https://vssps.dev.azure.com/$organization/_apis/tokens/pats?api-version=7.1-preview.1" -Headers $headers -Method Post -Body $body -ContentType "application/json"
$prettyJson = $response | ConvertTo-Json -Depth 2
$prettyJson | Out-file $env:USERPROFILEDownloadspatgenoutput.json -Encoding UTF8
if ($response) {
$newPat = $response.patToken.token
Write-Output "New PAT: $newPat"
}
Here’s what I see as output (which is exactly the same as patgenoutput.json):
Looks like the html of Azure DevOps Login screen.
Questions:
- Am I on the right track? If not, please point me in the right direction.
- How to make the Azure DevOps login as an interactive session and get the access token to generate the PAT?
2
Answers
According to your description, you are trying to use the Pats – Create REST API to create a new PAT token with the old PAT token. However, it is not allowed to create a new token with an old PAT token.
You can check this document Manage personal access tokens (PATs) using REST API and the following Frequently asked questions (FAQs).
Here are the steps to create an Azure AD application and create the PAT.
Register a new application under Microsoft Entra ID -> App Registrations in the Azure portal if you don’t have one.
Select your application and navigate to API Permissions and select Azure DevOps -> check user_impersonation -> select Add permissions.
Add https://jwt.ms as Redirect URI.
Create a Client secret in the Certificates & secrets
Paste and access the following link in your browser to make authorization request to get the authorization code in the resulting URL:
Result:
Please note that the authorization codes are short lived, typically expiring after about 10 minutes. So, if you want to get a new token, you can get a
refresh_token
first instead of getting an authorization code manually again. You can refer this question to get therefresh_token
. For more details, please refer Microsoft identity platform and OAuth 2.0 authorization code flowBy the way, in your script, you are using the
az devops login
command and then use theInvoke-RestMethod
call. However, theInvoke-RestMethod
call won’t automatically inherit the login status from theaz devops login
command. Theaz devops login
command is used to authenticate the Azure DevOps CLI, but it doesn’t directly affect the PowerShell session or theInvoke-RestMethod
call. So, yourInvoke-RestMethod
call met the azure devops login page as you didn’t login.Alternatively, make use of below modified script that asks user to login interactively and generates bearer token to call Azure DevOps API for PAT creation:
Output:
To confirm that, I checked the saved file where new PAT details displayed successfully as below:
Azure DevOps Portal: