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 Microsoft MVP in Windows PowerShell, Microsoft Certified Trainer and an IT veteran with over 20 years of experience, much of it spent as an IT consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He works today as an independent author, trainer and consultant. Jeff writes the popular Prof. PowerShell column for MPCMag.com and is a regular contributor to the Petri IT Knowledgebase and 4SysOps. If he isn't writing, then he's most likely recording training videos for companies like TrainSignal or hanging out in the forums at PowerShell.org. Jeff's latest books are Learn PowerShell 3 in a Month of Lunches, Learn PowerShell Toolmaking in a Month of Lunches and PowerShell in Depth: An Administrators Guide. You can keep up with Jeff at his blog http://jdhitsolutions.com/blog, on Twitter at twitter.com/jeffhicks and on Google Plus (http:/gplus.to/JeffHicks)

comments powered by Disqus

Reader Comments:

Mon, Feb 10, 2014

http://w DOT ww.findyourcollegesonline.com http://www.insurecaronline.com/ DOT http://www.maddonnasnashville.com/o DOT nline_casino.php http://www.jenniferbyrnevirtually.com/ DOT http://w DOT ww.amendmentinsurance.com

Sun, Feb 2, 2014

http://www.jbkind-doors-blog.com/ DOT http://www.insurecaronline.com/ DOT http://w DOT ww.genmedica.net http://www.premierkiwi.com/ DOT http://www.babydressingroom.com/ DOT http://www.djmarzone.com/ DOT

Thu, Jan 30, 2014

http://www.celticgreenla.com/ DOT http://clamorousfall.com/f DOT orex-online.html http://www.abwheminn.org/ DOT http://y DOT ankeegold-anovel.com http://www.naturalcosmeticandskincare.org/ DOT

Sat, Jan 25, 2014

http://w DOT ww.lifeinsuranceshopping.net http://w DOT ww.cheapinsurancemate.com http://w DOT ww.medinsurers.com http://y DOT ouroptionshere.com http://clamorousfall.com/f DOT orex-online.html http://www.buyfirstmedication.com/ DOT

Tue, Jan 21, 2014

http://w DOT ww.healthinsur.net http://w DOT ww.healthinsurancebible.com http://w DOT ww.gettopedmeds.com http://w DOT ww.insurplus.net http://f DOT orexlikepro.com http://w DOT ww.medspricechart.com

Sat, Jan 18, 2014 Kailin http://w DOT ww.cheapautoinsurcover.com http://www.mycheapinsuranceonline.com/ DOT http://w DOT ww.allhealthinsurers.net http://w DOT ww.medicaforyou.net

http://w DOT ww.cheapautoinsurcover.com http://www.mycheapinsuranceonline.com/ DOT http://w DOT ww.allhealthinsurers.net http://w DOT ww.medicaforyou.net http://www.cheapestcarproviders.com/ DOT http://www.mybestinsurancequotes.net/ DOT

Wed, Nov 6, 2013 Janai affordable car insurance quotes

http://www.carinsurquote.net/ DOT

Sun, Nov 3, 2013 Dede #19 My boyfriend and I went to an art muuesm in Montreal and one of the pieces was literally just a wooden box. Just a plain, not painted, wood freaking box.

#19 My boyfriend and I went to an arthttp://r DOT bcjgxfqvgu.com in Montreal and one of the pieces was literally just a wooden box. Just a plain, not painted, wood freaking box.

Mon, Sep 30, 2013 Keys small business insurance cash back credit card

http://www.eben-uk.org/ DOT http://www.comparecashbackcards.net/ DOT

Fri, Sep 27, 2013 Kayleen best credit card cash back

http://www.travelrewardscards.net/ DOT http://www.comparecashbackcards.net/ DOT

Thu, Sep 26, 2013 Summer credit card rewards

http://www.travelrewardscards.net/ DOT

Thu, Sep 19, 2013 Delores buy viagra on line cialis

http://effectiveedmedicine.com/ DOT http://priceforerection.com/ DOT

Fri, Sep 13, 2013 Marsue car insurance quotes low rate credit cards

http://www.findcarinsurdeals.com/ DOT http://www.lowaprcard.net/ DOT

Sun, Jul 28, 2013 Kathreen car insurance cheap cheap car insurance breakdown

http://www.careyourauto.com/ DOT http://www.careyourauto.com/S DOT hould-Women-Pay-Less-Than-Men-for-Their-Auto-Insurance.html

Fri, Jul 26, 2013 Janaye car insurance quotes cheap auto insurance free

http://www.careyourauto.com/S DOT hould-Women-Pay-Less-Than-Men-for-Their-Auto-Insurance.html http://www.careyourauto.com/ DOT

Add Your Comment Now:

Your Name:(optional)
Your Email:(optional)
Your Location:(optional)
Comment:
Please type the letters/numbers you see above