azurepowershellazure-devopsazure-cliazure-cli2

How to install azure devops extension from powershell script


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 ?


Solution

  • 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