Search code examples
powershellazure-devopsazure-cliazure-devops-rest-api

How do we deal with the --continuation-token when using the az devops security group list cmdlet?


How do you work with the --continuation-token argument in order to deal with pagination on the command line?

For example if you run:

   az devops security group list --org $Organization --project $project > tmp1.json 

the output is:

"continuationToken": "eyJTY29wZUlkIjoiNDQzYzBhYjEtZjY3OC00ZmZkLWE1NTEtNWUzYTU5ZTg0NjEwIiwiUGFnZVNpemUiOjUwMCwiSW5jbHVkZUdyb3VwcyI6dHJ1ZSwiSW5jbHVkZU5vbkdyb3VwcyI6ZmFsc2UsIlBhZ2VuYXRpb25Ub2tlbiI6ImY1MjI5NWI1LWVjNzgtNGNjZS04ODQ4LWFiYzM3ODc5ZmEzZSJ9",
  "graphGroups": [
    {and the rest of the file with the groups listed

How do we deal with the ContinuationToken when it is returned?

When


Solution

  • You can do this without dropping back to the API as suggested above.

    do {
                    
            if ($null -eq $token) {
                $groups = az devops security group list --org $Organization --project $project | ConvertFrom-Json
            }
            else {
                $groups = az devops security group list --org $Organization --project $project --continuation-token $Token | ConvertFrom-Json
            }
        
            if ($groups.Length -le 0) { Break; }
            
            $token = $groups[$groups.Count - 1].ContinuationToken
            
            foreach ($group in $groups) {
                # Do stuff and things
                
            }
        }
        while ($null -ne $token)