{"id":51973,"date":"2009-11-19T00:01:00","date_gmt":"2009-11-19T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2009\/11\/19\/hey-scripting-guy-how-can-i-quickly-check-stocks-with-windows-powershell\/"},"modified":"2009-11-19T00:01:00","modified_gmt":"2009-11-19T00:01:00","slug":"hey-scripting-guy-how-can-i-quickly-check-stocks-with-windows-powershell","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-quickly-check-stocks-with-windows-powershell\/","title":{"rendered":"Hey, Scripting Guy! How Can I Quickly Check Stocks with Windows PowerShell?"},"content":{"rendered":"<p><!-- AddThis Button BEGIN --><a class=\"addthis_button\" href=\"http:\/\/www.addthis.com\/bookmark.php?v=250&amp;pub=scriptingguys\"><img decoding=\"async\" alt=\"Bookmark and Share\" src=\"http:\/\/s7.addthis.com\/static\/btn\/v2\/lg-share-en.gif\" width=\"125\" height=\"16\"><\/a><!-- AddThis Button END --><\/p>\n<p class=\"MsoNormal\">&nbsp;<\/p>\n<p><img decoding=\"async\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"><\/p>\n<p class=\"MsoNormal\">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?<\/p>\n<p class=\"MsoNormal\">&#8212; HM<\/p>\n<p class=\"MsoNormal\"><img decoding=\"async\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\">Hello HM, <\/p>\n<p class=\"MsoNormal\">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 <a href=\"http:\/\/www.bing.com\/\"><font face=\"Segoe\">Bing search<\/font><\/a> for &ldquo;Web service&rdquo; or for &ldquo;WSDL,&rdquo; 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. <\/p>\n<p class=\"MsoNormal\">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. <\/p>\n<p class=\"CodeBlockScreenedHead\"><strong>Get-StockQuote.ps1<\/strong><\/p>\n<p class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\">#Requires -Version 2.0<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\">Function Get-StockQuote($symbol)<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\">{<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\"><span>&nbsp;<\/span>$URI = &#8220;http:\/\/www.webservicex.net\/StockQuote.asmx?WSDL&#8221;<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\"><span>&nbsp;<\/span>$stockProxy = New-WebServiceProxy -uri $URI -namespace WebServiceProxy -class stock<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\"><span>&nbsp;<\/span>$stockProxy.getQuote($symbol) <\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\">} #end Get-StockQuote<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\">&nbsp;<\/font><\/p>\n<p><\/span><\/p>\n<p class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\">$quote = Get-StockQuote -symbol msft<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\">[xml]$quote |<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\">ForEach-Object {<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\"><span>&nbsp;<\/span>$_.stockQuotes.Stock<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\">} #end Foreach-Object<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">The Get-StockQuote.ps1 script begins with the <b>#Requires &ndash;Version 2.0<\/b> 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 <b>New-WebServiceProxy<\/b> cmdlet does not work on Windows PowerShell 1.0, the script would generate an error. The <b>#Requires<\/b> command is shown here:<\/p>\n<p class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\">#Requires -Version 2.0<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">It then uses the <b>Function<\/b> keyword to create the <b>Get-StockQuote<\/b> function. The <b>Get-StockQuote<\/b> function accepts a single parameter&mdash;the stock symbol. This is shown here:<\/p>\n<p class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\">Function Get-StockQuote($symbol)<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlockScreened\"><span><font face=\"Lucida Sans Typewriter\">{<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">The path to the <b>StockQuote<\/b> Web Service Definition Language (WSDL) is assigned to the <b>$URI<\/b> variable. The WSDL describes the Web service. As seen in the following image, the <b>StockQuote<\/b> Web service contains a method named <b>GetQuote<\/b> that accepts a stock symbol as a string:<\/p>\n<p class=\"Fig-Graphic\"><img decoding=\"async\" title=\"Image of GetQuote method\" alt=\"Image of GetQuote method\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2009\/november\/hey1119\/hsg-11-19-09-01.jpg\" width=\"600\" height=\"421\"><\/p>\n<p class=\"MsoNormal\"><br>The path to the <b>StockQuote<\/b> WSDL is assigned to the <b>$URI<\/b> variable as shown here:<\/p>\n<p class=\"MsoNormal\"><span>$URI = &#8220;http:\/\/www.webservicex.net\/StockQuote.asmx?WSDL&#8221;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\">The <b>New-WebServiceProxy<\/b> cmdlet is used to create a new Web service proxy for the <b>StockQuote<\/b> Web service. To do this, the <b>New-WebServiceProxy<\/b> cmdlet requires the path to the WSDL for the <b>StockQuote<\/b> Web service. The resulting proxy is stored in the <b>$stockProxy<\/b> variable as shown here:<\/p>\n<p class=\"MsoNormal\"><span>$stockProxy = New-WebServiceProxy -uri $URI -namespace WebServiceProxy -class stock<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\">The <b>getQuote<\/b> method from the <b>StockQuote<\/b> Web service is used to retrieve the stock quote information. The <b>getQuote<\/b> method requires a stock symbol as a string to return the information. This is shown here: <\/p>\n<p class=\"MsoNormal\"><span>$stockProxy.getQuote($symbol) <\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span>} #end Get-StockQuote<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\">The entry point to the script calls the <b>Get-StockQuote<\/b> function and passes a string value for the <b>&ndash;symbol<\/b> parameter. The returned data is stored in the <b>$quote<\/b> variable. This is seen here:<\/p>\n<p class=\"MsoNormal\"><span>$quote = Get-StockQuote -symbol msft<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\">The XML type accelerator (<b>[xml]<\/b>) is used to cast the data that is returned from the <b>Get-StockQuote<\/b> function into an XML document. The <b>ForEach-Object<\/b> cmdlet is used to walk through the document, and the <b>stock<\/b> property is used to return the stock quote. This code is seen here: <\/p>\n<p class=\"MsoNormal\"><span>[xml]$quote |<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span>ForEach-Object {<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;<\/span>$_.stockQuotes.Stock <\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span>}<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\">When the Get-StockQuote.ps1 script runs, the following output is displayed:<\/p>\n<p class=\"Fig-Graphic\"><img decoding=\"async\" title=\"Image of script output\" alt=\"Image of script output\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2009\/november\/hey1119\/hsg-11-19-09-02.jpg\" width=\"600\" height=\"420\"><\/p>\n<p class=\"Fig-Graphic\">&nbsp;<\/p>\n<p class=\"MsoNormal\">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&rsquo;s right, it is time for Quick-Hits Friday.<span>&nbsp; <\/span><\/p>\n<p class=\"MsoNormal\">If you want to know exactly what we will be looking at tomorrow, follow us on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\"><font face=\"Segoe\">Twitter<\/font><\/a> or <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\"><font face=\"Segoe\">Facebook<\/font><\/a>. If you have any questions, send e-mail to us at <a href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\" target=\"_blank\"><font face=\"Segoe\">scripter@microsoft.com<\/font><\/a> or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p class=\"MsoNormal\">&nbsp;<\/p>\n<p><b><span>Ed Wilson and Craig Liebendorfer, Scripting Guys<\/p>\n<p><\/span><\/b><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; 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 [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[3,4,45,165],"class_list":["post-51973","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell","tag-xml"],"acf":[],"blog_post_summary":"<p>&nbsp; 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 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/51973","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=51973"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/51973\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=51973"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=51973"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=51973"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}