February 16, 2015

Grumpy Over Colour

Don’t get me started on the whole Colour v Color debate in UK IT! I used to code all my functions in powershell with colour just to piss off American English users off. But being a grumpy IT Administrator, the fun soon left and the 1 less char to type means, I soon learned to spell Colour the annoying American way!

Well if they make such a big deal over it, then why the hell don’t they use more actual colour more in Powershell!!! I’m stealing code here! I want it to be useful and pretty, don’t make me have to read the text guys! Green is good, Red is bad right?

Lets make it easier on my eyes please. When I write a script – I try to get it working first – (not always possible with my poor scripting skills). Then I try to make it easy to use, and pipeline safe, and then I try to make it pretty! I give user responses in colours, green for good… RED for bad… Yellow for requesting input or authorisation. Seems sensible – then if I’m happy with the resulting code, I might release it on the web or just stick in the \script directory on the servers at work to never be executed again!

So all you powershell script writers out there, please make grumpy Admin here, a happier and less likely to lock out random users accounts for fun and bring a bit of colour to the command line… all blue and white make IT a boring world to live in! And bored Admins, makes for a bad day for the end users!

Take for example, every so often I have to run, a PSH script on our Microsoft DPM Server (which will be the subject of many grumpy blogs!!!) to mark expired tapes as free so the backup product DPM won’t vomit a truck load of errors and alerts.

Also it means I don’t have to organise the junior admin guy in the remote office to change the backup tapes. He more lazy than me, but being the son of the technical director tends to not do anything he is told!  SO every so often I have to run the forcefree.ps1 script, to avoid the politics of having to send 5 or 6 emails to him, his line manager and then cc his father into the chain to get a backup tape swapped out.

This is a good script , does the job it says on the tin! It marks the times, free by expiring recovery points on that tape so it can be reused by DPM – it has some colour in it, when there is an error, the scripts uses write-error /o\ so the script is either white or red!

Grumpy Admin doesn’t like that… we demand pretty these days!

Would it kill them to put colour in it once it has marked a point as deleted… a screen of the odd green text saying a operation has been completed successfully makes me less grumpy than just a boring screen of white scrolling text and/or a screen of red error messages! (We will blog about error handling in Powershell another day)

I am also a very strong believer in using Google, chances are someone has done what you want to do before! Why rewrite the function when cut and paste will do – yes, always check the code and tweak it to your needs etc and be a good guy and credit your sources (we all do that don’t we???)

So years ago I started to include this function in to most of my polished PSH scripts, it took me less than 15 seconds to find, cut and paste and understand this code function and it makes my scripts all coloured and wonderfully responsive when outputting to the host!

function Write-Color([String[]]$Text, [ConsoleColor[]]$Color) {
for ($i = 0; $i -lt $Text.Length; $i++) {
Write-Host $Text[$i] -Foreground $Color[$i] -NoNewLine

This then allows me to put a line like this in to my script to output stuff to the user – makes it more easier on the eyes!

Write-Color -Text “Action Completed [“, “OK”,”]” -Color White,Green,White

Which gives the following output


SEE!!!! fantastic – just what we want isn’t it guys!!!

This makes it easier to see results from large amount of output, that could be scrolling down your screen – for example, a screen full of recovery points being expired on a tape! – Doesn’t make much effort Powershell script writers of the world, it is the little things after all! And makes this Grumpy Admin less grumpy!

So while the whole colour / color thing still makes me grumpy as a British IT Administrator – I had to eat American humble pie and adopt color in to my scripts, now make your scripts pretty with acutal color please!!!!