Home > Software engineering >  Getting Azure DevOps repository files with PowerShell and Invoke-RestMethod
Getting Azure DevOps repository files with PowerShell and Invoke-RestMethod

Time:09-28

Trying to see how I could make GET requests to get the content of the Postman collection files from the Azure DevOps repositories using Powershell. This is as far as I have gotten. It just returns me the page with the following:

Invoke-RestMethod : 
  
    Page not found.
    html {

The code is as follows:

$personalToken = "***"
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($personalToken)"))
$header = @{authorization="Basic $token"}
$projectUrl ="https://dev.azure.com/***/***/_git/PostmanCollections/PostmanFunctionalTest/PostmanFunctionalTest.postman_collection.json"
$content = Invoke-RestMethod -Uri $projectUrl -Method GET -contentType "application/json" -Headers $header
$content

What is wrong here? Much appreciated!

CodePudding user response:

You can use the Rest API: Items - Get to get the file content of the Repo.

Here is PowerShell example:

$token = "PAT"

$url=" https://dev.azure.com/{ORG}/{PPROJECT}/_apis/git/repositories/{Reponame}/items?recursionLevel=0&versionDescriptor.version={Branchname}&versionDescriptor.versionOptions=0&versionDescriptor.versionType=Branch&includeContent=true&resolveLfs=true&path={filepath}&6.1-preview.1"

$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))

$response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method Get  -ContentType application/json

Write-Host "$response"

Update:

$token = "PAT"

$url=" https://dev.azure.com/{ORG}/{PPROJECT}/_apis/git/repositories/{Reponame}/items?recursionLevel=0&versionDescriptor.version={Branchname}&versionDescriptor.versionOptions=0&versionDescriptor.versionType=Branch&includeContent=true&resolveLfs=true&path={filepath}&6.1-preview.1"

$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))

$response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method Get  -ContentType application/json | ConvertTo-Json -Depth 99


Write-Host "$response"
  • Related