How To Set Up an Internal NuGet Repository in PowerShell
Here's a simple way for an organization to stay in control of an ever-growing library of resources.
- By Adam Bertram
As an organization grows larger, the need for a centralized repository of software and PowerShell modules will increase. Eventually, it becomes cumbersome to access resources from a file share. This is when it's time for a change. This is when it's time to begin looking into a NuGet feed. NuGet is a technology that was initially developed for .NET developers. It allowed them to create feeds so that they could publish and download their compiled applications. However, NuGet has caught on in the IT space because of PowerShell's reliance on NuGet with the PowerShellGet offering.
In this article, I'm going to show you an example of how to set up an internal NuGet repository to distribute PowerShell modules and software packages from. There are a few different ways to do this, such as rolling your own NuGet feed, setting up an internal PowerShell Gallery or using an off-the-shelf product. In order to get up and going quickly, I've chosen to use ProGet -- a free tool that's done all of the hard work already.
First, download ProGet onto a Windows server in your environment. ProGet needs a Web server (but includes an integrated one that we'll be using). If you'd like to use IIS, go ahead and install one now before installing ProGet. Run the ProGet installer and accept all of the defaults. This step is pretty simple. For this demonstration, I'll be using the integrated Web server running on port 81.
Once the database is running on SQL Server Express and the integrated Web server has been installed you should now have a Web site running on port 81.
I'd like to set up a NuGet feed to distribute my PowerShell modules, so I'm going to delete the Default feed and create a new one. But, before I can do that, I'll need to log in. Click on the person icon in the upper-right hand corner and click Log In. You'll then be prompted to log in with the default Admin account.
Once logged in, click on the Default feed --> Manage Feed --> Delete Feed.
This should take you back to the Feeds page. Now, click on Create New Feed. Since I'm going to set up a feed to share PowerShell modules, I'll choose the PowerShell feed.
The feed should now be created. At this point, I need to upload some modules to the feed. To do this requires setting an API key on the feed. I'll do this by clicking on the feed and clicking change besides the NuGet API Key row and setting a key.
Because, by default, ProGet is not configured to allow anonymous users the rights to publish modules and I'd like to do that, I'll now give anonymous users that right by giving them the Publish Packages task right. To do that, click on the gear icon in the upper right corner and select Manage Users & Tasks.
Once on this screen, select Tasks and then Add Permission.
Then add Anonymous as a Principal and choose the Publish Packages task.
Once this is done, I now need to configure the client that will be uploading PowerShell modules. Since I'll be using the Publish-Module command, I'll need to first set up a PS Repository for our private NuGet feed. To do that, I'll use the Register-PSRepository command and specify the API Endpoint URL fo r the feed.
Register-PSRepository -Name InternalPowerShellModules -SourceLocation http://nugetserver:81/nuget/PowerShellModules -PackageManagementProvider NuGet -PublishLocation http://nugetserver:81/nuget/PowerShellModules -InstallationPolicy Trusted
I can now upload a module using the Publish-Module command. This command has a requirement of having a module manifest and having both the Description and Author manifest fields used so ensure both of those fields are populated first. Then, run Publish-Module.
Publish-Module -Name MyModule -Repository InternalPowerShellModules -NuGetApiKey secret
You should then see the package available on your feed.
Once the module is available in the feed, use Find-Module to discover it with PowerShell and Install-Module to get it installed!
Find-Module -Name MyModule -Repository InternalPowerShellModules | Install-Module
Adam Bertram is an independent consultant, technical writer, trainer and presenter. Adam specializes in consulting and evangelizing all things IT automation mainly focused around Windows PowerShell. Adam is a Microsoft Windows PowerShell MVP, 2015 powershell.org PowerShell hero and has numerous Microsoft IT pro certifications. He is a writer, trainer and presenter and authors IT pro course content for Pluralsight. He is also a regular contributor to numerous print and online publications and presents at various user groups and conferences. You can find Adam at adamtheautomator.com or on Twitter at @adbertram.