March 2, 2015

Console Trans-tripping!

Grumpy Monday, meh! After the thrill of the weekend coming in to work make me grumpy! Not just a case of the Monday’s!  What is worse than that, you were working in the Powershell console on Friday, you know stringing some commands together in an attempt to get decent output from the blue-screen! If you are like me you will  wish you had a transcript of that Powershell session for prosperity or to just prove you did some work to the boss!

I could use it to cut and paste, as well we all know Grumpy Admin is lazy and would hate having to type stuff again. Or we can use it in our defence. “why did you do that horrible action which killed the production SharePoint server…” what action… you mean by actually installing it in the first place… as we all know installing SharePoint is a major cause of loss of productivity in an office!!!

So in order for you to save your bacon, and to cover you ass and to help you when you have mind farts on a Monday morning… I would like to introduce to you a neat little feature of Powershell –

Start-Transcript

This feature was introduced to Powershell in version 2.0 and not many people know about this hidden little gem of a cmdlet 🙂

Here is a link to the technet page on the cmdlet

https://technet.microsoft.com/en-us/library/hh849687(v=wps.620).aspx

But the basic syntax is – I assume you do a get-help rather than going to the webpage 🙂

Start-Transcript [[-Path] <String> ] [-Append] [-Force] [-NoClobber] [-Confirm] [-WhatIf] [ <CommonParameters>]

This is a great and useful cmdlet – Now there are lots of things you can do with this – I personally recommend putting it inside your $profile so every time that you load a Powershell console – It will capture your output! How cool is that! You can use this to capture the output from installation scripts and other scripts that are unattended, could be easier than writing debugging functions inside your Powershell???

So armed with this great cmdlet, we can come up with some Powershell code like this

$output = (Join-Path $Env:USERPROFILE “Documents\WindowsPowerShell\Console”)
If (-Not (Test-Path $output)) {New-Item -path $output -Type Directory | out-null}
$global:TRANSCRIPT = (“{0}\{1:dd-MM-yyyy}.txt” -f $output,(Get-Date))
Start-Transcript -Append

Let me quickly talk you through this dirty script I wrote.

Line One: we want to make $output the correct path – so we steal the $Env:USERPROFILE path and use the Powershell cmdlet Join-Path to the full path that we want to use for the location of our transcript. You can change this is you want to something more useful if you want, but Documents\WindowsPowerShell\ is where my $profile is.

Line Two: prevents a issue, in that if it is your first time running the script and the directory console doesn’t exist, it might crash out so we test for the path using the cmdlet Test-Path and if it is not there it creates it – with a cheeky | out-null to suppress screen output after all this is a dirty hack to stop any possible crash conditions… Grumpy Admin get miffed, when his scripts crash! Don’t you?

Line Three: This line sets the $global variable TRANSCRIPT which holds the UNC for where we want out transcript should be stored, here we make it the path that we glued together in line one, by using {0} and then we get funky by using the (get-date) function, so we can craft a file name from the current date – very useful for other scripts as well 🙂 {1:dd-mm-yyyy} as you can see the {0} will get substituted with $output and {1} will get given (get-date), using the magic of formatting strings we return the (get-date) into dd-mm-yyyy!

Line Four: We start the Transcript feature – by using Start-Transcript -Append – this means we just append to the old file, so you whole day will be captured not just that individual Powershell console session.

So to trip out and start using transcripts in your Powershell consoles, just put them 4 lines in to your $profile and enjoy having every little typo and mistake on the record!

don’t forget you can issue the Stop-Transcript misbehave and do naughty commands and poke around, and then hit the Start-Transcript -append cmdlet and you are cooking on gas! Wasn’t me, I have every Powershell command logged boss, check the files :winks:

Hazzy