blog.powershell.no

On Windows PowerShell and other admin-related topics

How to automatically convert Windows PowerShell transcripts into script-files

In Windows PowerShell we can use the Start-Transcript cmdlet to record PowerShell sessions to a text-file. This will record both the commands you`ve run as well as the output from the commands.

Windows PowerShell MVP Jeffery Hicks recently wrote a great tip in his Friday Fun series on his blog, which tells you how to convert a PowerShell transcript into a PowerShell script file. That is, you`ll get a ps1-file which contains the commands extracted from the transcript. Combined with an object event which triggers when PowerShell exits, this can be set up to happen automatically. Jeff actually blogged another Friday Fun tip a couple years ago which describes how to set up such an object event.

Lets have a look at an example on how this would work. First we launch a new PowerShell session and executes a few commands:

image

 

When we exit PowerShell we`ll get two files in a specified log directory:

image

 

The transcript file (txt-file) contains all commands, errors and output from our session:

image

 

The PowerShell script file (ps1-file) contains a script header and the commands from our session:

image

 

This means that every PowerShell session automatically generates a PowerShell script file which can be the foundation for a new script.

To set this up you first need to copy the Export-Transcript PowerShell function from Jeff`s blog-post and add it to your PowerShell profile (Microsoft.PowerShell_profile.ps1), in addition to the following:

 

001
002
003
004
005
006
007
008
009
#Define variable for the transcript path, which we`ll use to generate the path to the ps1-file
$transcriptlog = ("C:\PS-logs\PS-Transcript_"+"{0:yyyy-MM-dd_HH-mm-ss}" -f (Get-Date)+".txt")
Start-Transcript -Path $transcriptlog | Out-Null

#Export transcript to ps1-file on exit
Register-EngineEvent PowerShell.Exiting –action {
Stop-Transcript
Export-Transcript
 -Transcript $transcriptlog -Script (($transcriptlog.Replace("Transcript","Script")).Replace("txt","ps1"
))
} 
| Out-Null

 

While this is very useful, there is a few gotcha`s to be aware of:

  • This doesn`t work if your exiting PowerShell using the X button. The PowerShell.Exiting event is only triggered when using the exit command.
  • This doesn`t work in the Windows PowerShell ISE, since that PowerShell host doesn`t support transcripts.
  • If you`ve customized your PowerShell prompt, you`ll need to tweak the Export-Transcript function to match the last letter in your prompt.
Advertisements

August 12, 2011 - Posted by | Windows PowerShell | ,

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: