Use an Azure Artifacts feed as a private PowerShell repository

Azure DevOps Services

Azure Artifacts provides an easy way to share PowerShell scripts across teams to promote collaboration and maximize effectiveness. By storing PowerShell modules in a private repository, you can give members of your team the ability to download or update those scripts quickly using the command line.

This article will guide you through setting up your Azure Artifacts feed as a private PowerShell repository to store and share your PowerShell modules. You'll learn how to:

  • Create a Personal Access Token
  • Create a new feed to store PowerShell modules
  • Create, package, and publish PowerShell modules
  • Connect to a feed with PowerShell
  • Use the private PowerShell repository with Azure Pipelines

Prerequisites

Create a personal access token

Using a personal access token (PAT) is a great way to authenticate with Azure DevOps without using your primary credentials. See Use personal access tokens for more details.

  1. Navigate to your Azure DevOps organization https://dev.azure.com/<ORGANIZATION_NAME>/

  2. Select the user settings icon, and then select Personal access tokens.

    A screenshot showing how to create a personal access token.

  3. Select New Token

  4. Enter a name for your PAT and then choose an Expiration date.

  5. Select Custom defined, and then select Packaging > Read, write & manage.

  6. Select Create when you are done. Copy and store your PAT in a safe location.

    A screenshot showing how to set up a new personal access token.

Create a module

  1. Create a new folder Get-Hello. Navigate inside your folder and create a new file Get-Hello.psm1.

    |--- Get-Hello               // Parent folder     
        |--- Get-Hello.psm1     // This will become our PowerShell Module
        |--- Get-Hello.psd1    // This will become our module manifest
    
  2. Paste the following script into your Get-Hello.psm1 file:

    Function Get-Hello{
        Write-Host "Hello from my Azure DevOps Services Package."
    }
    
  3. Create the module manifest by running the following command in your Get-Hello directory path.

    New-ModuleManifest -Path .\Get-Hello.psd1
    
  4. Open your Get-Hello.psd1 file and find the RootModule variable. Replace the empty string with the path to your Get-Hello.psm1 file as follows:

    RootModule = 'Get-Hello.psm1'
    
  5. The FunctionsToExport section is meant to define the list of functions that will be exported from this module. Add your Get-Hello function as follows:

    FunctionsToExport = @('Get-Hello')
    
  6. Find the FileList section, and add the following list of files that should be packaged with your module.

    FileList = @('PSModule.psm1',
                 'PSGet.Format.ps1xml',
                 'PSGet.Resource.psd1')
    

Pack and publish module

  1. Create a .nuspec file for your module. This command will create a Get-Hello.nuspec file that contains metadata needed to pack the module.

    nuget spec Get-Hello
    
  2. Run the following command to pack your module.

    nuget pack Get-Hello.nuspec
    
  3. Run the following command to add your feed source URL.

    nuget sources Add -Name "<FEED_NAME>" -Source "https://pkgs.dev.azure.com/<ORGANIZATION_NAME>/_packaging/<FEED_NAME>/nuget/v3/index.json" -username "<USER_NAME>" -password "<PERSONAL_ACCESS_TOKEN>"
    
  4. Publish the package to your feed.

    nuget push -Source "<FEED_NAME>" -ApiKey "<ANY_STRING>" "<PACKAGE_PATH>"
    

    A screenshot showing the published package.

Note

The version number in the Module Manifest (.psd1) and the .nuspec file must match.

Connect to feed as a PowerShell repository

Important

