In-Depth

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.

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.

[Click on image for larger view.] Figure 1.

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.

[Click on image for larger view.] Figure 2.

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.

[Click on image for larger view.] Figure 3.

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.

[Click on image for larger view.] Figure 4.

Once on this screen, select Tasks and then Add Permission.

[Click on image for larger view.] Figure 5.

Then add Anonymous as a Principal and choose the Publish Packages task.

[Click on image for larger view.] Figure 6.

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.

[Click on image for larger view.] Figure 7.

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

 

About the Author

Adam Bertram is a 20-year veteran of IT. He's an automation engineer, blogger, consultant, freelance writer, Pluralsight course author and content marketing advisor to multiple technology companies. Adam also founded the popular TechSnips e-learning platform. He mainly focuses on DevOps, system management and automation technologies, as well as various cloud platforms mostly in the Microsoft space. He is a Microsoft Cloud and Datacenter Management MVP who absorbs knowledge from the IT field and explains it in an easy-to-understand fashion. Catch up on Adam's articles at adamtheautomator.com, connect on LinkedIn or follow him on Twitter at @adbertram or the TechSnips Twitter account @techsnips_io.


Featured

comments powered by Disqus

Subscribe on YouTube