Learn about Windows PowerShell
Summary: Create a transcript of commands from the Windows PowerShell ISE in this Microsoft Scripting Guys how-to article.
Microsoft Scripting Guy Ed Wilson here. One of the things I enjoy about using the Windows PowerShell console is the transcript feature. When I am playing around on the Windows PowerShell console, experimenting, trying out new ideas, and exploring ideas, it is great not to worry about losing any cool code I discover. In my Windows PowerShell console profile, I use the Start-Transcript Windows PowerShell cmdlet to start a transcript when I am working in the Windows PowerShell console. I even have a function I wrote, which automates this—it is in my Windows PowerShell console profile. The function is shown here.
Start-Trans function
function start-Trans([switch]$debug) { # to see debuging information startTrans -debug if($debug) { $debugPreference = "continue" } #end if debug $dte = [dateTime]::Get_Today().DayOfWeek.tostring() write-debug $dte $dte = $dte + "_" + [dateTime]::now.hour write-debug $dte if(([datetime]::now.toLocalTIme()) -match "AM") { Write-debug "Inside if ..." $dte = $dte + "_AM" write-debug $dte } #end if... else { write-debug "Inside else ..." $dte = $dte + "_PM" write-debug $dte } #end else write-debug "Starting transcript ... c:\trans\$dte.txt" start-transcript -path "c:\trans\$dte.txt" } #end start-Trans
When I start the Windows PowerShell console, the transcript automatically starts, as shown in the following image.
Any information I return to the Windows PowerShell console screen is logged to the log file. This is shown in the following image.
Unfortunately, the Windows PowerShell ISE does not have a transcript command. The Start-Transcript command generates an error, which is shown here:
PS C:\Users\ed.NWTRADERS> Start-Transcript Start-Transcript : This host does not support transcription. At line:1 char:17 + Start-Transcript <<<< + CategoryInfo : NotImplemented: (:) [Start-Transcript], PSNotSup portedException + FullyQualifiedErrorId : NotSupported,Microsoft.PowerShell.Commands.Start TranscriptCommand
Using the Windows PowerShell ISE object model, it is possible to create a transcript command that will work inside the Windows PowerShell ISE. I decided to create two functions, even though we do not actually need both functions. The Get-LogNameFromDate function will create a text file in a specific location. The file name is based upon the current date and time. The time portion uses the hour, minute, and second. The date portion of the file name uses the year, month, and day. As is usually the case, the help information is much more involved than the code that actually does the work.
The code that creates the file name for the log is shown here. I use parameter substitution to create the file name. The file name comprises four parts: the path, the name prefix, the time, and the extension. After the file name is created, the New-Item cmdlet is used to create the file.
$$logname = "{0}\{1}{2}.{3}" -f $path,$name, `
(Get-Date -Format yyyyMMdd-HHmmss),"Txt" New-Item -Path $logname -ItemType file | out-null
In the Start-ISETranscript function, the text that is in the output pane is obtained from the text property of the output object and is written to the log file. The thing that is strange about the text property of the output object is that it is only available while the script is running. When the script ceases to run, the property is not available. This means you cannot access the output pane text from the Windows PowerShell ISE command pane. This portion of the Start-ISETranscript function is shown here.
$psISE.CurrentPowerShellTab.Output.Text >> $logname
The two functions are shown here.
Windows PowerShell ISE transcript functions
Function Get-logNameFromDate { <# .Synopsis Creates a log name from date .Description This script creates a log from a date. .Example Get-logNameFromDate -path "c:\fso" -name "log" Creates a file name like c:\fso\log20100914-122019.Txt but does not create the file. It returns the file name to calling code. .Example Get-logNameFromDate -path "c:\fso" -name "log" -Create Creates a file name like c:\fso\log20100914-122019.Txt and create the file. It returns the file name to calling code. .Parameter path path to log file .Parameter name base name of log file .Parameter create switch that determines whether log file or only name is created .inputs [string] .outputs [string] .Notes NAME: Get-LogNameFromDate AUTHOR: ed wilson, msft LASTEDIT: 09/10/2010 16:58:06 KEYWORDS: parameter substitution, format specifier, string substitution HSG: WES-09-25-10 .Link Http://www.ScriptingGuys.com #Requires -Version 2.0 #> Param( [string]$path = "c:\fso", [string]$name = "log", [switch]$Create ) $logname = "{0}\{1}{2}.{3}" -f $path,$name, ` (Get-Date -Format yyyyMMdd-HHmmss),"Txt" if($create) { New-Item -Path $logname -ItemType file | out-null $logname } else {$logname} } # end function get-lognamefromdate Function Start-iseTranscript { <# .Synopsis This captures output from a script to a created text file .Example Start-iseTranscript -logname "c:\fso\log.txt" Copies output from script to file named xxxxlog.txt in c:\fso folder .Parameter logname the name and path of the log file. .inputs [string] .outputs [io.file] .Notes NAME: Start-iseTranscript AUTHOR: ed wilson, msft LASTEDIT: 09/10/2010 17:27:22 KEYWORDS: HSG: WES-09-25-10 .Link Http://www.ScriptingGuys.com #Requires -Version 2.0 #> Param( [string]$logname = (Get-logNameFromDate -path "C:\fso" -name "log" -Create) ) $transcriptHeader = @" ************************************** Windows PowerShell ISE Transcript Start Start Time: $(get-date) UserName: $env:username UserDomain: $env:USERDNSDOMAIN ComputerName: $env:COMPUTERNAME Windows version: $((Get-WmiObject win32_operatingsystem).version) ************************************** Transcript started. Output file is $logname "@ $transcriptHeader >> $logname $psISE.CurrentPowerShellTab.Output.Text >> $logname } #end function start-iseTranscript
To use the Start-ISETranscript function, add the call to the function at the bottom of your script. The script does not have to be saved or have a name. This is shown in the following image. One thing to keep in mind is that the entire contents of the output pane will be written to the log file. This means that if you run the script two or three times and do not clear the output pane, you could have the results from several scripts in your transcript log.
The output of the Start-ISETranscript function is shown in the following image. As you can tell, the header to the transcript file is extremely similar to the second image shown above that was produced by the Start-Transcript cmdlet from the Windows PowerShell console.
We invite you to follow us on Twitter and Facebook. If you have any questions, send email to us at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow for another edition of the Weekend Scripter. Until then, peace.
Ed Wilson and Craig Liebendorfer, Scripting Guys