Powershell: Typist | Zak Emerick's IT Blog

My IT blog for random ideas and solutions.

Powershell: Typist

Tags:

[CmdletBinding()]
<#
.SYNOPSIS
Replaces key input with sections from a file. 
.DESCRIPTION
Will read from a text file of your choosing and replaces your keystrokes with random sections of the file.
Just a novelty script.
.EXAMPLE
 This param is required.
Powershell.exe -file Typist.ps1 -FilePath CodeFile.cs
.EXAMPLE
 The maximum parameter will determine the the ceiling of the random number generator.
Powershell.exe -file Typist.ps1 -FilePath CodeFile.cs -Maximum 40
#>
param(
    [ValidateRange(2,50)]
    [int]$Maximum = 10,
    [Parameter(Mandatory=$True)]
    [string]$FilePath
)
begin {
    Clear-Host
    $content = Get-content $FilePath | Out-String
    $s = 0
    $Error.Clear()
    if ($Maximum -ge $content.Length) { 
        Write-Host "Error: Maximum range $($Maximum) is greater than content size of $($content.Length)" -ForegroundColor Red
        return
    }
}
Process {
    [int]$c = $content.Length
    while (!$Error) {
        $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") | Out-Null
        $host.UI.RawUI.Flushinputbuffer()
        $r = Get-Random -Minimum 1 -Maximum $Maximum
        try { 
            Write-Host "$($content.Substring($s,$r))" -NoNewline
            $s += $r
            $c -= $r
        } catch {
            $wshell = New-Object -ComObject wscript.shell;
            $wshell.SendKeys('{ENTER}')
            $wshell.SendKeys('{ENTER}')
        }
    }
}
end {}

A simple novelty script I wrote today to mimic hackertyper.net . It could have been a little cleaner, but for what it is– it works just fine!

913 Total Views 1 Views Today


Post a Comment

Your email address will not be published. Required fields are marked *

 characters available