Thursday, December 27, 2012

Transcripts : Leverage them

This post is a result of the query asked on the Facebook page of Bangalore PowerShell User Group.
When someone asks a question regarding a Script, it becomes very difficult to follow through it especially if it  is related to some very different domain like SQL, SharePoint (especially for me).

I personally feel one could attempt to even try answering the question if he knows what happens behind the scenes like the Verbose Output associated with the cmdlets one is executing and the type of Objects that are being emitted etc.
Someone could ask the beginners to pipe the Objects into the Get-Member cmdlet to see the intricacies of the Object, or the Process one follows. Here we can leverage transcripts.

Already transcripts are used by PowerShell Gurus in the presentations or trainings , so that later you can see them and find out what went in which order.

In short Transcripts capture whatever appears in the PowerShell Console host.

Note! Windows PowerShell ISE doesn't support the transcripts here but you can follow Scripting Guy's post to make transcripts from ISE as well.

To keep things a bit easier I am going to use PowerShell Console for this post.


The Secret Sauce

The another great feature which I as a beginner found great is that PowerShell follows Universal Code Execution Model, which in simple terms means whatever works in the Script non-interactively can also be run interactively in the Shell and vice-versa.
This is actually great because I can try something interactively on the PowerShell Console, if it works as expected....Grab the code and put it in a Script. It will work too...awesome isn't it.


Example

I would take an elaborate example here . Please bear with me...

Suppose I was trying something related to services like I am trying to change the "start mode" of a running service (I know this is a typical example here)


Suppose I got the following Script form somewhere or someone :

$spooler = Get-Service -Name spooler
if ($spooler.Status -eq 'running'){
Write-Verbose "The Print Spooler Service is running..Trying to change the start mode"
$spooler.startMode = "automatic" # try to set the start  mode here
}
else {
Write-Verbose "The Print Spooler Service is not running"
}

Now when I run it it throws an error "Property 'startMode' cannot be found on this object; make sure it exists and is settable."
I did something obviously wrong here. So if I post the script in some forum they will ask me to post the Error first , if the experienced Power Users are already familiar with the Error the solution will come right in. If not then they will ask you to post the result of when you pipe the $spooler to Get-Member to know exactly what you did wrong.
One would go ahead and simply paste the output in the forum where the formatting goes awry some time. Better way is to do this.

Start the Transcript and Execute the Script Code statement by statement. Also to make things much clearer, after an Object is created pipe it into Get-Member to give clarity on the Object being spitted out.




Now execute the next statement



After you are done. Issue cmdlet "Stop-Transcript".

Now see the Transcript file has captured whatever you did in the Console and it also has the Error which was generated.


Take the file created and post it alongside your Question in the Forum. Someone could see it and observe that there is no property named "startmode" on the Object captured in $spooler.....Hmmmm..so this is where it went wrong ( Who knew ;) )

Now this is a very simple scenario where the transcripts can also be used . You can get creative in supplying the Useful information to others by putting $verbosepreference = 'continue' to include verbose messages in the transcript too( my little POSH trick ).

You can do more with them just pipe yourself to Get-Creative !!

No comments:

Post a Comment