Downloading the private PowerShell modules

Now you have built the PowerShell Modules, you need to download and update them from the machine you want to use them. Downloding of the modules can be done with

This blog is part of a series of blog about “Creating PowerShell modules with Azure DevOps.

Create a PAT

Use personal access tokens

Sign into your organization in Azure DevOps (https://dev.azure.com/{yourorganization})

From your home page, open your profile. Go to your security details.

Select + New Token.

Name your token, select the organization where you want to use the token, and then choose a lifespan for your token.

Select

When you’re done, make sure to copy the token. For your security, it won’t be shown again. Use this token as your password and past it into the script.

Source Location (PacLoc)

You need to retrieve the PacLoc or Source location of the packages.

The easiest way to do this is:

  • Select “Artifacts
  • Select “Connect to feed
  • Select “NuGet.exe

[!WARNING]
Because we are using the command “Register-PackageSource” only V2 source locations are allowed.

So we need to recreate the source location from:
https://pkgs.dev.azure.com/TheAzCloudNinja/_packaging/Test/nuget/v3/index.json
to:
https://pkgs.dev.azure.com/TheAzCloudNinja/_packaging/Test/nuget/v2/

#Construct password
$PATToken = "PAT"
$PacLoc = "https://pkgs.dev.azure.com/TheAzCloudNinja/_packaging/Test/nuget/v2/"
$password = ConvertTo-SecureString $PATToken -AsPlainText -Force
$credsVSTS = New-Object System.Management.Automation.PSCredential $PATToken, $password
$RepoName = 'TheAzCloudNinja'

# Hashtable with parameters for splatting
$Params = @{
    'Name' = $RepoName
    'InstallationPolicy' = 'Trusted'
    'SourceLocation' = $PacLoc
    'PublishLocation' = $PacLoc
}

Unregister-PSRepository $RepoName -ErrorAction SilentlyContinue

Register-PSRepository @Params -Credential $credsVSTS

#Install all modules
$Modules = Find-Module -Credential $credsVSTS -Repository $RepoName

foreach ($Module in $Modules) {

    #Remove the module from OS
    Get-Module $Module.Name | Uninstall-Module -Force

    #Install module from repo
    Find-Module -Name $Module.Name -Credential $credsVSTS -Repository $RepoName | Install-Module -Credential $credsVSTS -Verbose
}

Leave a Reply

Your email address will not be published. Required fields are marked *