Friday Fun Get Beer List

beerWell, another Friday and what goes better with it than beer. Of course I should mix in a little PowerShell as well. I live in the Syracuse, NY area and we have a terrific local brewery, Middle Ages Brewing Company. Not only is there a tasting room, but I can also get growler refills. Middle Ages brews a number of beers throughout the year and beers available for growlers vary. Fortunately they post online what is currently available.

But I can’t be bothered to open a web browser and visit their site everytime I want to see what they have. That’s what PowerShell is for. Specifically PowerShell 3.0 and Invoke-Webrequest. With only a few lines of PowerShell, actually it could be done as a one-liner, I can get the list of beers available for growler refills. Let me break it down.

First, I’ll grab the web page with Invoke-WebRequest


PS Scripts:\> $ma = Invoke-Webrequest http://middleagesbrewing.com/

Invoke-Webrequest “packages” it as a nice object.

invoke-webrequest-middleages

On the page, the growler refills have links pages for the respective beers. This is handy because I can get all the links from the object I just pulled down.


PS Scripts:\> $ma.links[-2]

innerHTML : Wailing Wench
innerText : Wailing Wench
outerHTML : Wailing Wench
outerText : Wailing Wench
tagName : A
href : /beers/wailing-wench

Naturally there are other links as well, but after looking through them I realized all the ones I was interested in had /beers/ in the href property. Knowing that, I can filter.


PS Scripts:\> $ma.links | where {$_.href -match "/beers/"}

innerHTML : Double Wit
innerText : Double Wit
outerHTML : Double Wit
outerText : Double Wit
tagName : A
href : /beers/double-wit

innerHTML : Druid Fluid
innerText : Druid Fluid
outerHTML : Druid Fluid
outerText : Druid Fluid
tagName : A
href : /beers/druid-fluid
...

Excellent. All I need is to grab one of the text properties and I have my list.


PS Scripts:\> $ma.links | where {$_.href -match "/beers/"} | select -expand InnerText
Double Wit
Druid Fluid
ImPaled Ale
Kilt Tilter
Middle Ages Pale Ale
Session IPA
Swallow Wit
The Duke of Winship
Tripel Crown
Wailing Wench

Perfect (and I’m getting a little thirsty). My script, such as it is comes down to two lines.


$ma = Invoke-Webrequest http://middleagesbrewing.com/
$ma.links | where {$_.href -match "/beers/"} | select -expand InnerText

This could even be consolidated down to a one-liner:

((Invoke-Webrequest http://middleagesbrewing.com/).Links | where {$_.href -match "/beers/"}).InnerText

although there’s no practical reason to do so. If you are new to PowerShell that is a little more difficult to understand. Normally I prefer sending objects to the pipeline but all I really need to see are beer names so this works just fine for me.

I see they are filling growlers with the Duke of Winship porter, one of my faves, so I’d better wrap this up. Cheers!