Extracting data from a web service
In this recipe, we will extract data from a free, public web service.
How to do it...
Let's explore how to access and retrieve data from a web service.
Open PowerShell ISE. Go to Start | Accessories | Windows PowerShell | Windows PowerShell ISE.
Add the following script and run it:
#delayed stock quote URI $stockUri = "http://ws.cdyne.com/delayedstockquote/delayedstockquote.asmx" $stockproxy = New-WebServiceProxy -Uri $stockUri -UseDefaultCredential #get quote $stockresult = $stockProxy.GetQuote("MSFT","") #display results $stockresult.StockSymbol $stockresult.DayHigh $stockresult.DayLow $stockresult.LastTradeDateTime
How it works...
To work with a web service, we first need to create a proxy object that will allow us to access the methods available from a web service. We can achieve this by using the New-WebProxy
cmdlet, which accepts the web service URL.
$stockUri = "http://ws.cdyne.com/delayedstockquote/delayedstockquote.asmx"
This URI points to a free...