From cead884c6492a728301b28bf4ca4e71220989933 Mon Sep 17 00:00:00 2001 From: Markus Fleschutz Date: Mon, 17 Feb 2025 11:31:00 +0100 Subject: [PATCH] Added convert-history2ps1.ps1 --- scripts/convert-history2ps1.ps1 | 35 +++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 scripts/convert-history2ps1.ps1 diff --git a/scripts/convert-history2ps1.ps1 b/scripts/convert-history2ps1.ps1 new file mode 100644 index 00000000..1313ad5d --- /dev/null +++ b/scripts/convert-history2ps1.ps1 @@ -0,0 +1,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 +} \ No newline at end of file