Table of Contents

PowerShell AToolbox: Get-Backpat

One of my early functions and before I graduated to animations. Cheesy backpatting for IT guys hard-at-work.

The function grabs a randomized ASCII image from a text file and a text string from another text file (like cheesy rap or Emily Dickinson) and then outputs the first to the screen with a randomized color, and the text string it converts to audio via a very robotic text to speech.

Here’s the main function. You’ll have to supply your own media files.

Function Get-BackPat {
    #Reads a line from a file, backpats.csv, at random. Writes some ASCII art to the PS console from a file, backpatsascii.txt, at random. Use the -custom parameter to enter a custom tts message.
    param(
        [parameter(helpmessage="For if you want it to say what you want it to say instead of what it wants to say on its own.")][string]$Custom = $Null
    )

    $Location = ENTERYOURDIRECTORYLOCATIONHERE, like c:\getpackpats

    $MessageLocation = $Location + "\backpats.txt"
    $ASCIILocation = $Location + "\backpatsascii.txt"
    

    
    #In the ASCII .text file, put <END> between the images (but not the end of the document). In the backpats.txt file, just have each string separated by a carriage return.
    $ASCIIItem = Get-Content $ASCIILocation -delimiter "<END>" | Get-Random
    #Pulled background colors out because they stink. To put them back in, the parameter is -BackgroundColor. $bgc = @('Black','DarkBlue','DarkGreen','DarkCyan','DarkRed','DarkMagenta','DarkYellow','Gray','DarkGray','Blue','Green','Cyan','Red','Magenta','Yellow','White') | Get-Random
    #Also, I pulled 'black' out of the below one because many consoles are black or something like it. 
    $FGC = @('DarkBlue','DarkGreen','DarkCyan','DarkRed','DarkMagenta','DarkYellow','Gray','DarkGray','Blue','Green','Cyan','Red','Magenta','Yellow','White') | Get-Random
    Write-Host -Object $ASCIIItem -ForegroundColor $FGC 


    If ($Custom -eq ""){
        $Messages = Get-Content $MessageLocation | Get-Random
    } Else {
        $Messages = $Custom
    }
    $TTS = New-Object -ComObject SAPI.SpVoice
    $TTS.Speak("$Messages") | Out-Null
}