-
-
Notifications
You must be signed in to change notification settings - Fork 473
/
Copy pathconvert-history2ps1.ps1
executable file
·35 lines (32 loc) · 1.27 KB
/
convert-history2ps1.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<#
.SYNOPSIS
Converts history to PowerShell script
.DESCRIPTION
This PowerShell script converts your command history into a PowerShell script.
It saves all your commands into a script for automation (executed by e.g Jenkins or AutoHotkey).
.PARAMETER path
Specifies the file path of the new script ('script-from-history.ps1' by default)
.EXAMPLE
PS> ./convert-history2ps1.ps1
✅ Converted your command history into the PowerShell script: script-from-history.ps1
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
param([string]$path = "script-from-history.ps1")
try {
$history = Get-History
foreach ($item in $history) {
Write-Output "`"⏳ Step #$($item.Id): Executing $($item.CommandLine) ...`"" >> $path
Write-Output "& $($item.CommandLine)" >> $path
Write-Output "" >> $path
}
Write-Output "`"✅ PowerShell script finished (source: command history of $($env:USERNAME) on $($env:COMPUTERNAME)).`"" >> $path
Write-Output "exit 0 # success" >> $path
"✅ Converted your command history into PowerShell script: $path"
exit 0 # success
} catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}