How to install azure devops extension from powershell script

57 Views Asked by At

I can't for the life of me understand what this api wan't from me ! I made a query to get the extensionId and publisherId from the cli. I connect with my tenantId because an admin PAT is not enough. Now he can't find it ?

function azLogin {
    param ( [string]$organization, [string]$tenant)

    az login --tenant $tenant
}


[string]$tenant = $args[0]
[string]$organization = $args[1]
[string]$extension = $args[2]
[string]$publisher = $args[3]

try {
    azLogin $organization $tenant

    $result = az devops extension search --search-query Timetracker | ConvertFrom-Json
    $extensionId = $result.extensionId
    $publisherId = $result.publisher.publisherId
    
    Write-Host $publisherId
    az devops extension install --extension-id $extensionId --publisher-id $publisherId

    if ($result.PSObject.Properties.Name -notcontains 'extensionId' -or $result.PSObject.Properties.Name -notcontains 'publisherId') {
        throw
    }
}
catch {
   Write-Error "##[ERROR] failed."
}

At this point it's supposed to install this the organisation but i get :

The requested extension 'ecbf0d3e-ca03-4972-88ad-edcd1cb4a69c.83b3c590-5cd3-495b-b401-b8b4ed6da230' doesn't exist.

How am i supposed to install an extension in my organisation using powershell and a pat only ?

2

There are 2 best solutions below

0
wenbo On BEST ANSWER

Use azure devops service rest api may make things simple if you use PAT, API REFER

Below example shows how to install azure devops extension SonarQube (you can choose any extension you want), just copy and replace your own userName, PAT, orgName

function Get-AuthHeaders {
    [CmdletBinding()]
    param (
        [Parameter()]
        [string]$UserName,
        [string]$PersonalAccessToken
    )

    $basicAuth = ("{0}:{1}" -f $UserName, $PersonalAccessToken)
    $basicAuth = [System.Text.Encoding]::UTF8.GetBytes($basicAuth)
    $basicAuth = [System.Convert]::ToBase64String($basicAuth)
    $headers = @{Authorization = ("Basic {0}" -f $basicAuth) }
    return $headers
}
function Install-AdoExtension {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory)]
        [string]$OrganizationName,
        [Parameter(Mandatory)]
        [string]$PublisherName,
        [Parameter(Mandatory)]
        [string]$ExtensionName,
        [Parameter(Mandatory)]
        [string]$Version,
        [Parameter(Mandatory)]
        [hashtable]$Headers
    )

    $uri = "https://extmgmt.dev.azure.com/{0}/_apis/extensionmanagement/installedextensionsbyname/{1}/{2}/{3}?api-version=7.2-preview.1" -f $OrganizationName, $PublisherName, $ExtensionName, $Version


    $response = Invoke-RestMethod -Uri $uri -Method 'Post' -ContentType 'application/json' -Headers $headers -Body $body

    return $response
}
function List-AdoExtension {

    [CmdletBinding()]
    param (
        [Parameter(Mandatory)]
        [string]$OrganizationName,
        [Parameter(Mandatory)]
        [hashtable]$Headers
    )
    $uri = "https://extmgmt.dev.azure.com/{0}/_apis/extensionmanagement/installedextensions?api-version=7.2-preview.1" -f $OrganizationName
    $response = Invoke-RestMethod -Uri $uri -Method 'Get' -Headers $headers

    return $response
}

##
$userName            = "wbaccesstoken20250228"
$personalAccessToken = "xxxxxxx"
$orgName = "yourOrgName"

$extensionName = 'SonarQube'
$publisherName = 'SonarSource'
$version = '5.19.2'

###
$headers = Get-AuthHeaders -UserName $userName -PersonalAccessToken $personalAccessToken

Install-AdoExtension -OrganizationName $orgName -PublisherName $publisherName -ExtensionName $extensionName -Version $version -Headers $headers

List-AdoExtension -OrganizationName $orgName -Headers $headers

my example result:

enter image description here

1
Bright Ran-MSFT On

In your PowerShell script, you do not specify the Azure DevOps organization which the extension is installed to. you can try to specify the organization to see if the issue can be fixed.

To specify the Azure DevOps organization, you can try with one of the below methods:

  1. Configure the default organization after the "az login" command.

    az login xxxx
    az devops configure --defaults organization=https://dev.azure.com/xxxx
    
  2. If you use "az login" command but not set the default organization, you need to specify the organization when calling the "az devops extension install" command.

    az devops extension install --extension-id xxxx --publisher-id xxxx --org https://dev.azure.com/xxxx
    
  3. Directly login using the "az devops login".

    az devops login --org https://dev.azure.com/xxxx