PowerShell: Working with NuGet Repositories to Store Scripts
One thing that seems to interest even the most advance PowerShell user is the concept of having an Internal Repository of scripts for their team to share and have version control over. I will say for a Sys Admin this has been a very challenging experience in figuring out what to use and I want to share what I have learned. Starting in Windows PowerShell 5.0 Microsoft includes the PowerShellGet module (Powershell Gallery for more info) which allows users to download new modules and scripts from the online PowerShell Gallery. These modules also allow users to create their own internal gallery and store custom scripts or modules within it. In future post I will probably cover more info on package management.
Setting up the repo
Since Windows includes the NuGet provider it makes sense to set up a NuGet Server. Most results from online searches require Visual Studio to do a NuGet.server install. I decided I didn’t want to go this route and found a few solutions available online and I ended up using an open source demo solution on github called PSPrivateGallery.
Getting started once the repo is online
After I got PSPrivateGallery installed and configured I decided to give it a try.
I updated to the newest version of PowerShellGet, at the time I wrote this version 1.1.3.2 was available in the PowerShell Gallery.
I had issues with the update-scriptfileinfo cmdlet until I updated it
find-module PowerShellGet
find-module PowerShellGet | install-module
remove-module PowerShellGet
Import-Module PowerShellGet
In PowerShell Add the Repository to the list of repo’s
Register-PSRepository –Name PSPrivateGallery –SourceLocation "https://nugetreposervername:8080/api/v2" –InstallationPolicy Trusted`
–PackageManagementProvider NuGet -ScriptSourceLocation https://nugetreposervername:8080/api/v2
#validate
get-psrepository
Now create a new script using the New-ScriptFileInfo cmdlet. This sets the metadata for a new script.
#create new script file
New-ScriptFileInfo C:\data\get-allservices.ps1 -version 1.0 -Author "chad.cox@microsoft.com" -Description "my first upload to the repo"
#edit the script
psedit C:\data\get-allservices.ps1
Add cmdlets, Save and close the file
Validate the script by running:
Test-ScriptFileInfo -Path C:\data\get-allservices.ps1
Publish the script.
$nugetkey = Place key here
Publish-Script -Path C:\data\get-allservices.ps1 -Repository "PSPrivateGallery" -NuGetApiKey $nugetkey
Check and see if the script was published
find-module get-allservices
Version 1.0 is now published for my team to share. Now lets say I make modifications to that script. Once all the modifications are done I need to update the version number.
update-scriptfileinfo -path C:\data\get-allservices.ps1 -version 2.0
Publish the modifications to the repo
$nugetkey = Place key here
Publish-Script -Path C:\data\get-allservices.ps1 -Repository "PSPrivateGallery" -NuGetApiKey $nugetkey
Validate the versions
find-script get-allservices
#or all versions available
find-script get-allservices -AllVersions
Using the script
Make sure the repository is defined on the machine that needs the script. Using the register-psrepository cmdlet
Create a folder to download the script to and validate it is the current version.
new-item c:\scripts -ItemType directory
save-script get-allservices -Repository PSPrivateGallery -Path C:\scripts
Get-ChildItem C:\scripts
Test-ScriptFileInfo -path C:\scripts\get-allservices.ps1
Just like that admins can now publish and version their scripts.
That is all I have for today, thanks for reading and I hope you find this info useful.
Chad
Just a quick note, I use this blog as a way to share things I have discovered. Most of the time I’m in a rush to get it out and my grammar and spelling is horrific. I usually proof read after I post and try to fix things as I see them.