A number of years ago I shared a fun PowerShell script that generated a description of a new thriller you might find in the action thriller section of your local book store. I modeled it after the works of authors like Vince Flynn, Ben Coes and James Rollins. I'm a big fan and of these types of books and certainly a guilty pleasure. Because they are genre books there is a certain formula and my script generated a description of a "new" book using data randomly drawn for a collection of different items such as the hero's name, his former profession (in these books the hero is almost always male), the female interest and the villainous organization.
ManageEngine ADManager Plus - Download Free Trial
Exclusive offer on ADManager Plus for US and UK regions. Claim now!
The first iteration was a bit simplistic. Even though I had better things to do, I spent some time updating and expanding the script. As with all my Friday Fun posts, the script is intended as a learning tool. Unless you are a budding thriller author, you really don't have any need for the script itself, other than an amusing diversion. But, if you need to see an example of how arrays are used, or here strings or even how to structure a simple script, my project will help you out.
The current code is written as a script which means you need to specify the full path to the script and of course you need an execution policy that will run the script. Here's an example of what you can expect. The script writes a string object to the pipeline.
Because markdown is seemingly everywhere now, this version includes a parameter to convert some of the text into markdown.
One of the reasons is because PowerShell Core has a few markdown cmdlets including Show-Markdown which will render a markdown document in the console.
It also works in PowerShell Core on Linux.
As you can read, I'm having a bit of fun with this. The script is posted as a gist on my Github repo.
In the script I define a number of arrays that contains the source items for the different elements in the book description. Originally I was piping each array to Get-Random to select an element. But I wanted a bit more randomization and also some flexibility. So I wrote a private function that is embedded in the script. This function is never meant to be run directly by a user which is why it does not have a proper name. Each array is processed by the function to first randomize the entire collection and then select a random number of elements. In some instances I want more than one item.
The main element of the script is a here string, defined as $blurb. A here string is a great way of creating a multi-line string. Just remember that the closing line needs to be left justified. Because this is a string, I can use variable replacement throughout. Although I am also demonstrating how to use the -f operator. In the here string you'll see placeholders {0} and {1}. The -f operator takes a comma separated list of values and plugs each one into the corresponding placeholder.
$($blurb -f "'$($title[1])'","'$($title[2])'")
The entire expression is defined as a sub-expression because it is being replaced in yet another here string, $out, which will be the script's output. I define a different version of $out depending on whether I need to generate markdown or not.
if ($AsMarkdown) { $out = @" # $($Title[0]) ## by $author $($blurb -f "[$($title[1])]()","[$($title[2])]()") ### Published $pubDate "@ } else { $out = @" $($Title[0]) $("-" * ($title[0].length)) by $author $($blurb -f "'$($title[1])'","'$($title[2])'") Published $pubDate "@ }
I've tried to annotate the code to help explain what I am doing and why. If you are learning PowerShell, I hope you'll take some time to analyze how the script works. After you've had some fun generating a few new books!
As always, I love to hear what you think. Enjoy and check the corners.
1 thought on “Friday Fun: PowerShell Thriller Revisited”
Comments are closed.