Love to code, although it bugs me.

Windows Powershell: Invoking a Web Service

No comments
About a year ago I posted about invoking a Web Service using Windows Powershell v3 CTP 1. Powershell v3 is the standard on Windows 8 and Windows 2012 Server. However, I learned that you don’t need to use v3, because there is a very nice cmdlet one can use to invoke Web services through a proxy object. It’s called New-WebServiceProxy.

So here is the example of invoking a stock quote Web service using powershell with the proxy cmdlet:
 
 


  1. $URI = "http://www.webservicex.net/stockquote.asmx?WSDL" 
  2. $stock = New-WebServiceProxy -uri $URI  
  3. $xml = [xml]$stock.getQuote("MSFT"
  4. $xml.StockQuotes.Stock 

The outcome should be something like this:

image 
On the above example I invoked the “getQuote” method. After instantiating the proxy on the $stock variable, if you’d like to learn what methods are available, as well as the parameters and return types, you can type at the prompt:

 
 


  1. $stock | get-member -type method 

Overall, it’s a more powerful solution than the previous one I presented because you get to instantiate a proxy object with all the methods and properties of the Web service and it’s more compatible since you don’t need to upgrade.

Happy coding!

No comments :

Post a Comment