Book Image

SQL Server 2014 with PowerShell v5 Cookbook

By : Donabel Santos
Book Image

SQL Server 2014 with PowerShell v5 Cookbook

By: Donabel Santos

Overview of this book

Table of Contents (21 chapters)
SQL Server 2014 with PowerShell v5 Cookbook
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

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:

  1. Open PowerShell ISE as an administrator.

  2. 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 in a web service. We can achieve this using the New-WebProxy cmdlet, which accepts the web service URI:

$stockUri = "http://ws.cdyne.com/delayedstockquote/delayedstockquote.asmx"

This URI points to a free web service that provides delayed stock quote values. If we...