BATCHman Uses PowerShell to Thwart Dr. Regex and Save the Zoo

BATCHman Uses PowerShell to Thwart Dr. Regex and Save the Zoo

  • Comments 3
  • Likes

Summary: In this exciting episode, BATCHman uses Windows PowerShell and regular expressions to thwart the evil Dr. Regex and save the monkeys.

 

Microsoft Scripting Guy, Ed Wilson, is here. Sean Kearney is back with us for three more episodes of the thrilling BATCHman series. Today is Episode 4.

BATCHman and Cmdlet logo

Whenever trouble happens in systems and people will call,
and darkness rolls out causing your fall,
Creatures of bits roam in the night,
Shine to the sky, the bright bluish Light,

and call to…BATCHman !

…and, oh yes, his sidekick Boy Blunder Cmdlet, too.

 

In Today’s Episode, BATCHman Encounters Doctor Regex!

Cutting through the midnight blue sky, a beacon for justice, a light for freedom calls out!

“Holy shell, BATCHman! It’s the BATCHsignal! We have…”

“Ahem, Cmdlet, I would suggest you watch your language there. This is after all a ‘G’ rated blog post.”

“But but, I…I…ugh…yes, BATCHman,” he sullenly bowed in defeat. “Shall I fire up the WinMobile?”

“No, but you can start it,” snickered our hero. “To the WinMobile!”

Buckling under the weight of our heroes, the WinMobile moved off. “Thank goodness it’s made of Gorilla Glass,” muttered Cmdlet.

Meanwhile, somewhere else in the city of Redmond, evil has placed a call. The arch criminal simply known as the dreaded Dr. Regex has visited. Cursed after too many nights of writing parse queries, our arch villain only speaks in riddles of Regex. He has taken the team at the local Redmond Zoo by surprise. All the cages are being unlocked, potentially unleashing the vicious GUI eating SAPIEN monkeys! Our arch villain dances off on top of the cages laughing maniacally in an oddly cheeseburger colored jumpsuit with his big “!” symbol. “Muah ha haaaaa!”

The only passcode to the electronic Chubb lock system has been left somewhere in this cryptic riddle left on Notepad on the desktop of the main server:

Evil may Come And Evil May Go
The BATCHman Will Seek, be Fast Or Be Slow
But Soon On the Day The pass It Must Be
The word Should He Say, To Not Set Them Free
is The BATCHman Too Smart, Or Is He Not Bright?
under The Stars And Under the Light!
The key We Shall See, Unlocking This Clue
Will chain Them All Down, Or Let Them Run Through

Signed ^[Dr!Regex]^ 

Photo of the evil Dr. Regex

BATCHman and Cmdlet arrive on the scene, the sound of Monkeys about to tear scripts apart. Cmdlet stared at the riddle. “Holy twisted blisters, BATCHman! That must have worn out somebody’s fingers typing! What a bizarre rid…”

“….bizarre clue!” corrected BATCHman “Yes. Yes, indeed. What a strange person. There must be some type of pattern in this clue. Something usual to the naked human eye, something I can’t quite see, some…”

“BATCHman! It appears only certain words start with a lowercase letter!”

“Cmdlet! Excellent ! How did you spot it so quickly?”

“It’s written here in the plot, page 5, line 7…”

BATCHman looks down scratching his cowl. “Ah, so it is.”

“So we could just write down every word,” suggested Cmdlet.

“Pah! And waste a good chance to use Windows PowerShell? Never! This is a perfect opportunity to use regular expressions to find those matches! First, we need to identify the pattern. We need all words starting with a lowercase letter only. Within regular expressions, that would be a matter of specifying that the first letter is from a to z only like this.”

[a-z]

“And then we’ll need to specify it’s at the beginning of the word.”

\b[a-z]

“Then we’ll specify that the word must be at least two letters long if we find it, but can be as long as we want. This should trap our series of words.”

\b[a-z]{2,}

“With this in place, Cmdlet, we need to pipe the information from the text file into a Select-String statement like this.”

GET-CONTENT C:\EVILCLUE.TXT | SELECT-STRING ‘\b[a-z]{2,}’

Cmdlet sat there cross-legged watching the magic unfold. “But how do we tell it to look for more than one instance, BATCHman? How does it know it is lowercase?”

“Presently it doesn’t, but it will if we add more parameters to our Select-String. One is -allmatches in case there is more than one instance per line. The other is -casesensitive because Select-String by default just does a –match which doesn’t care about uppercase or lowercase.”

