Welcome to Day 19 of the “100 Days of DevOps with PowerShell”! For background on our goals in this series, see Announcing the “100 Days of Devops with PowerShell” Series here at SCC.
Disclaimer: The code below is from PowerShell 5.0 July CTP release and could change or break when RTM is released. I’ll endeavour to update this article where applicable.
In a Day 14 of this series we looked at how to discover and install modules using PowerShellGet and we saw that by default the module is set to use a Microsoft resource gallery at https://msconfiggallery.cloudapp.net/. After finding how easy it is to discover and install modules with PowerShellGet, the obvious question is how can I create my own repositories? If you have read Day 4 or Day 11, you will probably have noticed that the repositories for OneGet or PowerShellGet are based on NuGet package managers and therefore you need to create your own NuGet server. There are a couple of options here and both are fairly straight forward. You can either host your own NuGet server or create one online, and in this article we will focus on creating your online PowerShellGet repository.
Creating Your Own Online Repository
There are a number of providers to choose from and the list includes:
All offer free online trails and in this example we are going to use MyGet. After signing up, the first step is to create a feed which is going to be the URL to either find or publish your modules.
Clicking on Create a feed, you then need to provide the name, description and type of feed i.e. public, community or private. Once you’ve filled out the necessary details, click Create feed.
Add the New Repository to PowerShellGet
In this example my NuGet feed is https://www.myget.org/F/sccpowershell, but before I can use this with PowerShellGet I need to make some minor changes to update to the new NuGet feed URL. In the latest CTP released in July (x64 or x86), the number of PowerShellGet modules increased from the original four cmdlets to include three more cmdlets for managing the module source:
To update PowerShellGet to the new URL, lets try unregistering the the existing source
Unregister-ModuleSource –Name PSGallery
Unregister-ModuleSource : The module source ‘PSGallery’ cannot be unregistered.
No luck there, let’s try registering a new module source:
Register-ModuleSource -name SCCGallery -Location 'https://www.myget.org/F/sccpowershell/api/v2'
Now when we run Get-ModuleSource we have two source locations!
So it would appear that it’s not possible to unregister the default module source by Microsoft.
Publishing a Module to Your New Feed
With the additional module source, the next step is to publish a module to it. In this current CTP release it would appear in order to specify which feed the module should be published to you need to update the variable $PSGalleryPublishUri, but you must first load the module and then set the variable. The reason for this is that each time the module loads it will set this value back to the default URL.
Note: the format of the $PSGalleryPublishUri URL should be your NuGet feed URL and append “api/v2/package” . So in this case it will be ‘https://www.myget.org/F/sccpowershell/api/v2/package’
To publish a module you can either provide the name of the PowerShell module, assuming it’s located in one of the paths specified in $env:PSModulePath or you can provide a path to the module. In the example below I’m publishing a module called PowerShell Remote Registry which is already loaded on my machine and I can therefore just provide the name:
#Set the location where the module will be published
$PSGalleryPublishUri = 'https://www.myget.org/F/sccpowershell/api/v2/package'
#Publish the module - not the real API key!
Publish-Module -Name psrr -NuGetApiKey secret-c862-4125-9523-be42ef21f55a -Tags 'Remote Registry', 'Utils' -Verbose
Now we are able to search specific repositories for a module and here we can see the module we’ve just published:
SIDEBAR: The NuGetApiKey is available under Access Tokens in your profile in MyGet or under your feed details. Notice below that you can easily regenerate this key if you happen to publish it in plain text on the Internet!
Setting up your own PowerShellGet repository and publishing your modules is very straight forward and can be summarized in the steps below:
- Create your own online NuGet repository and feed
- Add the feed using Register-ModuleSource
- Update $PSGalleryPublishUri
- Publish your module using Publish-Module
I hope this has been useful and you can now start sharing PowerShell modules amongst your communities and organizations.
To see the previous installments in this series, visit “100 Days of DevOps with PowerShell”.