Of Mistakes and Modules
Recently I’ve been doing more with Powershell functions, and blissfully telling people to run the script and then the function is available to you. This is great if you use a Powershell ISE, either the built in one or a third-party one like PowerGUI. ISE’s will keep functions alive after you’ve run your script, which is a great feature while you’re testing a new script. The shell, on the other hand, isn’t so forgiving. Run it there and it loads into memory and as soon as the script finishes it promptly is DUMPED from memory.
Oops, sorry about that!
Which segue’s nicely into a quick talk about modules. I won’t go into a deep dive on modules because, frankly, I’m new to them too. But one cool thing a module can do is load your script functions into memory and allow you to use them from the command line. Sound familiar?
The cmdlet we need here is shown here:
By default, on Windows 7, the module path will check c:\users\<username>\documents\Powershell\Modules but you can simply type the full pathname if you want. I’ll go back through my blog posts and update them to point to this post! Of course, you can’t just run Import-Module on your script, the file needs to be saved as a module. So rename your file and use a *.PSM1 extension.
That’s all you need to do!
While we’re here, I thought I’d mention PowerGUI too. PowerGUI is a free Powershell ISE (Integrated Scripting Environment) that will make your life as a Powershell scripter so much easier. It has full cmdlet help and properly help along with many other features. By far the most useful one is the runtime variable values pane. You can actually track how your different variable values change as the script progresses. This single feature will save you HOURS of scripting time. Highly recommended and free, so tough to beat.
Up until recently posts from me were pretty few and far between, and then suddenly in the past two months I’ve been doing a lot about Powershell script’s. I once heard a quote–and I really wish I could remember the actual percentages but I’ll throw something out there to make my point—that said something like if you learn a subject by being told you’ll retain about 20% of what you hear. If you actually do the task you’ll retain something like 50% of what you learned. But if you teach the subject you’ll retain something like 99% of what you learned. That’s why I started posting about my scripting adventures.
Powershell has been out for about 4 years now and is up to version 3 now (just released in fact). Unfortunately I didn’t really catch on until about 6 months ago. I actually wrote a pretty extensive Powershell script a couple of years ago (my first DFS Monitor with History) but I was still pretty much in love with vbScript then and preferred it as a language. Not sure what changed 6 months ago when I “got” Powershell, but I did. A couple of months I found the Hey Scripting Guy blog and enjoyed the writing style and progression of how he built scripts—as it often matched how I did things and also how I learn things. That led to the rebirth of this blog and the posts you’ve been seeing (all 2 of my faithful readers). It’s been a great experience for me and really pounds home the lessons of I’m trying to learn by making the scripts in the first place!
The best example of that is the Get-ServiceInfo script I recently wrote about. I was trying to convert a big vbScript to a little Powershell one and essentially failed. I was all ready to hang it up by writing the “So What?” heading when it hit me to make a function out of it and have it work like any other Powershell cmdlet. Suddenly that little bit of code became incredibly powerful and was a true eye-opener for me.
So I hope you enjoy and who knows, maybe I can even get readership up a little!!