Sunday, March 22, 2015

Significantly Improved Powershell Variable Namespace Script

So a while back I posted about a script I wrote to help me namespace variables and functions I was using for testing stuff in PowerShell, to keep them suitably separate from all the default variables present in PowerShell's Variable: PSDrive.

Well, since that post, I've significantly improved upon the script, enough so that it warrants a new post.  Also, rather than just copy/pasting the whole thing here, I've put it on pastebin.  I think that'll be my new policy for posting large sections of code that I've written, just to cut down on clutter here.  Plus, pastebin has syntax highlighting.  So there's that.

Anyway.  Pastebin link.  Right.

So, what's changed?

Well, it now acts more like a constructor.  Essentially, the script acts as a function, and returns the object it's created.  So now, rather than having to edit a few lines of code to change the name of the namespace, you can just assign the result of the script to a variable.  Do that in your profile script, and you're good to go.  Also, should you need the capability, you can now easily make more than one namespace.

As far as functionality, it's gained another member, _protected, which holds a bunch of stuff that the object uses internally.  Doing this removed the requirement for certain things to be hardcoded, making it easier to extend the whole thing.  This extra member is named like it is so that it hopefully won't collide with any names you actually want to use.

Also, I swapped the order of the arguments on the list() method, because it was backwards and that was bothering me.

Furthermore, there's now a giant documentation comment at the top, which means you can use Get-Help on the script and get all kinds of (hopefully) useful information.

Side note: Pastebin overzealously turned some strategically placed spaces in my indentation into tabs.  It even did it in the "raw paste data" box at the bottom.  Ugh.

No comments:

Post a Comment

I moderate comments because when Blogger originally implemented a spam filter it wouldn't work without comment moderation enabled. So if your comment doesn't show up right away, that would be why.