Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / PowerShell

Using a common.js/require-like Approach in powershell

5.00/5 (2 votes)
2 Mar 2014CPOL3 min read 6.8K   7  
Registering your powershell modules on demand with PSReg

Introduction

The more and more I do with powershell, the more scripts I have on a number of machines that are not connected with each other. So I came to write the same scripts over and over again, sending them to me by email, remembering them and forgetting them, until finally I thought "now that's it, I want that t ".

Beside coding in languages like C++, C# and Java, I've used Ruby and node.js. Both have a nice thing that allow you to get the modules you need from an external website. If I had to work with powershell (and it's in my current job description) I wanted something like that as well.

Using the Code

First let's install the client. It's a powershell script you can find on github (https://github.com/MatthiasKainer/PSReg), and you can install it with the following line in the cmd:

@powershell -NoProfile -ExecutionPolicy unrestricted 
-Command "iex ((new-object net.webclient).DownloadString('http://psreg.net/install.ps1'))" 

Now when you open the powershell, all you have to do is register one of the available repositories like so:

register PSColorWriter 

The module will be downloaded from psreg.net and automatically registered (dot-loaded if a scriptfile but without exposing the variable to environment:global, and import-moduled for a module).

Finally, you can use the cmdlets the module has:

Write-Output-Color -Red "Look " -White "I have colors"  

What Else Can the Script Do?

Add More Sources

You can add more repository servers or local folders. The latter is especially handy, because you might have a lot of components that are only on your machine and not on a PSRegistration-Server. You can add these files as well by adding a path as location. Let's try that.

If you look in the directory, you can see the following subdirectories:

PS>ls C:\Projects\SMP\Production\SetupTesting | select Name

Name
----
Monitoring
Msmq

Looking inside, we can see there are files inside the directory:

PS>cd .\Msmq
PS>ls | select Name
Name
----
msmq-tools.ps1
msmq-helpers.ps1 

Since we have added the location to the PSRegistration, we can add both files easily like this:

register msmq

If we'd want to import only the msmq-tools file, we would write:

register msmq-tools

Sandbox

Occasionally, you may want a clean powershell. Type:

sandbox

and you're good to go. All registered locations are copied and PSReg is loaded for you.

List All Available Repositories

You can do this by calling:

list-available-repositories 

or by searching on psreg.net.

Adding Your Own Modules

I like OpenSource, and I like github. So I force everybody to do first and use latter. Once you have added your powershell module to github (or saw another interesting module on github that you'd like to have), go to http://psreg.net/, click on "add from github", insert the URL from the repo (i.e. "https://github.com/MatthiasKainer/PSReg") and you are good to go.

If you update your repo, you will have to add the same repository again - it will create a new version for you.

Wait, Did You Say "Version"?

Yep, I did. This thing theoretically supports version. You can see it in the web-app (http://psreg.net/repos/PSColorWriter/0.0.0.4 vs http://psreg.net/repos/PSColorWriter/0.0.0.3), but the powershell script does not. It's on the screen and will be done soon (I hope).

I Want My Own Server

Feel free to create one. The current server apps on psreg are written in node.js, and are ugly beyond imagination. If I ever come to refactor it I will OpenSource it, but right now this piece of software would completely destroy my reputation.

To do your own server, just provide the same XMLs as psreg does. You can find the initial point here:

Each repo has a repo element and a URL, at the URL you can find the XML with all the files that belong to this repo. Easy as that; for small libraries you can just create static XML-files, then publish them.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)