Mar 27

The 5 ways in which PowerShell can be used to make your work easier

PowerShell PowerShell PowerShell… It’s everywhere! You can’t read up on anything in Microsoft (or elsewhere) where it doesn’t talk about how PowerShell is a major part of the subject and yet, there are still lots of admins and engineers out there not using PowerShell. One response I got as to why this is the case was “My way still works, why should I change?” This response will probably come from someone who thinks the cloud is just a fad (Hint: It is not a fad).

I thought I would give a synopsis of the different ways in which you can use PowerShell.

#1 Automation

This you’ve probably heard, but it always bears repeating. PowerShell is an automation machine. Not only is it an automation machine, it does automation faster, easier and cleaner than anything previous (Sorry VBScripters… it true). Scripts I wrote in VBScript years ago I’ve since recreated in PowerShell in less than half the amount of lines. Of course you realize less code = less errors = less time spent working on those scripts. If you are not automating with PowerShell, you’re working too hard.

#2 Gathering Information

PowerShell is a fantastic tool for gathering information. Not only will it gather that information from multiple sources, it will put it all together and output it any way you want all with more than a single line of code. Your boss wants to work on something he or she things will be a huge undertaking? Gather the information with PowerShell and let your boss know it will or won’t be a huge undertaking in a few minutes. How many users have never logged on? How many Group Policies are not linked anywhere? How many Exchange mailboxes aren’t being used? Find this information easily with PowerShell. I don’t think your GUI will tell you this stuff.

#3 Reactive

Something just broke and it’s your job to find out what it was. I have a collection of little functions I call from a “Parent” function any time something goes wrong in my Exchange environment. It checks disk space, mail queues, running services to name a few. When someone comes into my office with a problem, the first thing I do is run my script. Any red flags are checked on and in some cases we’ve gotten back up an running in no time. As issues (and their resolutions) come up, I add it to my script. PowerShell is a building process you don’t have to have all the answers day 1. As you build your library, your downtime will shrink and everyone likes that.

#4 Proactive

Before anything goes wrong, before the red phone rings, be proactive with PowerShell. You can easily setup PowerShell to check your event logs on a nightly bases and send you (or your team) an email summary of events which may be of concern. Got System Center running? Add a PowerShell script which gathers get-process information when the Operations Manager says the system is out of memory (ok, that might be reactive, but getting that little bit of info in System Center is proactive). Found a recurring problem write a script to check things on a regular interval. DSC anyone?

#5 PowerShell working for PowerShell

This is the BEST! This is breaking down the fourth wall of PowerShell! This is where you make PowerShell REALLY work for you and itself. Here is an example: A while back I needed to write a script which would run on a nightly bases and make regular changes to hundreds of groups. I won’t go into details, but think of it as we gave instructions to hundreds of admins and this was our insurance policy that things were done right. Anyway, I’m working on the script, I add copious amounts of logging. One log for changes made, one for errors that occurred and one super verbose for me in case I needed to make a change. At the time there was potential that the changes this script would make would break user’s functionality. I started to think about that. What I did was when it came time to make the change, and the change was successful, I also wrote a command to a script file which reversed the change. Not only was I reactively automating the change, I was proactively writing the script which would reverse the change if there was ever a problem!

Here is another way PowerShell can make your life easier… I often need to gather messaging logs from Exchange. To do this, with a certain amount of specificity, I need to write a single line of PowerShell which is generally fairly long. It’s not much use to write a function for it because calling the function wouldn’t be much shorter than the original. So, I wrote an “Example module.” A module where I call various commands to get *My* examples for commands. All these functions do is write to the screen (color coordinated of course) my examples. All I need to do then is copy the one most like what I need, paste at the command line and make a few changes.

Windows PowerShell for Developers

There are dozens of other ways you can get PowerShell to work for you. Get “Windows PowerShell for Developers” by Douglas Finke (Microsoft MVP) if you really want to get an idea of the kinds of things you can do to make PowerShell work for you.

PowerShell is really an amazing tool. Its versatility lends itself to great creativity.

For those who haven’t started with PowerShell, don’t feel overwhelmed. PowerShell (like ANY language) is a building process. Don’t wory, you’ll get there and you will be so happy when you do.

If you know of other ways in which PowerShell can be used, I’d love to hear them.

Thanks for reading

Mar 20

Why you should make your PowerShell scripts publicly available

In my environment, I am the “PowerShell guy.” Just about everyone around me still does things the old fashion way and sometimes, I think they roll their eyes a little bit when I mention writing a script or doing it in PowerShell. Some people are coming around and they are learning with a bit of help from me (future post: Why you should teach at EVERY opportunity).

As a result of scripting in a bubble, I write a lot of scripts and functions for me and me alone. Because I know exactly how some piece of code works, I don’t really worry about all the help that should be in there, or worry about systems which are not setup the same as me. You know… I get sloppy! This is a bad thing.

The other day I participated in a short GitHub workshop which was just enough to get me interested and I’m hooked on the idea. Not only in the idea of having versioning and branching and all that great stuff, but putting *my* work, out “there.” Anyone can see what I put together. As a test, I created a repository for my “RBACHelper” module. This is a small module of a few functions which I find very helpful when working with Exchange Role Based Access Controls. Shortly after putting up the initial version (http://www.github.com/glaisne/RBACHelper) I realized there were large sections of commented code full of little tests I had in place while developing and there wasn’t much if any help information.

I quickly made some changes and updated my module because I was embarrassed by the less than professional appearance of my code. Now, if you are a person who writes code, hopefully you are the kind of coder who strives to be better all the time. Maybe we’ll get there, maybe the journey will never end. For me, making my code publicly available on a wider stage (outside my little blog here) will force me to be a better coder and not so disheveled.

I welcome the challenge, and I recommend you do too… if you want to be a better coder.