Hey, Scripting Guy! How Can I Quickly Check Stocks with Windows PowerShell?

ScriptingGuy1

Bookmark and Share

 

Hey, Scripting Guy! Question

Hey, Scripting Guy! I have a customized MSN home page. I really like the new layout, but it is rather inefficient to open Internet Explorer, and wait for a few minutes just to check out the latest stock prices. What I need is a Windows PowerShell script that I can use to quickly retrieve my stock information without the need to open Internet Explorer and scroll down half the page to find a current stock quote. I wrote a Windows PowerShell script that opens a page in Internet Explorer and scrapes the information, but it is only marginally faster than clicking on the Internet Explorer icon on my quick launch bar. Can you write a Windows PowerShell script to retrieve quickly a stock quote?

— HM

Hey, Scripting Guy! AnswerHello HM,

Microsoft Scripting Guy Ed Wilson here. the cool thing about using Windows PowerShell 2.0 to connect to Web services is there are hundreds of Web services on the Internet. Some are highly specialized and rather complex, and others such as the one for retrieving stock quotes are very straightforward. By performing a Bing search for “Web service” or for “WSDL,” you can find hundreds of free and easy-to-use Web services that will do everything from resolving a ZIP code to performing currency conversions.

HM, the Get-StockQuote.ps1 script uses a Web service to retrieve the latest quote for a given stock. The complete Get-StockQuote.ps1 script is seen here.

Get-StockQuote.ps1

#Requires -Version 2.0

Function Get-StockQuote($symbol)

{

 $URI = “http://www.webservicex.net/StockQuote.asmx?WSDL”

 $stockProxy = New-WebServiceProxy -uri $URI -namespace WebServiceProxy -class stock

 $stockProxy.getQuote($symbol)

} #end Get-StockQuote

 

$quote = Get-StockQuote -symbol msft

[xml]$quote |

ForEach-Object {

 $_.stockQuotes.Stock

} #end Foreach-Object

The Get-StockQuote.ps1 script begins with the #Requires –Version 2.0 tag. This is a best practice to prevent the script from attempting to run on a computer with Windows PowerShell 1.0 installed. Because the New-WebServiceProxy cmdlet does not work on Windows PowerShell 1.0, the script would generate an error. The #Requires command is shown here:

#Requires -Version 2.0

It then uses the Function keyword to create the Get-StockQuote function. The Get-StockQuote function accepts a single parameter—the stock symbol. This is shown here:

Function Get-StockQuote($symbol)

{

The path to the StockQuote Web Service Definition Language (WSDL) is assigned to the $URI variable. The WSDL describes the Web service. As seen in the following image, the StockQuote Web service contains a method named GetQuote that accepts a stock symbol as a string:

Image of GetQuote method


The path to the StockQuote WSDL is assigned to the $URI variable as shown here:

$URI = “http://www.webservicex.net/StockQuote.asmx?WSDL”

The New-WebServiceProxy cmdlet is used to create a new Web service proxy for the StockQuote Web service. To do this, the New-WebServiceProxy cmdlet requires the path to the WSDL for the StockQuote Web service. The resulting proxy is stored in the $stockProxy variable as shown here:

$stockProxy = New-WebServiceProxy -uri $URI -namespace WebServiceProxy -class stock

The getQuote method from the StockQuote Web service is used to retrieve the stock quote information. The getQuote method requires a stock symbol as a string to return the information. This is shown here:

$stockProxy.getQuote($symbol)

} #end Get-StockQuote

The entry point to the script calls the Get-StockQuote function and passes a string value for the –symbol parameter. The returned data is stored in the $quote variable. This is seen here:

$quote = Get-StockQuote -symbol msft

The XML type accelerator ([xml]) is used to cast the data that is returned from the Get-StockQuote function into an XML document. The ForEach-Object cmdlet is used to walk through the document, and the stock property is used to return the stock quote. This code is seen here:

[xml]$quote |

ForEach-Object {

 $_.stockQuotes.Stock

}

When the Get-StockQuote.ps1 script runs, the following output is displayed:

Image of script output

 

Well, HM, as you can see, Web services are fun and can be relatively easy to use from within Windows PowerShell 2.0. Join us tomorrow as we open the virtual mailbag and address questions that require short answers. That’s right, it is time for Quick-Hits Friday. 

If you want to know exactly what we will be looking at tomorrow, follow us on Twitter or Facebook. If you have any questions, send e-mail to us at scripter@microsoft.com or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

 

Ed Wilson and Craig Liebendorfer, Scripting Guys

 

0 comments

Discussion is closed.

Feedback usabilla icon