#Requires -version 2.0 # ----------------------------------------------------------------------------- # Script: Convert-TranscriptToScript.ps1 # Version: 1.0 # Author: Jeffery Hicks # http://jdhitsolutions.com/blog # http://twitter.com/JeffHicks # Date: 3/19/2011 # Keywords: Transcript # Comments: # # Take a PowerShell transcript and parse it for commands, # sending each line to a new script file which can then # be further edited. # # "Those who neglect to script are doomed to repeat their work." # # **************************************************************** # * DO NOT USE IN A PRODUCTION ENVIRONMENT UNTIL YOU HAVE TESTED * # * THOROUGHLY IN A LAB ENVIRONMENT. USE AT YOUR OWN RISK. IF * # * YOU DO NOT UNDERSTAND WHAT THIS SCRIPT DOES OR HOW IT WORKS, * # * DO NOT USE IT OUTSIDE OF A SECURE, TEST SETTING. * # **************************************************************** # ----------------------------------------------------------------------------- Function Convert-TranscriptToScript { <# .Synopsis Convert a PowerShell transcript to a script. .Description This function will parse out all commands from a PowerShell transcript and create a PowerShell script file that you can then continue to edit and flesh out. Existing script files will be overwritten unless you use -NoClobber. IMPORTANT: This function assumes your PowerShell prompt starts with PS and ends with >. .Parameter Transcript The file name and path of your transcript file. .Parameter ScriptFile The filename and path for your new script. Existing files will be overwritten. .Parameter NoClobber Do not overwrite an existing script file. A warning will be displayed and the transcript will not be processed. .Example PS C:\> Convert-TranscripttoScript "c:\work\trans.txt" "c:\scripts\NewScript.ps1" Finished parsing c:\work\trans.txt. See c:\scripts\NewScript.ps1 for your script. .Notes NAME: Convert-TranscriptToScript AUTHOR: Jeffery Hicks VERSION: 1.0 LASTEDIT: 03/19/2011 Learn more with a copy of Windows PowerShell 2.0: TFM (SAPIEN Press 2010) .Link http://jdhitsolutions.com/blog/2011/03/convert-transcript-to-script/ .Link Start-Transcript about_Regular_Expressions .Inputs None .Outputs None #> Param ( [Parameter(Position=0,Mandatory=$True,HelpMessage="You need to specify a transcript file name and path.")] [ValidateNotNullOrEmpty()] [ValidateScript({Test-Path $_})] [string]$Transcript, [Parameter(Position=1,Mandatory=$True,HelpMessage="You need to specify a filename for the new script.")] [ValidateNotNullorEmpty()] [string]$ScriptFile, [Switch]$NoClobber ) #If -NoClobber make sure file doesn't already exist if ($NoClobber -AND (Test-Path -Path $ScriptFile)) { Write-Warning "NoClobber specified and $ScriptFile already exists. Try again with a new name." } else { #Create new file and overwrite any existing file #This is a script header "# Script Created $(Get-Date)" | Out-File -filepath $ScriptFile "# Source file: $Transcript" |Out-File -filepath $ScriptFile -append "# Author: $env:userdomain\$env:username" | Out-File -filepath $ScriptFile -append #insert a blank line write `n | Out-File -filepath $ScriptFile -append #get all the lines that start with PS which should be part of a prompt. #and filter out the stop-transcript line #if you have a custom prompt that doesn't start with PS this probably won't work for you. Or #you will need to modify the regular expression [regex]$r="^(?PS(.+?)>)" $filteredLog=Get-Content $Transcript | Select-String -pattern $r | where {$_ -notmatch "stop-transcript"} $filteredLog | Foreach { #each object in $filteredLog is a MatchInfo object which needs to be converted to a string #Then split the string into an array using the regular expression $lines=$r.Split($_) #trim off spaces on the last element and write to the script file $lines[-1].Trim() | Out-File -filepath $ScriptFile -append } #add a closing comment write "`n#End of script" | Out-File -filepath $ScriptFile -append Write-Host "Finished parsing $Transcript. See $ScriptFile for your script." -ForegroundColor Green } #else } #end function #define an alias Set-Alias -Name cts -Value Convert-TranscriptToScript