Watfaq / PowerSession

Record a Session in PowerShell
MIT License
152 stars 9 forks source link

Small script to automate recordings #26

Open AnderssonPeter opened 2 years ago

AnderssonPeter commented 2 years ago

Hi I wrote a small powershell script to automate recordings. Feel free to use it!

function Parse-KeyText ($Content) {
    $Content = $Content.Replace("`r`n", "`n").Replace("`n", "{ENTER}")
    [regex]$regex = '(?<modifiers>[+^%]+)?(?<content>{[^}]+}|[^+?^\^^%])'
    $regex.Matches($Content) | % { $_.Value }
}

function Send-Keys ($KeyStrokes, $KeyPressSleep = 30, $LongSleep = 1500) {
    $wshell = New-Object -ComObject wscript.shell;
    foreach ($KeyPress in $KeyStrokes)
    {
        if ($KeyPress -eq '{SLEEP}')
        {
            Sleep -Milliseconds $LongSleep
        }
        else {
            Sleep -Milliseconds $KeyPressSleep
            $wshell.SendKeys($KeyPress)
        }
    }
}
#Documentation https://ss64.com/vb/sendkeys.html
$textToSend = @"
+^t{SLEEP}cd D:\Users\Peter\source\repos\FluentTerminal
powersession rec -c "pwsh -nologo" recording.txt
{SLEEP}Import-Module PowerType
{SLEEP}Enable-PowerType
git c{SLEEP}{DOWN}{SLEEP}{DOWN}{SLEEP} m{SLEEP}{DOWN}{SLEEP}
exit
{SLEEP}exit

"@
Write-Host "Installing powertype"
Install-Module PowerType
Write-Host "Simulating"
$KeyStrokes = Parse-KeyText -Content $textToSend
Send-Keys -KeyStrokes $KeyStrokes
Write-Host "Uninstalling PowerType"
Uninstall-Module PowerType

example of the result: asciicast

AnderssonPeter commented 2 years ago

Hmm some chars are lost in the preview image, but I'm guessing that's a asciinema issue?