March 29th, 2016

Using the new PowerShell ISE transcript tool

Summary: Ed Wilson, Microsoft Scripting Guy, talks about using the new Windows PowerShell 5.0 ISE Transcript tool.

 

One of my favorite features for Windows PowerShell has always been the transcript tool. I mean, it seems like it was made for Windows PowerShell … I guess it was. What am I talking about? Well, one of my favorite ways to spend time is plugging in various Windows PowerShell commands to see what they might do. Then I try to shorten them, make them leaner, or more powerful, or modify them to better suit my needs. Back when I used to do this on other systems, I would end up copying the command, and storing it in a notebook, or in a text file. With the Windows PowerShell transcript tool, I have not only a record of my commands, and errors they generate, but also the output that comes from the command. It is a great way to learn stuff about Windows PowerShell.

The problem, is that the Start-Transcript tool only worked in the Windows PowerShell console, and I have been using the Windows PowerShell ISE more and more … so I did like any good Windows PowerShell person would do, and I wrote my own transcript tool for the Windows PowerShell ISE.

But now … there is a Windows PowerShell ISE transcript tool built into Windows PowerShell ISE … beginning with Windows PowerShell 5.0 on my Windows 10 computer.

Start the PowerShell transcript

So, the first thing I do is open the Windows PowerShell ISE, and in the command pane, I type the command Start-Transcript. The command outputs the path to the transcript. This appears here:

PS C:\> Start-Transcript

Transcript started, output file is C:\Users\mredw\Documents\PowerShell_transcript.EDL

T.6VtWcOHE.20160329162630.txt

 

Now I type a command. I want one that will not take up too much space, so I select the last process in the collection of objects returned by the Get-Process cmdlet (gps is an alias for get-Process). This command appears here:

PS C:\> gps | select -Last 1

 

Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id  SI ProcessName

——-  ——    —–      —– —–   ——     —  — ———–

293      18     5416      10536   116            2808   0 ZeroConfigService

 

Ok, now I stop the transcript. To do this, I use the Stop-Transcript cmdlet. This command once again outputs the path to the transcript file. This command and output appears here:

PS C:\> Stop-Transcript

Transcript stopped, output file is C:\Users\mredw\Documents\PowerShell_transcript.EDL

T.6VtWcOHE.20160329162630.txt

 

Note that the transcript file is the path to my documents folder in my profile, then the word transcript and a computer name, a bit of random number, and then the date and time. I could never remember all of tha, so I copy and paste it and try to open it in notepad. The result? An error. This appears here:

PS C:\> notepad C:\Users\mredw\Documents\PowerShell_transcript.EDL

T.6VtWcOHE.20160329162630.txt

T.6VtWcOHE.20160329162630.txt : The term ‘T.6VtWcOHE.20160329162630.txt’ is not

recognized as the name of a cmdlet, function, script file, or operable program.

Check the spelling of the name, or if a path was included, verify that the path is

correct and try again.

At line:2 char:1

+ T.6VtWcOHE.20160329162630.txt

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

+ CategoryInfo          : ObjectNotFound: (T.6VtWcOHE.20160329162630.txt:String

) [], CommandNotFoundException

+ FullyQualifiedErrorId : CommandNotFoundException

 

The problem is the path is so long, that it actually breaks into two lines. When I attempted to paste it back, I picked up a random break and the command failed. So, now I do a copy and paste again, but this time I hit backspace to remove the random break. Yeah, it works. Here is the log file:

HSG-3-29-16-01

A better way to handle this long log file path

A better way to handle this semi random long file name in a deep profile path, is to simply capture the returned output from the Start-Transcript command. Unfortunately, what returns is simply a string. This appears here:

PS C:\> $a = Start-Transcript

PS C:\> $a.GetType()

IsPublic IsSerial Name                                     BaseType

——– ——– —-                                     ——–

True     True     String                                   System.Object

 

PS C:\> $a

Transcript started, output file is C:\Users\mredw\Documents\PowerShell_transcript.EDL

T.wnH7glru.20160329163437.txt

 

When I wrote my transcript function, I also wrote a function called Get-TranscriptFile that parsed this string into a path that I could directly pass to notepad to open the transcript. I still have that function, but another cool cmdlet in Windows PowerShell 5 makes that a bit obsolete. That is because Windows PowerShell 5 includes a cool cmdlet called ConvertFrom-String that will create an object from a string. Here is an example:

PS C:\> Convertfrom-String -InputObject $a

P1 : Transcript

P2 : started,

P3 : output

P4 : file

P5 : is

P6 : C:\Users\mredw\Documents\PowerShell_transcript.EDLT.wnH7glru.20160329163437.txt

 

The part I want is the P6 property. The command appears here:

Notepad (Convertfrom-String -InputObject $a).p6

Sweet! So much easier than having to write my own functions and the like. PowerShell 5 for the win!

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. Also check out my Microsoft Operations Management Suite Blog. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy

Author

0 comments

Discussion are closed.