Calling a Web Service from PowerShell

Have you ever wanted to write a script that will get the weather forecast or maybe travel directions? Many sites expose web services that you can tap into from PowerShell to do things like converting currency, getting news headlines, or really anything! Maybe you already have created your own web service and simply want to consume it from PowerShell.

The first step is to create a Web Service Proxy to access the methods of the web service. This is done by using the New-WebServiceProxy cmdlet. When using this cmdlet point it to the url of your web service:

                                        
  $zip = New-WebServiceProxy -Uri http://www.webservicex.net/uszip.asmx?WSDL

                                        

Now that you have a Web Proxy object you can use the Get-Member cmdlet to list the methods that can be called:

                                        
  $zip | gm -MemberType method

  Name
  ----
  Abort
  BeginGetInfoByAreaCode
  BeginGetInfoByCity
  BeginGetInfoByState
  BeginGetInfoByZIP
  CancelAsync
  CreateObjRef
  Discover
  Dispose
  EndGetInfoByAreaCode
  EndGetInfoByCity
  EndGetInfoByState
  EndGetInfoByZIP
  Equals
  GetHashCode
  GetInfoByAreaCode
  GetInfoByAreaCodeAsync
  GetInfoByCity
  GetInfoByCityAsync
  GetInfoByState
  GetInfoByStateAsync
  GetInfoByZIP
  GetInfoByZIPAsync
  GetLifetimeService
  GetType
  InitializeLifetimeService
  ToString

                                        

Next, lets try calling the "GetInfoByZip" method by passing in a valid US Zip code. Note that this particular web service returns the results in a "Table" property so by ending the command with ".Table" the results will be displayed. However, you could do another Get-Member on the Table property and see how to return only specific pieces of information:

                                        
  $zip.GetInfoBYZip(43209).Table

  CITY      : Columbus
  STATE     : OH
  ZIP       : 43209
  AREA_CODE : 614
  TIME_ZONE : E

                                        

As you can see, the web service returned details about the City, State, and Zip code. This web service could be useful if you had a collection of zip codes and wanted to loop through them and get all the City and State names. The following returns City and State information:

                                        
  
  # Define an array of Zip Codes
  [array] $zipAry = @(43209,90210,33614)

  # Iterate each Zip code and get the City and State
  foreach($zipCode in $zipAry)
  {
    $zip.GetInfoByZip($zipCode).Table | Select City,State | Format-list
  }

  CITY  : Columbus
  STATE : OH

  CITY  : Beverly Hills
  STATE : CA

  CITY  : Tampa
  STATE : FL

                                        

The above command uses Select-Object to only return the City and State properties. It also uses Format-List to display the output in a list style. There are lots of web services like this one that are freely accessible on the web.

Bonus Tip:

  Google offers a Directions API Web Service that you can use for getting travel directions.

                                        


© 2024 Embrs.net