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 guides you through setting up your Azure Artifacts feed as a private PowerShell repository to store and share your PowerShell modules.
In this article, you'll learn how to:
[!div class="checklist"]
- Create a Personal Access Token
- Create, package, and publish a PowerShell module
- Connect to a feed as a PowerShell repository
- Register and install a PowerShell module using Azure Pipelines
::: zone pivot="PowerShellGet"
-
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 into your folder and create a new file Get-Hello.psm1.
-
Paste the following script into your Get-Hello.psm1 file:
Function Get-Hello{ Write-Host "Hello Azure DevOps!" }
-
Generate the module manifest by running the following command in your Get-Hello directory:
New-ModuleManifest -Path .\Get-Hello.psd1
-
Open your Get-Hello.psd1 file and locate the
RootModule
variable. This setting specifies the main script file that PowerShell loads when the module is imported. Replace the empty string with the path to your Get-Hello.psm1 file:RootModule = 'Get-Hello.psm1'
-
The
FunctionsToExport
section specifies which functions are accessible to users when they import your module. Include your Get-Hello function:FunctionsToExport = @('Get-Hello')
-
Find the
FileList
section, which specifies the files included when packaging the module. Add the file you wish to package with your module:FileList = @('./Get-Hello.psm1')
-
Generate a nuspec file for your module. This command creates a Get-Hello.nuspec file containing the necessary metadata for packing the module:
nuget spec Get-Hello
-
Run the following command to package your module:
nuget pack Get-Hello.nuspec
-
Run the following command to add your feed source URL. Make sure that you use V2 in your feed source URL, as NuGet V3 is not supported.
-
Organization-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>"
Important
The version number in your Module Manifest (.psd1) must be identical to the version number in your .nuspec file.
This section guides you through authenticating with a feed as a PowerShell repository and consuming a module hosted in your feed:
-
Run the following command in a PowerShell prompt window to set up your credentials for authenticating 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)
-
Run the following command to register your PowerShell repository. You can find the
SourceLocation
link by navigating to Artifacts > Connect to Feed > NuGet.exe, under the Project setup section > 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
-
Organization-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
[!TIP] Some versions of PowerShell may require starting a new session after running the
Register-PSRepository
cmdlet to prevent encountering the Unable to resolve package source warning. -
-
Run the following command to 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
-
Organization-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
-
-
To verify if the repository was successfully registered, run the following command to retrieve all registered repositories for the current user:
Get-PSRepository
-
Run the following command to install the Get-Hello module.
Install-Module -Name Get-Hello -Repository PowershellAzureDevopsServices
Note
If your organization uses a firewall or a proxy server, make sure that you allow access to Azure Artifacts Domain URLs and IP addresses.
This example guides you through the steps to authenticate with an Azure Artifacts feed and install a PowerShell Module from your pipeline. To use your personal access token within the pipeline, include it as a pipeline variable, as shown below:
-
Sign in to your Azure DevOps organization, and then navigate to your project.
-
Select Pipelines, select your pipeline definition, and then select Edit to modify your pipeline.
-
Select Variables at the top right corner, and then select New variable.
-
Enter a Name for your variable, and then paste your personal access token into the Value textbox.
-
Make sure that you select the Keep this value secret checkbox. Select Ok when you're done.
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)
:::zone-end
::: zone pivot="PSResourceGet"
-
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 PSResourceGet.
-
Install the SecretManagement and SecretStore modules.
Note
Azure Artifacts Credential Provider is not supported with PSResourceGet.
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 PowerShell-Demo. Navigate into your folder and create a new file PowerShell-Demo.psm1.
-
Paste the following script into your PowerShell-Demo.psm1 file:
Function PowerShell-Demo{ Write-Host "Hello World!" }
-
Generate the module manifest by running the following command in your PowerShell-Demo directory:
New-ModuleManifest -Path .\PowerShell-Demo.psd1
-
Open your PowerShell-Demo.psd1 file and locate the
RootModule
variable. This setting specifies the main script file that PowerShell loads when the module is imported. Replace the empty string with the path to your PowerShell-Demo.psm1 file:RootModule = 'PowerShell-Demo.psm1'
-
The
FunctionsToExport
section specifies which functions are accessible to users when they import your module. Include your PowerShell-Demo function:FunctionsToExport = @('PowerShell-Demo')
-
Locate the
FileList
section, which lists the files included when packaging the module. Add the file you wish to package with your module:FileList = @('./PowerShell-Demo.psm1')
-
Run the following command to create a credential object. Replace the placeholders with the correct information.
$username = "<USER_NAME>" $patToken = "<PERSONAL_ACCESS_TOKEN>" | ConvertTo-SecureString -AsPlainText -Force $credentials = New-Object System.Management.Automation.PSCredential($username, $patToken)
-
Ensure that SecretManagement and SecretStore are installed, then run the following command to create a vault and add a secret:
Register-SecretVault -Name "MySecretVault" -ModuleName Microsoft.PowerShell.SecretStore -DefaultVault Set-Secret -Name "MyNewCredential" -Secret $newCredentials -Vault "MySecretVault" $CredentialInfo = [Microsoft.PowerShell.PSResourceGet.UtilClasses.PSCredentialInfo]::new('MySecretVault', 'MyCredential')
-
Run the following command to register your PowerShell repository. You can find the
SourceLocation
link by navigating to Artifacts > Connect to Feed > NuGet.exe, under the Project setup section > source URL.-
Project-scoped feed:
Register-PSResourceRepository -Name "PowershellPSResourceRepository" ` -Uri "https://pkgs.dev.azure.com/<ORGANIZATION_NAME>/<PROJECT_NAME>/_packaging/<FEED_NAME>/nuget/v2" ` -Trusted ` -CredentialInfo $CredentialInfo
-
Organization-scoped feed:
Register-PSResourceRepository -Name "PowershellPSResourceRepository" ` -Uri "https://pkgs.dev.azure.com/<ORGANIZATION_NAME>/_packaging/<FEED_NAME>/nuget/v2" ` -Trusted ` -CredentialInfo $credentials
[!TIP] Some versions of PowerShell may require starting a new session after running the
Register-PSResourceRepository
cmdlet to prevent encountering the Unable to resolve package source warning. -
-
To verify if the repository was successfully registered, run the following command to retrieve all registered repositories for the current user:
Get-PSResourceRepository
Note
If you encounter the error: Response status code does not indicate success: 404 (Not Found)., make sure that
:::zone-end