Reputation: 833
I am trying to retrieve all projects from my organizations I have 300 projects but api call returns only 100
I tried using $top with api then it returns nothing
Here is my script which works for getting 100 projects and converting to csv file but I need details of all 300 projects in this csv file -
connectionToken = ""
$BaseUrl = "https://dev.azure.com/{organization_name}/_apis/projects?api-
versions=5.0"
$base64AuthInfo =
[System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes
(":$($connectionToken)"))
$ProjectInfo = Invoke-RestMethod -Uri $BaseUrl -Headers @{authorization =
"Basic $base64AuthInfo"} - Method Get
$ProjectDetails = $ProjectInfo | ConvertTo-Json -Depth 100 | out-file
"/list.json"
$ProjectList = Get-Content "/list.json" -Raw | ConvertFromJson | select -
Expand value | Export-CSV "list.csv"
I also tried using invoke-webrequest it also returns nothing
Thank you
Upvotes: 0
Views: 1217
Reputation: 76670
Azure devops rest api returns only 100 projects
The answer is use a specify $top
parameter.
But when using this parameter in powershell, we need to be careful to escape the $
by ` instead of &.
That's because in powershell, single quotation ''
marks only represent the characters within the quotation marks in any case. In other words, the content within single quotes will not be substituted for variables and escaped characters. In double quotes ""
, variable substitution and character escaping are allowed.
When you use the double quotes with the BaseUrl
:
$BaseUrl = "https://dev.azure.com/{organization_name}/_apis/projects?api-versions=5.0"
We need escape character $
if we add $top=300
instead of &
in that URL.
So, the BaseUrl
should be:
$BaseUrl = "https://dev.azure.com/{organization_name}/_apis/projects?`$top=300&api-version=5.0"
Or you could use single quotation for the URL:
$BaseUrl = 'https://dev.azure.com/{organization_name}/_apis/projects?$top=300&api-version=5.0'
Check the document Escaping in PowerShell:
The PowerShell escape character is the backtick "`" character.
Below is my test result:
Upvotes: 3