PowerShell does not support version 3 of NuGet.

  1. Open an elevated PowerShell prompt window.

  2. Set up your credentials to authenticate with Azure Artifacts. Replace the placeholders with the appropriate information.

    $patToken = "<PERSONAL_ACCESS_TOKEN>" | ConvertTo-SecureString -AsPlainText -Force
    
    $credsAzureDevopsServices = New-Object System.Management.Automation.PSCredential("<USER_NAME>", $patToken)
    
  3. Register your PowerShell repository. The SourceLocation link can be found by navigating to Artifacts > Connect to Feed > NuGet.exe under Project setup source URL.

    • Project-scoped feed:
    Register-PSRepository -Name "PowershellAzureDevopsServices" -SourceLocation "https://pkgs.dev.azure.com/<ORGANIZATION_NAME>/<PROJECT_NAME>/_packaging/<FEED_NAME>/nuget/v2" -PublishLocation "https://pkgs.dev.azure.com/<ORGANIZATION_NAME>/<PROJECT_NAME>/_packaging/<FEED_NAME>/nuget/v2" -InstallationPolicy Trusted -Credential $credsAzureDevopsServices
    
    • Org-scoped feed:
    Register-PSRepository -Name "PowershellAzureDevopsServices" -SourceLocation "https://pkgs.dev.azure.com/<ORGANIZATION_NAME>/_packaging/<FEED_NAME>/nuget/v2" -PublishLocation "https://pkgs.dev.azure.com/<ORGANIZATION_NAME>/_packaging/<FEED_NAME>/nuget/v2" -InstallationPolicy Trusted -Credential $credsAzureDevopsServices
    

    If you're still using the older visualstudio.com URLs, use the following command instead:

    • Project-scoped feed:
    Register-PSRepository -Name "PowershellAzureDevopsServices" -SourceLocation "https://<ORGANIZATION_NAME>.pkgs.visualstudio.com/<PROJECT_NAME>/_packaging/<FEED_NAME>/nuget/v2" -PublishLocation "https://<ORGANIZATION_NAME>.pkgs.visualstudio.com/<PROJECT_NAME>/_packaging/<FEED_NAME>/nuget/v2" -InstallationPolicy Trusted -Credential $credsAzureDevopsServices
    
    • Org-scoped feed:
    Register-PSRepository -Name "PowershellAzureDevopsServices" -SourceLocation "https://<ORGANIZATION_NAME>.pkgs.visualstudio.com/_packaging/<FEED_NAME>/nuget/v2" -PublishLocation "https://<ORGANIZATION_NAME>.pkgs.visualstudio.com/_packaging/<FEED_NAME>/nuget/v2" -InstallationPolicy Trusted -Credential $credsAzureDevopsServices
    

    Tip

    If you encounter the following warning Unable to resolve package source, restart your PowerShell session.

  4. Register your package source:

    • Project-scoped feed:
    Register-PackageSource -Name "PowershellAzureDevopsServices" -Location "https://pkgs.dev.azure.com/<ORGANIZATION_NAME>/<PROJECT_NAME>/_packaging/<FEED_NAME>/nuget/v2" -ProviderName NuGet -Trusted -SkipValidate -Credential $credsAzureDevopsServices
    
    • Org-scoped feed:
    Register-PackageSource -Name "PowershellAzureDevopsServices" -Location "https://pkgs.dev.azure.com/<ORGANIZATION_NAME>/_packaging/<FEED_NAME>/nuget/v2" -ProviderName NuGet -Trusted -SkipValidate -Credential $credsAzureDevopsServices 
    
  5. Run the following command to confirm if the repository was registered successfully. This command gets all the registered repositories for the current user:

    Get-PSRepository
    
  6. Run the following command if you want to find all modules in the repository.

    Find-Module -Repository PowershellAzureDevopsServices
    
  7. Run the following command if you want to install the Get-Hello module.

    Install-Module -Name Get-Hello -Repository PowershellAzureDevopsServices
    

If the Install-Module command is returning the following error: Unable to resolve package source, run the Register-PackageSource cmdlet again with the Trusted flag as follows:

Register-PackageSource -Name "PowershellAzureDevopsServices" -Location "https://pkgs.dev.azure.com/<ORGANIZATION_NAME>/_packaging/<FEED_NAME>/nuget/v2" -ProviderName NuGet -Trusted -Trusted -SkipValidate -Credential $credsAzureDevopsServices

Connect to feed with Azure Pipelines

The following example shows how to authenticate and install a PowerShell Module with YAML pipeline.

variables:
  PackageFeedEndpoint: https://pkgs.dev.azure.com/<ORGANIZATION_NAME>/_packaging/<FEED_NAME>/nuget/v2 # For Project-scoped feed use this endpoint url: https://pkgs.dev.azure.com/<ORGANIZATION_NAME>/<PROJECT_NAME>/_packaging/<FEED_NAME>/nuget/v2 
  
  # Construct a JSON object that contains the endpoint URL and the personal access token to pass them to Azure Artifacts credential provider. 
  PackageFeedEndpointCredential: '{"endpointCredentials": [{"endpoint":"$(PackageFeedEndpoint)", "username":"OPTIONAL", "password":"ACCESS TOKEN"}]}'
  
steps:
  # To prevent possible 'Unable to resolve package source' errors when installing modules from your feed, call Install-Module in a separate PowerShell task.
  - powershell: |
      Register-PSRepository -Name "PowershellAzureDevopsServices" -SourceLocation "$(PackageFeedEndpoint)" -PublishLocation "$(PackageFeedEndpoint)" -InstallationPolicy Trusted
    displayName: 'Register Azure Artifacts Feed as PSRepository'
    env:
      # This environment variable passes the credentials to the credential provider.
      VSS_NUGET_EXTERNAL_FEED_ENDPOINTS: $(PackageFeedEndpointCredential)
      
  - powershell: |
      Install-Module -Name Get-Hello -Repository PowershellAzureDevopsServices
    displayName: 'Install Get-Hello PowerShell module'
    env:
      # The credentials must be set on every task that interacts with your private PowerShell repository.
      VSS_NUGET_EXTERNAL_FEED_ENDPOINTS: $(PackageFeedEndpointCredential)
      
  - powershell: |
        Get-Hello
    displayName: Execute Get-Hello