Skip to main content

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 !!

Popular posts from this blog

Test connectivity via a specific network interface

Recently while working on a Private cloud implementation, I came across a scenario where I needed to test connectivity of a node to the AD/DNS via multiple network adapters.  Many of us would know that having multiple network routes is usually done to take care of redundancy. So that if a network adapter goes down, one can use the other network interface to reach out to the node. In order to make it easy for everyone to follow along, below is an analogy for the above scenario: My laptop has multiple network adapters (say Wi-Fi and Ethernet) connected to the same network. Now how do I test connectivity to a Server on the network only over say Wi-Fi network adapter?

PowerShell + SCCM : Run CM cmdlets remotely

Today I saw a tweet about using implicit remoting to load the Configuration Manager on my machine by Justin Mathews . It caught my eye as I have never really tried it, but theoretically it can be done. Note - The second tweet says "Cannot find a provider with the name CMSite", resolution to which is in the Troubleshooting section at the end.

PowerShell : Trust network share to load modules & ps1

Problem Do you have a central network share, where you store all the scripts or PowerShell modules ? What happens if you try to run the script from a network share ? or if you have scripts (local) which invoke scripts or import PowerShell modules stored on this network share ? Well you would see a security warning like below (Note - I have set execution policy as 'Unrestricted' not 'bypass' here): Run a .ps1 from the network share Well this is a similar warning, which you get when you download scripts from Internet. As the message says run Unblock-File cmdlet to unblock the script and then run it, let's try it.