Weekend Scripter: PowerShell for "Talk Like a Pirate" Day


Summary: Ed Wilson, Microsoft Scripting Guy, talks about using Windows PowerShell to translate for International Talk Like A Pirate day.

Ahoy mates! Microsoft Scripting Guy, Ed Wilson, is here. It is one of my favorite holidays of the year—probably my favorite holiday. Yep, it is International Talk Like a Pirate day today. The Scripting Wife and I are heading to the coast for some authentic pirate reenactments and a fun day of culture and education. Arrrrrr...

Here is a picture of the Scripting Wife trying out her pirate speak with a couple of nautical looking individuals.

Photo of Scripting Wife

Well, me hearties...If you would not be dubbed a landlubber on this auspicious occasion, you may need to brush up on the lingo.

Luckily with Windows PowerShell, this is easier than two shakes of a parrots feathers.

I looked around for an honest to goodness web service, but for some reason, there is not such a thing. I mean, the Bing Translator API does Klingon, so I figured it might also do pirate speak, but blimey, that just is not the case.

But all is not lost, because beginning with Windows PowerShell 3.0, we have the Invoke-WebRequest cmdlet. Unfortunately, there have not been a lot of examples of using this cmdlet. So rather than give the ol' heave ho and do it the old-fashioned way, we will simply hoist anchor and write a couple quick lines of code.

Note  This can be an extremely frustrating technique, and you can flip the hour glass several times if you are not careful. In addition, sometimes one update to your target webpage and your code be shark bait.

Find out what webpage you wants

At times, when you do not have a web service or an API that you can use, you are limited to supplying values for web forms and capturing the returned information. This can work well for simple tasks, but it can also take a lot of time. For example, I wasted nearly two hours playing around with this today—and I do not even have my eye patch on or a parrot looking over my shoulder.

I found a page that had a simple form I could use: Speak Pirate...How to Talk Like a Pirate. The next thing to do is figure out what the input box is called. To do this, I used the developer tools from the Microsoft Edge Browser (to access them, press F12 or select Developer Tools from the action menu). Here is the page that appears:

Image of command output

The original pate is shown here:

Image of webpage

I know that I want to post to the page, so I will be calling the Post method. I also know, from looking around, that the input box has a name of source_text_area and that the box has source_text. So I played around with it until I found something that would work.

I stored the returned information in a variable so that I could examine the returned data until I had something I would use. Because I did not want to do too much typing, I stored my URL in a variable I called $web:

$web = http://www.speakpirate.com/

Next I called the Invoke-WebRequest, passed my URL, called the Post method, and specified a value for the source_text string to translate. The resultant command is shown here:

$a = Invoke-WebRequest -Uri $web -Method post -Body @{source_text="A Fine Navy day"}

Lastly, I parsed the output. I had to search around a bit to find the output. But once again, by using the developer tools, I found that the returned box had an idea of translated. So I used Windows PowerShell to parse all the output until I found what I was looking for:

Image of command output

From this output, I could see that I was interested in the OuterText. I was then able to clean up the output a bit and find only the translated text. This command is shown here:

$a.AllElements.where({$_.id -eq 'translated'}).outertext

So, the three lines of code I need appear here:

$web = "http://www.speakpirate.com/"

$a = Invoke-WebRequest -Uri $web -Method post -Body @{source_text="A Fine Navy day"}

$a.AllElements.where({$_.id -eq 'translated'}).outertext

Here are the commands and their associated output:

Image of command output

Avast you swabs! I hope you have a fun Talk Like a Pirate day. Armed with a bit of Windows PowerShell code, you’ll be talk’n pirate quickly and avoid'n walk'n the plank.

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

Comments (0)

Skip to main content