title | description | ms.service | ms.author | author | ms.date | monikerRange | recommendations |
---|---|---|---|---|---|---|---|
Use an Azure Artifacts feed as a private PowerShell repository |
Learn how to use an Azure Artifacts feed as a private PowerShell repository |
azure-devops-artifacts |
rabououn |
ramiMSFT |
07/03/2024 |
azure-devops |
true |
[!INCLUDE version-eq-azure-devops]
Azure Artifacts provides a convenient solution for sharing PowerShell scripts. By using Azure Artifacts feeds, you can seamlessly publish your PowerShell modules from the command line and control access to them through your feed settings. 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:
[!div class="checklist"]
- Create a Personal Access Token
- Create, package, and publish a PowerShell module
- Connect to an Azure Artifact feed
- Register and install a PowerShell module using Azure Pipelines
-
Create an Azure DevOps organization and a project if you haven't already.
-
Create a new feed if you don't have one already.
-
Install the Azure Artifacts Credential Provider.
-
Install NuGet.
A personal access token acts as your digital identity and serves as an alternative password to authenticate you with Azure DevOps.
-
Navigate to your Azure DevOps organization
https://dev.azure.com/<ORGANIZATION_NAME>/
-
Select the user settings icon, select Personal access tokens, and then select New Token.
-
Enter a name for your PAT, set an Expiration date, select Custom defined, and then select Packaging > Read, write & manage.
-
Select Create when you're done, and make sure you copy and store your PAT in a safe location.
:::image type="content" source="../media/config-new-pat.png" alt-text="A screenshot that shows how to set up a new personal access token.":::
If you don't have your own module, follow the instructions in this section to create a sample PowerShell module. Otherwise, skip to the next step:
-
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
-
Paste the following script into your Get-Hello.psm1 file:
Function Get-Hello{ Write-Host "Hello from my Azure DevOps Services Package." }
-
Create the module manifest by running the following command in your Get-Hello directory path.
New-ModuleManifest -Path .\Get-Hello.psd1
-
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'
-
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')
-
Find the
FileList
section, and add the following list of files that should be packaged with your module.FileList = @('./Get-Hello.psm1')
-
Create 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
-
Run the following command to pack your module.
nuget pack Get-Hello.nuspec
-
Run the following command to add your feed source URL. NuGet v3 is not supported, make sure you use v2 in your feed source URL.
-
Org-scoped feed:
nuget sources Add -Name "<FEED_NAME>" -Source "https://pkgs.dev.azure.com/<ORGANIZATION_NAME>/_packaging/<FEED_NAME>/nuget/v2" -username "<USER_NAME>" -password "<PERSONAL_ACCESS_TOKEN>"
-
Project-scoped feed:
nuget sources Add -Name "<FEED_NAME>" -Source "https://pkgs.dev.azure.com/<ORGANIZATION_NAME>/<PROJECT_NAME>/_packaging/<FEED_NAME>/nuget/v2" -username "<USER_NAME>" -password "<PERSONAL_ACCESS_TOKEN>"
-
-
Publish the package to your feed.
nuget push -Source "<FEED_NAME>" -ApiKey "<ANY_STRING>" "<PACKAGE_PATH>"
:::image type="content" source="../../repos/git/media/artifact-package-powershell.png" alt-text="A screenshot showing the published package.":::
Important
The version number in your Module Manifest (.psd1) and the .nuspec file must match.
-
Open an elevated PowerShell prompt window.
-
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)
-
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] Certain versions of PowerShell require restarting a new session after executing the
Register-PSRepository
cmdlet to avoid the Unable to resolve package source warning. -
-
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
-
-
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
-
Run the following command if you want to find all modules in the repository.
Find-Module -Repository PowershellAzureDevopsServices
-
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
Note
If your organization is using a firewall or a proxy server, make sure you allow Azure Artifacts Domain URLs and IP addresses.
This example illustrates the process of authenticating and installing a PowerShell Module using a YAML pipeline. To use your personal access token within the pipeline, you should include it as a pipeline variable, as follows:
-
Sign in to your Azure DevOps organization, and then navigate to your project.
-
Select Pipelines, select your pipeline and then select Edit to edit your pipeline.
-
Select Variables at the top right corner, and then select the + sign to create a new variable.
-
Provide a Name for your variable, and then paste your personal access token in the Value textbox.
-
Make sure you select the Keep this value secret checkbox. Select Ok when you're done. You are now ready to use your variable in your pipeline.
trigger:
- main
pool:
vmImage: 'Windows-latest'
variables:
PackageFeedEndpoint: 'https://pkgs.dev.azure.com/<ORGANIZATION_NAME>/_packaging/<FEED_NAME>/nuget/v2' ## For project scoped feeds use: 'https://pkgs.dev.azure.com/<ORGANIZATION_NAME>/<PROJECT_NAME>/_packaging/<FEED_NAME>/nuget/v2'
PackageFeedEndpointCredential: '{"endpointCredentials": [{"endpoint":"$(PackageFeedEndpoint)", "username":"Admin", "password":"$(AZURE_DEVOPS_PAT)"}]}'
steps:
- powershell: |
Register-PSRepository -Name "psRepoPipeline" -SourceLocation '$(PackageFeedEndpoint)' -InstallationPolicy Trusted
displayName: 'Register Azure Artifacts Feed as PSRepository'
env:
VSS_NUGET_EXTERNAL_FEED_ENDPOINTS: $(PackageFeedEndpointCredential)
- powershell: |
echo (Get-PSRepository)
displayName: 'Get all module repositories'
- powershell: |
Find-Module -Name "Get-Hello" | Install-Module -Confirm:$false -Force
displayName: 'Install the Get-Hello PowerShell module'
env:
VSS_NUGET_EXTERNAL_FEED_ENDPOINTS: $(PackageFeedEndpointCredential)