Quantcast
Channel: TechNet Blogs
Viewing all articles
Browse latest Browse all 34890

PowerShell–Working with Internal Repositories

$
0
0

 

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 and I will 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.

 

Read up on Package Management Providers in Windows 10

 

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 gethub called PSPrivateGallery.

NuGet.Server

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

image

 

In PowerShell Add the Repository to the list of repo’s

Register-PSRepository –Name PSPrivateGallery –SourceLocation "http://nugetreposervername:8080/api/v2" –InstallationPolicy Trusted`
    –PackageManagementProvider NuGet -ScriptSourceLocation http://nugetreposervername:8080/api/v2


#validate
get-psrepository

clip_image001[10]

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:dataget-allservices.ps1 -version 1.0 -Author "chad.cox@microsoft.com" -Description "my first upload to the repo"
#edit the script
psedit C:dataget-allservices.ps1

clip_image001

clip_image002

Add cmdlets, Save and close the file

Validate the script by running:

Test-ScriptFileInfo -Path C:dataget-allservices.ps1

clip_image001[4]

Publish the script.

$nugetkey = Place key here
Publish-Script -Path C:dataget-allservices.ps1 -Repository "PSPrivateGallery" -NuGetApiKey $nugetkey

Check and see if the script was published

find-module get-allservices

clip_image001[6]

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:dataget-allservices.ps1 -version 2.0

Publish the modifications to the repo

$nugetkey = Place key here
Publish-Script -Path C:dataget-allservices.ps1 -Repository "PSPrivateGallery" -NuGetApiKey $nugetkey

Validate the versions

find-script get-allservices
#or all versions available
find-script get-allservices -AllVersions

clip_image001[8]

clip_image002[6]

Using the script

Make sure the repository is defined on the machine that needs the script. Using the register-psrepository cmdlet

clip_image001[12]

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:scriptsget-allservices.ps1

clip_image001[14]

clip_image002[8]

Just like that admins can now publish and version their scripts.

That is all I have for today

Chad Cox


Viewing all articles
Browse latest Browse all 34890

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>