Jan 13

PowerShell warnings with ASCII art

I thought I would share a little fun I had with some scripts I’ve written. The situation was that the script I was running was going to do something, which if not done correctly, or at the wrong time, would have disastrous consequences in a production environment. I didn’t want to use the standard warning tools provided with PowerShell because I didn’t want people mindlessly clicking the OK button and creating a LOT more work than would be necessary. My thought was to have a giant error which would take up the entire screen it would force people to pay attention.

Searching the internet I found this site (http://www.patorjk.com/software/taag/) which had ASCII art for text in a large array of different styles. For my purposes I liked “ANSI Shadow” using the word ‘WARNING’ I felt it was striking enough. I copied the text and I had the first part of my script. To that, I add the Instructions for what I want them to do.

I want to write that out it bold RED with a BLACK  background so I break it up by new lines “`n” and Write-Host

Next, I want the user to type an exact string to ensure they know what they are about to do. You can make this anything you want, but I like to keep in the idea of what is about to happen. Something like “I WANT TO DELETE ALL USERS IN CONTOSO.COM” If the user types this, they can’t say they didn’t know what they were doing.

To facilitate this, I use a while loop.

The ‘if’ statement is a friendly reminder that the reply needs to be in all capital letters. As long as they type it exactly as it appears, the script will go on. Otherwise it won’t go past this point.

Make things a bit more interesting…

Some of the characters in that original warning text are of a certain type. There are solid characters making up the majority of the letters and there is a collection of “boarder” characters. To identify these boarder characters I want to get the int equivalent for the char we see. To do this, I use the following syntax:

This identifies the character as type char and then converts it to its integer equivalent ([int]). This particular character gives us an integer value of 9565. Now I want to look at the other characters in that range. There are only so many “double-line boarder” characters so I choose all the characters between 9500 and 9600. To look at these characters and their values I run this:

What I’m doing here is taking all the numbers between 9500 and 9600 and looping through them one at a time writing the number and the char equivalent using string formatting (-f). “{0}{1}” the 0 is the first entry after the ‘-f or the number passed in ($_) converted to a string (.ToString()) and padded to the right 5 characters. I need to convert the number to a string in order to gain access to the ‘padright()’ method (all this is just for neatness. I could have easily just used “{0} {1}” since all the numbers are 4 digits anyways). In the next index {1} I take the number, Identify it as an integer ([int]) then convert it to a character ([char]) “$([char][int]$_))”.

All this tells me that the characters I want have integer values between 9552 and 9580.

Now that we know that, we can take each line, split it up by characters and if a character’s int value falls in between 9550 and 9580 we can apply a different color to it like this:

gives us this:

or DarkRed and Red gives us:

Using a different Font (Alligator2) and some more colors and this code:

You get the idea!

Oh, and if you want to get rid of the vertical lines (better seen in the first example, you’ll need to change your font to Lucida Console.

Apr 24

Rename a distribution group in Exchange with Powershell

Recently I was asked to rename some distribution groups in Exchange. Not so tough of a problem, but painstaking to do one at a time and there were a few of these to do. So I did what any good admin should do, I wrote a script to take care of this.

The issues
To properly rename a distribution group, you need to not only change the name of the group, but also the Alias, DisplayName and entries in the EmailAddresses field. The first two are easy but if you’re like me, you need to add a handful of emailaddresses to these fields. In our environment, we tend to add records when a name is changed rather than swap the old for the new. This way, if someone uses the old address, it still goes to where they want it too. Probably a better answer would be to create a mailbox with the old address and setup an auto reply that says “Hey, use the new address for this list.” That’ll be for another day.

Anyways, here is the script…
I should note, that I’m keeping this function (along with some other Exchange functions on github. you can get the most recent version of this script here: https://github.com/glaisne/PowerShellExchangeFunctions/archive/master.zip


Some things worth noting

First of all, I’m working trying to make every bit of code I write more of a tool for others rather than something I use in my environment only. As a result, I’m utilizing ShouldContinue and ShouldProcess more. In this script I use them twice. First when I change the Name, Alias and DisplayName fields

The second time I use ShouldProcess and ShouldContinue is when I set the EmailAddresses field.

Also, you’ll notice there isn’t much in the way of actual comments. This is a bit of a departure for me as I love my comments. But, rather than use comments, I’ve decided to use Write-Verbose so that everyone can share in “What should be happening.”

Let me know if you have any questions, or if you think there is a better way.


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