GET-CONTENT C:\EVILCLUE.TXT | SELECT-STRING ‘\b[a-z]{2,}’ –casesensitive –allmatches

Evil may Come And Evil May Go
The BATCHman Will Seek, be Fast Or Be Slow
But Soon On the Day The pass It Must Be
The word Should He Say, To Not Set Them Free
is The BATCHman Too Smart, Or Is He Not Bright?
under The Stars And Under the Light!
The key We Shall See, Unlocking This Clue
Will chain Them All Down, Or Let Them Run Through

Cmdlet looked and laughed. “Holy foot in mouth! The output is the same!”

The BATCHman smiled. “Not quite. If we run Get-Member against this output, you’ll see something different.”

GET-CONTENT C:\EVILCLUE.TXT | SELECT-STRING ‘\b[a-z]{2,}’ –casesensitive –allmatches | GET-MEMBER

“This, Cmdlet, is not just a string. We can see there are properties such as Matches. If we were to look at the properties of Matches, we mind find something interesting.”

GET-CONTENT C:\EVILCLUE.TXT | SELECT-STRING ‘\b[a-z]{2,}’ –casesensitive –allmatches | SELECT-OBJECT Matches

Image of matches

“Holy obvious! Do you mean…” looking to his left Cmdlet lifts the keychain to the old cages. A Big envelope with the words “^[Dr!Regex]^” is written on it. He bites it open and his mouth drops sullenly.

“What is it, Cmdlet?”

“Another clue. Awww, come on!!!” Cmdlet stomps about like a little child having a hissy fit showing the new clue to BATCHman.

Remove ME now and unlock the clue

‘meHeymeheymewemearemethemescriptmemonkeesmeandmepeoplemesaymewemearemescriptingmeawayme’

BATCHman looks. “I’m seeing an awful lot of ME in this particular line. I will bet we could just use a –replace on it!

“What’s that?”

Seating himself down in front of the Windows PowerShell console, he explains. “Replace is another part of regular expressions where we can replace one thing with another.”

‘meHeymeheymewemearemethemescriptmemonkeesmeandmepeoplemesaymewemearemescriptingmeawayme’ –replace ‘me’,’’

“That will replace every reference of me with nothing, which gives us this result.”

Heyheywearethescriptmonkeesandpeoplesaywearescriptingaway

“Cmdlet, please try this password on the Chubb security system. I’m sure you’ll find it works; just press the Lock All Cages button.”

Soon echoing through the Redmond Zoo, massive sounds of *KLANG* echo throughout with the odd *yelp* as the occasional finger gets pinched on closing doors.

With one loud *THUD*, Dr. Regex falls into bushes below. “Waaaaihihghghhgih!!”

The keeper of the Redmond Zoo looks up. “Oh, BATCHman! How can ever thank you?”

“Easy, good citizen. One, do not put your master security password under a set of keys to the Monkey House and…do you have something I could scrape my shoes on? I think I stepped on something near the cages.”

 

Thanks, Sean, for another exciting episode of BATCHman! Join us tomorrow for Episode 5.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy

 

 

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Dear - honored - BATCHman,

    you saved the world again!

    Dr. Regex has been beaten and the riddles have been solved!

    GREAT!

    As great as regular expression are!

    And I REALLY invite all powershell scripters to have a closer look at them, if you haven't already! they are incredibly useful if your task is to parse text!

    If you don't want to bother the Select-String Cmdlet, you can apply a simple replace to solve the first riddle:

    PS C:\Users\Schulte> ((GET-CONTENT I:\EVILCLUE.TXT) -creplace '\b[A-Z]+[a-z]+[\s,?!]*') -join ''

    may be the pass word is under the key chain

    This is another approach, that deletes all unwanted words, whitespace and punctuation.

    It uses the case sensitve creplace operator to replace words starting with uppercase letters  '\b[A-Z]+[a-z]+' followed by any whitespace, comma, question- or exclamation marks ... leaving the words beginning with lowercase letters for us.

    the final "-join ''" is just added for readability sake to avoid spreading the solution over several lines.

    Klaus.

  • We should sit down and have tea in the BATCHcave sometime, after Cmdlet cleans up the mess :)

  • Some more resources for learning regex for Powershell.

    social.technet.microsoft.com/.../learning-regex-for-powershell.aspx