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 an IT veteran with over 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 is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. He works today as an independent author, trainer and consultant. Jeff has written for numerous online sites and print publications, is a contributing editor at Petri.com, and a frequent speaker at technology conferences and user groups.

comments powered by Disqus
Most   Popular

Upcoming Training Events

0 AM
Live! 360 Orlando
November 17-22, 2024
TechMentor @ Microsoft HQ
August 11-15, 2025