MCPMag.com

Sign up for our newsletter.

I agree to this site's Privacy Policy.

Prof. Powershell

PowerShell 3 Web of Wow, Part 2

In part 2 of this series, we take a look at cmdlet RestMethod.

Last time we looked at one of the new PowerShell v3 cmdlets for working with web data, Invoke-WebRequest. This week we'll look at a cmdlet that I think you'll find even easier to use, Invoke-RestMethod. REST, which stands for Representational State Transfer is being used more and more to deliver web-based data.

But don't worry, I'm not going to turn this into a .NET programming lesson. That's the beauty of using a cmdlet; it does all the heavy lifting. You just have to use the results. Here's an easy demonstration.

PS C:\> $url = "http://mcpmag.com/rss-feeds/prof-powershell.aspx"
PS C:\> $data = invoke-restmethod $url

The URL is to the RSS feed for this column. Here's what $data looks like (see Fig. 1):

Invoke-WebRequest cmdlet

Figure 1. Example of $data. (Click image to view larger version.)

Behind the scenes, Invoke-Restmethod creates an XML object.

PS C:\> $data[0].gettype().name
XmlElement

This makes it very easy to get the information we want.

PS C:\> $data | Select PubDate,Link,Title | out-gridview -title "Prof. PowerShell"

You can see the results in Figure 2:

Invoke-WebRequest cmdlet

Figure 2. Results from Invoke-Restmethod. (Click image to view larger version.)

One thing to be careful of, depending on the results you get from Invoke-Restmethod, is that most properties will be strings. In this example this means if you try to sort on the PubDate property, you won't get the results you expect. The solution is to treat the property as a DateTime object.

PS C:\> $data | Select @{Name="Published";Expression={$_.PubDate -as
[datetime]}},Title,Link | where Published -gt "11/01/2012" | Sort Published | format-
list

Published : 11/6/2012 7:14:30 PM
title : HTML Bits and Pieces, Part 1
link : http://mcpmag.com/articles/2012/11/06/pshell-html-1.aspx

Published : 11/13/2012 6:31:26 PM
title : HTML Bits and Pieces, Part 2
link : http://mcpmag.com/articles/2012/11/13/pshell-html-2.aspx

Published : 11/27/2012 4:15:33 PM
title : HTML Bits and Pieces, Part 3
link : http://mcpmag.com/articles/2012/11/27/pshell-html-3.aspx

Published : 12/4/2012 4:26:02 PM
title : A Better View of PowerShell Help
link : http://mcpmag.com/articles/2012/12/04/pshell-showwindow.aspx

Published : 12/11/2012 4:25:12 PM
title : Get Your PowerShell Object Properties In Order
link : http://mcpmag.com/articles/2012/12/11/pshell-order.aspx

I created a new property called Published that is the PubDate property cast as a datetime object and then I filter out articles published since Nov. 1, 2012. This is a great cmdlet for building an RSS reporting tool in PowerShell.

But some web services can be a bit more interactive. A typical example that most people can relate to is Twitter. Sites, like Twitter, often provide a web-based API that you can query. Sometimes you need an API key and authorization token, but I leave those examples for you. This is pretty straight forward.

PS C:\> $url = http://search.twitter.com/search.json?q=PowerShell&rpp=10&lang=en

The query says find all tweets in the English language with "PowerShell" and return 10 results per page. Now to invoke the query.

PS C:\> $data = Invoke-RestMethod $url

You can see the $data object in Figure 3:

Invoke-WebRequest cmdlet

Figure 3. PowerShell query results. (Click image to view larger version.)

PowerShell should take the response and create a custom object. The information I want is in the results property. It looks like a collection so I'll peek at the first one to figure out what it looks like (see Figure 4).

PS C:\> $data.results[0]

Invoke-WebRequest cmdlet

Figure 4. PowerShell data results. (Click image to view larger version.)

Excellent! Now I can get the information I want from the results.

PS C:\> $data.Results | Select @{Name="Datetime";Expression={$_.created_at -as
[datetime]}},@{Name="From";Expression={"{0} ({1})" -f
$_.from_User_Name,$_.from_User}},Text,@{Name="Link";Expression={"https://twitter.com/
{0}/status/{1}" -f $_.from_user,$_.id }} | Out-GridView -Title "PowerShell Tweets"

Figure 5 displays the results:

Invoke-WebRequest cmdlet

Figure 5. Information extracted from data results. (Click image to view larger version.)

Invoke-RestMethod is a powerful tool and I've only scratched the surface. Please find some time to look through full help and examples. In the last part of this series we'll look at one final way of interacting with web-based services.

More on this topic:

About the Author

Jeffery Hicks is a multi-year Microsoft MVP in Windows PowerShell, Microsoft Certified Professional and an IT veteran with almost 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He works today as an independent author, trainer and consultant. Jeff is a regular contributor to a variety on online sites, as well as frequent speaker at technology conferences and user groups. Keep up with Jeff and his projects at http://jdhitsolutions.com/blog.

comments powered by Disqus