Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
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.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Function - XML

Reply
 
Old 01-09-2008   #1 (permalink)
NeilOz


 
 

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 SpecsSystem Spec
Old 01-09-2008   #2 (permalink)
Marco Shaw [MVP]


 
 

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
> }
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 SpecsSystem Spec
Old 01-09-2008   #3 (permalink)
NeilOz


 
 

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 SpecsSystem Spec
Old 01-09-2008   #4 (permalink)
NeilOz


 
 

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 SpecsSystem Spec
Old 01-10-2008   #5 (permalink)
Shay Levi


 
 

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 SpecsSystem Spec
Reply

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


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46