For those of you who don’t know what Lorem Ipsum is and know what PowerShell is, let me say this:
For each paragraph, create some random number of sentences, then for each sentence, create a random number of words, and then for each word, create some random letters. But remember, each sentence needs to start with a capital letter and the whole thing should start with “Lorem Ipsum”.
There are plenty of websites that you can find to create Lorem Ipsum for you, but I wanted to see if I could create a generator in PowerShell.
Now let me preface this by saying I love scripts that make me think, but have very little purpose beyond, can I do it?
I was at TechMentor 2016 in Orlando, and one of the people I admired asked me what I was working on. Now first off, it’s nice to meet some of the people you admire, but occasionally, not so much. Well, I beamed with pride to say that I’m writing a script to create Lorem Ipsum. I won’t mention names, but the look of confusion on his face, and then followed up by, “why would you do that?” He followed that up with some more comments belittling my script and then asked again, why would you do that? My answer: “Because I wanted to see if I can.”
So how does my script work? Start with the basic logic, only two options to send to the script, paragraph count or how many words you want to produce. You can’t give both, so make your choice and give me some information of what you are looking for.
If you tell me paragraphs, I loop through the generator and produce that number of Lorem Ipsum paragraphs. If you tell me words, I over guess how many paragraphs just to be safe, but then produce that exact number of words.
There was some testing to make sure I got the spacing correct, capitalization for the first word in each sentence and making sure I counted words correctly. After I got all that, I added the word count to stop after X number of words requested. Of course, if I’m doing a word count, I had to give my paragraph, sentence and word counts more than it expected to stop loops.
Logic of the script
# Looping thru each paragraph.
for ($p = 1; $p -lt ($Paragraphs + 1); $p++) {
# Looping through each sentence.
$sentencesInParagraph = Get-Random -Minimum 4 -Maximum 18
for ($s = 1; $s -lt ($sentencesInParagraph + 1); $s++) {
# Loop through the words in each sentence
$wordsInSentence = Get-Random -Minimum 4 -Maximum 18
for ($w = 1; $w -lt ($wordsInSentence + 1); $w++) {
# Determine if it's the first paragraph, the first sentence and the first word.
if ($p -eq 1 -and $s -eq 1 -and $w -eq 1) {
"Lorem Ipsum"
} else {
# Letters in word
for ($l = 1; $l -lt ((Get-Random -Minimum 4 -Maximum 10) + 1); $l++) {
# do something
}
}
}
}
}
So, that’s the basic logic of the script. The output is random each time you run it so you’ll never get the same output twice.
I had fun writing, but can’t see much use for it in a production environment.
A complete copy of the script is here:
https://github.com/digway/LoremIpsum/blob/master/ds_New-IgLoremIpsum.ps1
