![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | Function - XML I've created this function to recieve an XML Document from one of our Web servers. The $Quote.Quote.BestBuyPrice returns the value of System.XML.XMLDocument.Quote.BestBuyPrice. When I run it as a normal script it gets the correct data, is there different syntax etc for functions? function Get-Quote { param([String]$Code) $WebClient = New-Object System.Net.WebClient [xml]$Quote = $Webclient.DownloadString("http://server:1891/MarketInfoService.asmx/LiveQuote?Code="+$Code) Start-Sleep -Seconds 2 #While it recieves the results. Write-Host "The current best buy price for $Code is $Quote.Quote.BestBuyPrice at $Quote.Quote.QuoteTime } |
My System Specs![]() |
| | #2 (permalink) |
| | Re: Function - XML NeilOz wrote: Quote: > I've created this function to recieve an XML Document from one of our Web > servers. > The $Quote.Quote.BestBuyPrice returns the value of > System.XML.XMLDocument.Quote.BestBuyPrice. When I run it as a normal script > it gets the correct data, is there different syntax etc for functions? > > function Get-Quote > { > param([String]$Code) > $WebClient = New-Object System.Net.WebClient > [xml]$Quote = > $Webclient.DownloadString("http://server:1891/MarketInfoService.asmx/LiveQuote?Code="+$Code) > Start-Sleep -Seconds 2 #While it recieves the results. > Write-Host "The current best buy price for $Code is > $Quote.Quote.BestBuyPrice at $Quote.Quote.QuoteTime > } 1. Put this entire function in a ps1 script, then simply call get-quote on the script. 2. Enter this function from the command line directly into your current shell, then call get-quote directly. You're saying that #1 doesn't work the same as #2? How about putting the function into a ps1 script, dot-sourcing that ps1, and then trying the function? Really all 3 of these should work exactly the same, all things being equal. Do you get any output/errors? -- Microsoft MVP - Windows PowerShell http://www.microsoft.com/mvp PowerGadgets MVP http://www.powergadgets.com/mvp Blog: http://marcoshaw.blogspot.com |
My System Specs![]() |
| | #3 (permalink) |
| | Re: Function - XML if i go $Code = "xxx" $WebClient = New-Object System.Net.WebClient [xml]$Quote = $Webclient.DownloadString("http://server:1891/MarketInfoService.asmx/LiveQuote?Code="+$Code) $Time = $Quote.Quote.QuoteTime $BestBuy = $Quote.Quote.BestBuyPrice $time and $bestbuy both come up with the correct figures. But from within the function they return "System.Xml.XmlDOcument.Quote.BestBuyPrice" After . sourcing the .ps1 and running $Code is returns the correct value, so the issue I believe is with the line [xml]$Quote = $Webclient.DownloadString("http://server:1891/MarketInfoService.asmx/LiveQuote?Code="+$Code). "Marco Shaw [MVP]" wrote: Quote: > NeilOz wrote: Quote: > > I've created this function to recieve an XML Document from one of our Web > > servers. > > The $Quote.Quote.BestBuyPrice returns the value of > > System.XML.XMLDocument.Quote.BestBuyPrice. When I run it as a normal script > > it gets the correct data, is there different syntax etc for functions? > > > > function Get-Quote > > { > > param([String]$Code) > > $WebClient = New-Object System.Net.WebClient > > [xml]$Quote = > > $Webclient.DownloadString("http://server:1891/MarketInfoService.asmx/LiveQuote?Code="+$Code) > > Start-Sleep -Seconds 2 #While it recieves the results. > > Write-Host "The current best buy price for $Code is > > $Quote.Quote.BestBuyPrice at $Quote.Quote.QuoteTime > > } > OK so you: > 1. Put this entire function in a ps1 script, then simply call get-quote > on the script. > 2. Enter this function from the command line directly into your current > shell, then call get-quote directly. > > You're saying that #1 doesn't work the same as #2? > > How about putting the function into a ps1 script, dot-sourcing that ps1, > and then trying the function? > > Really all 3 of these should work exactly the same, all things being equal. > > Do you get any output/errors? > > -- > Microsoft MVP - Windows PowerShell > http://www.microsoft.com/mvp > > PowerGadgets MVP > http://www.powergadgets.com/mvp > > Blog: > http://marcoshaw.blogspot.com > |
My System Specs![]() |
| | #4 (permalink) |
| | Re: Function - XML My own silly mistake. Here is the correct code.. function Get-Quote { param([String]$Code) $WebClient = New-Object System.Net.WebClient [xml]$Quote = $Webclient.DownloadString("http://server:1891/MarketInfoService.asmx/LiveQuote?Code=$Code") Start-Sleep -Seconds 2 #While it recieves the results. Write-Host "The current best buy price for $Code is" return $Quote.Quote.BestBuyPrice "at" $Quote.Quote.QuoteTime } "NeilOz" wrote: Quote: > if i go > > $Code = "xxx" > $WebClient = New-Object System.Net.WebClient > [xml]$Quote = > $Webclient.DownloadString("http://server:1891/MarketInfoService.asmx/LiveQuote?Code="+$Code) > $Time = $Quote.Quote.QuoteTime > $BestBuy = $Quote.Quote.BestBuyPrice > > $time and $bestbuy both come up with the correct figures. But from within > the function they return "System.Xml.XmlDOcument.Quote.BestBuyPrice" > > After . sourcing the .ps1 and running $Code is returns the correct value, so > the issue I believe is with the line [xml]$Quote = > $Webclient.DownloadString("http://server:1891/MarketInfoService.asmx/LiveQuote?Code="+$Code). > > > "Marco Shaw [MVP]" wrote: > Quote: > > NeilOz wrote: Quote: > > > I've created this function to recieve an XML Document from one of our Web > > > servers. > > > The $Quote.Quote.BestBuyPrice returns the value of > > > System.XML.XMLDocument.Quote.BestBuyPrice. When I run it as a normal script > > > it gets the correct data, is there different syntax etc for functions? > > > > > > function Get-Quote > > > { > > > param([String]$Code) > > > $WebClient = New-Object System.Net.WebClient > > > [xml]$Quote = > > > $Webclient.DownloadString("http://server:1891/MarketInfoService.asmx/LiveQuote?Code="+$Code) > > > Start-Sleep -Seconds 2 #While it recieves the results. > > > Write-Host "The current best buy price for $Code is > > > $Quote.Quote.BestBuyPrice at $Quote.Quote.QuoteTime > > > } > > OK so you: > > 1. Put this entire function in a ps1 script, then simply call get-quote > > on the script. > > 2. Enter this function from the command line directly into your current > > shell, then call get-quote directly. > > > > You're saying that #1 doesn't work the same as #2? > > > > How about putting the function into a ps1 script, dot-sourcing that ps1, > > and then trying the function? > > > > Really all 3 of these should work exactly the same, all things being equal. > > > > Do you get any output/errors? > > > > -- > > Microsoft MVP - Windows PowerShell > > http://www.microsoft.com/mvp > > > > PowerGadgets MVP > > http://www.powergadgets.com/mvp > > > > Blog: > > http://marcoshaw.blogspot.com > > |
My System Specs![]() |
| | #5 (permalink) |
| | Re: Function - XML NeilOz, You can remove the sleep statement, it doesn't count. Can you post a sample XML result? ----- Shay Levi $cript Fanatic http://scriptolog.blogspot.com Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic Quote: > My own silly mistake. Here is the correct code.. > > function Get-Quote > { > param([String]$Code) > $WebClient = New-Object System.Net.WebClient > [xml]$Quote = > $Webclient.DownloadString("http://server:1891/MarketInfoService.asmx/L > iveQuote?Code=$Code") > Start-Sleep -Seconds 2 #While it recieves the results. > Write-Host "The current best buy price for $Code is" return > $Quote.Quote.BestBuyPrice "at" $Quote.Quote.QuoteTime > } > > "NeilOz" wrote: > Quote: >> if i go >> >> $Code = "xxx" >> $WebClient = New-Object System.Net.WebClient >> [xml]$Quote = >> $Webclient.DownloadString("http://server:1891/MarketInfoService.asmx/ >> LiveQuote?Code="+$Code) >> $Time = $Quote.Quote.QuoteTime >> $BestBuy = $Quote.Quote.BestBuyPrice >> $time and $bestbuy both come up with the correct figures. But from >> within the function they return >> "System.Xml.XmlDOcument.Quote.BestBuyPrice" >> >> After . sourcing the .ps1 and running $Code is returns the correct >> value, so the issue I believe is with the line [xml]$Quote = >> $Webclient.DownloadString("http://server:1891/MarketInfoService.asmx/ >> LiveQuote?Code="+$Code). >> >> "Marco Shaw [MVP]" wrote: >> Quote: >>> NeilOz wrote: >>> >>>> I've created this function to recieve an XML Document from one of >>>> our Web >>>> servers. >>>> The $Quote.Quote.BestBuyPrice returns the value of >>>> System.XML.XMLDocument.Quote.BestBuyPrice. When I run it as a >>>> normal script >>>> it gets the correct data, is there different syntax etc for >>>> functions? >>>> function Get-Quote >>>> { >>>> param([String]$Code) >>>> $WebClient = New-Object System.Net.WebClient >>>> [xml]$Quote = >>>> $Webclient.DownloadString("http://server:1891/MarketInfoService.asm >>>> x/LiveQuote?Code="+$Code) >>>> Start-Sleep -Seconds 2 #While it recieves the results. >>>> Write-Host "The current best buy price for $Code is >>>> $Quote.Quote.BestBuyPrice at $Quote.Quote.QuoteTime >>>> } >>> OK so you: >>> 1. Put this entire function in a ps1 script, then simply call >>> get-quote >>> on the script. >>> 2. Enter this function from the command line directly into your >>> current >>> shell, then call get-quote directly. >>> You're saying that #1 doesn't work the same as #2? >>> >>> How about putting the function into a ps1 script, dot-sourcing that >>> ps1, and then trying the function? >>> >>> Really all 3 of these should work exactly the same, all things being >>> equal. >>> >>> Do you get any output/errors? >>> >>> -- >>> Microsoft MVP - Windows PowerShell >>> http://www.microsoft.com/mvp >>> PowerGadgets MVP >>> http://www.powergadgets.com/mvp >>> Blog: >>> http://marcoshaw.blogspot.com |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| About the function. | PowerShell | |||
| function to delete a function | PowerShell | |||
| function not working dispite working outside the function block | PowerShell | |||
| help about function | PowerShell | |||
| BUG: Redirecting function contents to a file truncates function lines at the width of the console | PowerShell | |||