Tuesday 30 December 2008

Get-StockQuote.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     Gets stock quotes using a web servoice 
  4. .DESCRIPTION 
  5.     This function uses the New-WebServiceProxy cmdlet to 
  6.     create a web service proxy. Then it calls that proxy 
  7.     to get stock information.  
  8. .NOTES 
  9.     File Name  : get-stockquote.ps1 
  10.     Author     : Thomas Lee - tfl@psp.co.uk 
  11.     Requires   : PowerShell V2 CTP3 
  12. .LINK 
  13.    Original post:   
  14.    http://PoshCode.org/embed/752 
  15.    Updated script posted: 
  16.    http://www.pshscripts.blogspot.com 
  17. .INPUTTYPE 
  18.    String(s) representing stock tickers to find 
  19. .RETURNVALUE 
  20.    XML Element, holding stock details 
  21. .EXAMPLE 
  22.     Run from PowerShell Prompt: 
  23.     Get-StockQuote "MSFT" 
  24.     Output - left as an exercise for the reader 
  25. .EXAMPLE 
  26.     Run from Pipeline 
  27.     "IBM","INTC" | Get-StockQuote 
  28.     Output - left as an exercise for the reader 
  29. .PARAMETER TICKER 
  30.     A string, or array of strings representing stock tickers 
  31.     The function gets stock details for each stock ticker provided 
  32. #> 
  33.  
  34. function Get-StockQuote { 
  35. param
  36. [Parameter(Position=0, Mandatory=$FALSE, ValueFromPipeline=$TRUE)]  
  37. [String] $TICKER="MSFT"
  38. process { 
  39.    $ticker 
  40.    $s = new-webserviceproxy -uri http://www.webservicex.net/stockquote.asmx 
  41.    foreach ($symbol in $ticker) { 
  42.       $result = [xml]$s.GetQuote($symbol
  43.       $result.StockQuotes.Stock 
  44.     } # end foreach 
  45. } #end process block 
  46. } # end function 
  47.  
  48.  
  49. "Example 1:" 
  50. "==========" 
  51. Get-StockQuote MSFT 
  52. "" 
  53. "Example 2:" 
  54. "==========" 
  55. "IBM","INTC" | Get-StockQuote 

No comments: