#requires -version 2.0 # ----------------------------------------------------------------------------- # Script: Convert-HistoryToScript.ps1 # Version: 2.0 # Author: Jeffery Hicks # http://jdhitsolutions.com/blog # http://twitter.com/JeffHicks # Date: 1/10/2011 # Keywords: Get-History, Add-Content # Comments: Convert history commands to a ps1 script file. # # "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. * # **************************************************************** # ----------------------------------------------------------------------------- <# .Synopsis Convert history commands to a script file. .Description This script will take history commands and convert them into a text file, presumably a PowerShell script. The default is all history commands, but you can also specify a comma separated list of history items. .Parameter Path The filename and path to create. .Parameter ID The history IDs to convert, separated by commas. .Parameter NoClobber Do not overwrite an existing file. .Example PS C:\> Convert-HistoryToScript c:\scripts\Dev1.ps1 Directory: C:\scripts Mode LastWriteTime Length Name ---- ------------- ------ ---- -a--- 1/10/2011 11:03 AM 3227 dev.ps1h .Example PS S:\> 1,3,4 | .\Convert-HistoryToScript.ps1 Dev2.ps1 Directory: C:\scripts Mode LastWriteTime Length Name ---- ------------- ------ ---- -a--- 1/10/2011 11:05 AM 400 Dev2.ps1 Export commands 1,3,4 to a script file. .Example PS S:\> (12..20),34,45,8,5 | .\Convert-HistoryToScript.ps1 -Path Dev3.ps1 Directory: C:\scripts Mode LastWriteTime Length Name ---- ------------- ------ ---- -a--- 1/10/2011 11:10 AM 408 Dev3.ps1 Export commands 12 through 20 then commands 34, 45, 8 and 5 to a script file. .Notes NAME: Convert-HistoryToScript AUTHOR: Jeffery Hicks VERSION: 2.0 LASTEDIT: 01/10/2011 Learn more with a copy of Windows PowerShell 2.0: TFM (SAPIEN Press 2010) .Link http://jdhitsolutions.com/blog/2011/01/convert-history-to-script/ .Link Get-History .Inputs [int64] .Outputs FileObject #> Param( [Parameter(Position=0,Mandatory=$True,HelpMessage="Enter the filename and path for the script file", ValueFromPipeline=$False)] [ValidateNotNullorEmpty()] [string]$Path, [Parameter(Mandatory=$False,ValueFromPipeline=$True)] [int64[]]$ID, [switch]$NoClobber ) Begin { Set-StrictMode -Version 2.0 Write-Verbose "Starting $($myInvocation.mycommand)" #delete the script if it already exists if ((Test-Path -Path $Path) -AND ($NoClobber)) { $msg="$path already exists and NoClobber was specified." Write-Warning $msg #bail out Exit } Elseif (Test-Path -Path $path) { Write-Verbose "Removing existing version of $path" Remove-Item $Path } #get the file name and convert the last part to upper case $FileName=(Split-Path -Path $path -Leaf).ToUpper() #add a brief header $header=@" #Requires -Version 2.0 # $Filename # Generated from PowerShell History $(Get-Date) # Author: $env:username # $("-"*80) "@ $header | Out-File -FilePath $Path -Encoding ASCII $history=@() } #close Begin Process { if ($id) { foreach ($i in $id) { Write-Verbose "Retrieving history item $([string]$i) $((Get-history -id $i).CommandLine)" $history+= Get-History -id $i } } else { Write-Verbose "Retrieve All History" $history=Get-History -count $maximumhistorycount } } #close Process End { Write-Verbose "Writing $($history.count) history commands to $path" Add-Content -Path $path -Value ($history | select -expandproperty commandline ) -Encoding ASCII #write the file object to the pipeline Write-Verbose "Writing script file object to the pipeline" Get-Item -Path $path Write-Verbose "Finished" } #close End