ash_c
ash_c

Reputation: 31

How to list information using AZURE cli commands in azure pipeline

I'm trying to make a PowerShell script containing Azure CLI commands to retrieve the information about the repos, I'm new to azure DevOps.

Information that I'm trying to gather is the list of repos, their branches, the latest commits, and the author.

I've gone through the AZ Repos docs but I'm not able to execute it properly, here's the code I've done so far.

$orgUrl = "https://dev.azure.com/{MyOrganization}"

$ProjectUrl = "https://dev.azure.com/{MyOrganization}/_apis/git/repositories?api-verepo_demo/sion=4.1"

$env:SYSTEM_ACCESSTOKEN | az devops login --org $org --project $ProjectUrl

foreach ($Proj in $ProjectUrl) 
{

    $Repos = az repos list --organization $orgUrl --project $Proj | ConvertFrom-Json

    foreach ($Repo in $Repos) {

        Write-Output $Repo.webUrl
    }

Upvotes: 2

Views: 1331

Answers (1)

jessehouwing
jessehouwing

Reputation: 114651

Looks like you're getting there... But there are a few things pretty wrong in the code you pasted, I'm guessing the code is incomplete.

$orgUrl = "https://dev.azure.com/{MyOrganization}"

# there is an error in the project url below, it should contain the project name. Plus, you need to pass the project name, not the full URI.
$ProjectUrl = "https://dev.azure.com/{MyOrganization}/_apis/git/repositories?api-verepo_demo/sion=4.1"

$env:SYSTEM_ACCESSTOKEN | az devops login --org $org --project $ProjectUrl

# you can't for each through a url. I assume you had some az DevOps list command here previously.

foreach ($Proj in $ProjectUrl) 
{

    # you should probably enforce json output here.
    $Repos = az repos list --organization $orgUrl --project $Proj | ConvertFrom-Json

    foreach ($Repo in $Repos) {

        Write-Output $Repo.webUrl
    }
}

If you're only querying a single project, you can simply hardcode the project name:

$orgUrl = "https://dev.azure.com/{MyOrganization}"
$project = "demo"

$env:SYSTEM_ACCESSTOKEN | az devops login --org $org --project $project

$Repos = az repos list --organization $orgUrl --project $project -o json | ConvertFrom-Json

foreach ($Repo in $Repos) {
    Write-Output $Repo.webUrl
}

Upvotes: 3

Related Questions