MCPMag.com

Sign up for our newsletter.

I agree to this site's Privacy Policy.

Prof. Powershell

PowerShell 3 Web of Wow, Part 3

Over the last few articles we've been looking at how to interact with Web -based services using PowerShell v3. There is one more cmdlet in the new toolkit: New-WebServiceProxy. This cmdlet creates an object that acts as a proxy to a Web service. When you want to invoke one of the service's methods you instead can invoke the proxy object. There is no need for .NET programming. PowerShell simplifies the entire process.

Let's look at this with a publicly available Web service that retrieves weather information:

PS C:\> $url=http://www.webservicex.com/globalweather.asmx?WSDL

This isn't a Web page that we want to retrieve. Instead we'll need to invoke its "methods." Begin by creating a Web service proxy object.

You can see the resulting object in Figure 1.

Invoke-WebRequest cmdlet

Figure 1. Web service proxy object. (Click image to view larger version.)

The proxy object will maintain a Web session with the Web service. What can we do with this service? Let's ask using Get-Member.

PS C:\> $proxy | Get-Member -MemberType Method
XmlElement

As you can see in Figure 2 there are a number of methods at our disposal.

Invoke-WebRequest cmdlet

Figure 2. Resulting method list. (Click image to view larger version.)

Some of these methods take parameters.

PS C:\> ($proxy | Get-Member GetWeather).Definition
string GetWeather(string CityName, string CountryName)

It seems all I need to do is pass a few names.

PS C:\> $proxy.GetWeather("Orlando","United States")
<?xml version="1.0" encoding="utf-16"?>
<CurrentWeather>
  <Location>ORLANDO SANFORD AIRPORT, FL, United States (KSFB) 28-47N 081-15W</L
ocation>
  <Time>Dec 18, 2012 - 04:53 PM EST / 2012.12.18 2153 UTC</Time>
  <Wind> from the W (270 degrees) at 13 MPH (11 KT):0</Wind>
  <Visibility> 10 mile(s):0</Visibility>
  <SkyConditions> clear</SkyConditions>
  <Temperature> 75.0 F (23.9 C)</Temperature>
  <DewPoint> 45.0 F (7.2 C)</DewPoint>
  <RelativeHumidity> 34%</RelativeHumidity>
  <Pressure> 29.94 in. Hg (1013 hPa)</Pressure>
  <Status>Success</Status>
</CurrentWeather>

Depending on the service and method you may need to resort to documentation to learn what values to pass or how to use a method. In this particular case I get back what looks like XML data. So let's try this:

PS C:\> [xml]$weather = $proxy.GetWeather("Orlando","United States")
PS C:\> $weather.currentweather | Select
Time,Location,Temperature,RelativeHumidity,Pressure

Time : Dec 18, 2012 - 04:53 PM EST / 2012.12.18 2153 UTC
Location : ORLANDO SANFORD AIRPORT, FL, United States (KSFB) 28-47N 081-15W
Temperature : 75.0 F (23.9 C)
RelativeHumidity : 34%
Pressure : 29.94 in. Hg (1013 hPa)


I can invoke the method, or any other method, for as long as I have this proxy object. I might want something like this:

"Las Vegas","Chicago","Cleveland","Seattle","Miami" | foreach {
([xml]($proxy.GetWeather($_,"United States"))).CurrentWeather
} | Out-GridView -Title "Weather Conditions"

 

Invoke-WebRequest cmdlet

Figure 3. Example of invoked method. (Click image to view larger version.)

With the cmdlets we've looked at in the last few lessons, you should be ready to interact with just about any Web service either on the public Internet or something delivered internally. If you run into issues using these Web cmdlets, don't forget to use the forums at PowerShell.org.

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:

Tue, Jan 21, 2014

http://w DOT ww.medspricechart.com http://w DOT ww.cheapinsurancemate.com http://w DOT ww.findairflights.com http://b DOT aapalsa2013.com http://www.forgetyoured.net/ DOT

Wed, Jan 15, 2014 Kaylin http://w DOT ww.carinsursite.com http://w DOT ww.medicaforyou.net http://w DOT ww.antidepressionadviser.com http://w DOT ww.insureyourbusiness.net

http://w DOT ww.carinsursite.com http://w DOT ww.medicaforyou.net http://w DOT ww.antidepressionadviser.com http://w DOT ww.insureyourbusiness.net http://w DOT ww.slotschips.com http://w DOT ww.medformen.com http://w DOT ww.getcarinsurquotes.com

Sun, Oct 20, 2013 Davion You got to push it-this estiasenl info that is! http://mpiahdnlce.com [url=http://yejeml.com]yejeml[/url] [link=http://jejbkql.com]jejbkql[/link]

You got to push it-this estiasenl info that is! http://mpiahdnlce.com [url=http://yejeml.com]yejeml[/url] [link=http://jejbkql.com]jejbkql[/link]

Wed, Oct 16, 2013 Esther http://iplocals.com/

http://iplocals.com/ DOT

Sat, Sep 21, 2013 Tangie small business insurance estimate

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

Mon, Sep 16, 2013 Honey car insurance quotes generic cialis online

http://www.findcarinsurdeals.com/ DOT http://priceforerection.com/ DOT

Sat, Sep 14, 2013 Matei viagra sale cash back

http://effectiveedmedicine.com/ DOT http://www.comparecashbackcards.net/ DOT

Tue, Feb 5, 2013 Michael Cullina Philadephia

Dear Jeffery, Very nice three part series. Keep up the good PowerShell info sharing.

Add Your Comment Now:

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