AzureDevOps.Packaging/Private/Install-AzureDevOpsPackage.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
function Install-AzureDevOpsPackage
{
    <#
    .SYNOPSIS
    Installs a package from an Azure Artifacts feed.
 
    .DESCRIPTION
    Installs a package from an Azure Artifacts feed using the Azure Artifacts REST API.
 
    .PARAMETER Source
    The name or GUID of the package feed.
 
    .PARAMETER Provider
    The type of the feed. This can be "nuget", "npm", "maven", etc.
 
    .PARAMETER Name
    The name of the package to download.
 
    .PARAMETER Version
    The version of the package to download
 
    .PARAMETER OutFile
    The file name for the downloaded package.
 
    .EXAMPLE
    Install-AzureDevOpsPackage -Provider "nuget" -Source "myfeed" -Name "Contoso.Examples.Shared" -Version "1.1.0.22" -OutFile "Contoso.Examples.Shared.1.1.0.22.nupkg";
 
    Downloads a NuGet package.
 
    .EXAMPLE
    Install-AzureDevOpsPackage -Provider "npm" -Source "myfeed" -Name "contoso-example-shared" -Version "1.1.0" -OutFile "contoso-example-shared.1.1.0.tgz";
 
    Downloads an npm package.
    #>

    param
    (
        [string]$Source,
        [string]$Provider,
        [string]$Name,
        [string]$Version,
        [string]$OutFile
    )

    if(-not $AzureDevOpsAccount)
    {
        throw "Please run Add-AzureDevOpsAccount to connect to Azure DevOps.";
    }

    $organisationName = $AzureDevOpsAccount.OrganisationName;
    $authorizationHeader = $AzureDevOpsAccount.AuthorizationHeader;
    $header = @{Authorization = "Basic $authorizationHeader"};
    $uri = "https://$organisationName.pkgs.visualstudio.com/_apis/packaging/feeds/$source/$provider/packages/$name/versions/$version/content";

    Write-Verbose "Downloading $name $version from $provider feed '$source'...";
    Invoke-WebRequest -Uri $uri -Header $header -Method Get -OutFile $outFile;
    Write-Verbose "Downloaded $name $version into $outFile";
}