Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

String expansion question

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 11-07-2007   #1 (permalink)
gurbao
Guest


 

String expansion question

Hi,

I am looping through a xml construct and when displaying the $variable
I see what the xmlelement contains.
When I add this in a string (for expansion) powershell displays the
type.

foreach ($instance in $server.instance) {
$instance #displays instance as expected
$sqlagent="SQL Server Agent ($instance)" " #displays SQL Server
Agent (System.Xml.XmlElement)"

What is the trick here?

Thanks,


My System SpecsSystem Spec
Old 11-07-2007   #2 (permalink)
Oisin Grehan
Guest


 

Re: String expansion question

On Nov 7, 5:47 pm, gurbao <aud...@xxxxxx> wrote:
Quote:

> Hi,
>
> I am looping through a xml construct and when displaying the $variable
> I see what the xmlelement contains.
> When I add this in a string (for expansion) powershell displays the
> type.
>
> foreach ($instance in $server.instance) {
> $instance #displays instance as expected
> $sqlagent="SQL Server Agent ($instance)" " #displays SQL Server
> Agent (System.Xml.XmlElement)"
>
> What is the trick here?
>
> Thanks,
Try "SQL Server Agent ($($instance.get_innerText()))"

Hope this helps,

- Oisin

My System SpecsSystem Spec
Old 11-08-2007   #3 (permalink)
gurbao
Guest


 

Re: String expansion question

On Nov 8, 12:06 am, Oisin Grehan <ois...@xxxxxx> wrote:
Quote:

> On Nov 7, 5:47 pm, gurbao <aud...@xxxxxx> wrote:
>
Quote:

> > Hi,
>
Quote:

> > I am looping through a xml construct and when displaying the $variable
> > I see what the xmlelement contains.
> > When I add this in a string (for expansion) powershell displays the
> > type.
>
Quote:

> > foreach ($instance in $server.instance) {
> > $instance #displays instance as expected
> > $sqlagent="SQL Server Agent ($instance)" " #displays SQL Server
> > Agent (System.Xml.XmlElement)"
>
Quote:

> > What is the trick here?
>
Quote:

> > Thanks,
>
> Try "SQL Server Agent ($($instance.get_innerText()))"
>
> Hope this helps,
>
> - Oisin
Unfortunately that didn't help :-(
It retuns an empty string.

Other tips?

Thanks,

My System SpecsSystem Spec
Old 11-08-2007   #4 (permalink)
Shay Levi
Guest


 

Re: String expansion question

> I am looping through a xml construct and when displaying the $variable
Quote:

> I see what the xmlelement contains.
When you see the element content, does it display correctly? If so try to
convert it to string:

$variable.ToString()

I can't test your code since it doesn't include all statments. Try to post
A working example, it can help.

-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com


Quote:

> Hi,
>
> I am looping through a xml construct and when displaying the $variable
> I see what the xmlelement contains.
> When I add this in a string (for expansion) powershell displays the
> type.
> foreach ($instance in $server.instance) {
> $instance #displays instance as expected
> $sqlagent="SQL Server Agent ($instance)" " #displays SQL Server
> Agent (System.Xml.XmlElement)"
> What is the trick here?
>
> Thanks,
>

My System SpecsSystem Spec
Old 11-08-2007   #5 (permalink)
gurbao
Guest


 

Re: String expansion question

On Nov 8, 9:03 am, Shay Levi <n...@xxxxxx> wrote:
Quote:
Quote:

> > I am looping through a xml construct and when displaying the $variable
> > I see what the xmlelement contains.
>
> When you see the element content, does it display correctly? If so try to
> convert it to string:
>
> $variable.ToString()
>
> I can't test your code since it doesn't include all statments. Try to post
> A working example, it can help.
>
> -----
> Shay Levi
> $cript Fanatichttp://scriptolog.blogspot.com
>
Quote:

> > Hi,
>
Quote:

> > I am looping through a xml construct and when displaying the $variable
> > I see what the xmlelement contains.
> > When I add this in a string (for expansion) powershell displays the
> > type.
> > foreach ($instance in $server.instance) {
> > $instance #displays instance as expected
> > $sqlagent="SQL Server Agent ($instance)" " #displays SQL Server
> > Agent (System.Xml.XmlElement)"
> > What is the trick here?
>
Quote:

> > Thanks,
Ok, here is the example.

Save the following as c:\temp\servers2.xml:
<?xml version="1.0" encoding="utf-8"?>
<servers>
<server servername = "ws4762">
<instance instancename = "defaultinstance"></instance>
<instance instancename = "s2000"></instance>
</server>
</servers>

Then try this script:
$servers = [xml](gc "c:\temp\servers2.xml")
foreach ($server in $servers.servers.server) {
foreach ($instance in $server.instance) {
$instance # this displays the instancename
"test $instance.ToString()" # here I miss something :-)
}
}

Give it a go and enlighten me :-)

Thanks,

My System SpecsSystem Spec
Old 11-08-2007   #6 (permalink)
Shay Levi
Guest


 

Re: String expansion question

Here you go:

$servers = [xml](gc "c:\temp\servers2.xml")
foreach ($server in $servers.servers.server) {
foreach ($instance in $server.instance) {
#$instance.instancename
$sqlagent="SQL Server Agent $($instance.instancename)"
write-host $sqlagent
}
}



defaultinstance
SQL Server Agent defaultinstance
s2000
SQL Server Agent s2000






-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com


Quote:

> On Nov 8, 9:03 am, Shay Levi <n...@xxxxxx> wrote:
>
Quote:
Quote:

>>> I am looping through a xml construct and when displaying the
>>> $variable I see what the xmlelement contains.
>>>
>> When you see the element content, does it display correctly? If so
>> try to convert it to string:
>>
>> $variable.ToString()
>>
>> I can't test your code since it doesn't include all statments. Try to
>> post A working example, it can help.
>>
>> -----
>> Shay Levi
>> $cript Fanatichttp://scriptolog.blogspot.com
Quote:

>>> Hi,
>>>
>>> I am looping through a xml construct and when displaying the
>>> $variable
>>> I see what the xmlelement contains.
>>> When I add this in a string (for expansion) powershell displays the
>>> type.
>>> foreach ($instance in $server.instance) {
>>> $instance #displays instance as expected
>>> $sqlagent="SQL Server Agent ($instance)" " #displays SQL Server
>>> Agent (System.Xml.XmlElement)"
>>> What is the trick here?
>>> Thanks,
>>>
> Ok, here is the example.
>
> Save the following as c:\temp\servers2.xml:
> <?xml version="1.0" encoding="utf-8"?>
> <servers>
> <server servername = "ws4762">
> <instance instancename = "defaultinstance"></instance>
> <instance instancename = "s2000"></instance>
> </server>
> </servers>
> Then try this script:
> $servers = [xml](gc "c:\temp\servers2.xml")
> foreach ($server in $servers.servers.server) {
> foreach ($instance in $server.instance) {
> $instance # this displays the instancename
> "test $instance.ToString()" # here I miss something :-)
> }
> }
>
> Give it a go and enlighten me :-)
>
> Thanks,
>

My System SpecsSystem Spec
Old 11-08-2007   #7 (permalink)
gurbao
Guest


 

Re: String expansion question

On Nov 8, 11:11 am, Shay Levi <n...@xxxxxx> wrote:
Quote:

> Here you go:
>
> $servers = [xml](gc "c:\temp\servers2.xml")
> foreach ($server in $servers.servers.server) {
> foreach ($instance in $server.instance) {
> #$instance.instancename
> $sqlagent="SQL Server Agent $($instance.instancename)"
> write-host $sqlagent
> }
>
> }
>
> defaultinstance
> SQL Server Agent defaultinstance
> s2000
> SQL Server Agent s2000
>
> -----
> Shay Levi
> $cript Fanatichttp://scriptolog.blogspot.com
>
Quote:

> > On Nov 8, 9:03 am, Shay Levi <n...@xxxxxx> wrote:
>
Quote:
Quote:

> >>> I am looping through a xml construct and when displaying the
> >>> $variable I see what the xmlelement contains.
>
Quote:
Quote:

> >> When you see the element content, does it display correctly? If so
> >> try to convert it to string:
>
Quote:
Quote:

> >> $variable.ToString()
>
Quote:
Quote:

> >> I can't test your code since it doesn't include all statments. Try to
> >> post A working example, it can help.
>
Quote:
Quote:

> >> -----
> >> Shay Levi
> >> $cript Fanatichttp://scriptolog.blogspot.com
> >>> Hi,
>
Quote:
Quote:

> >>> I am looping through a xml construct and when displaying the
> >>> $variable
> >>> I see what the xmlelement contains.
> >>> When I add this in a string (for expansion) powershell displays the
> >>> type.
> >>> foreach ($instance in $server.instance) {
> >>> $instance #displays instance as expected
> >>> $sqlagent="SQL Server Agent ($instance)" " #displays SQL Server
> >>> Agent (System.Xml.XmlElement)"
> >>> What is the trick here?
> >>> Thanks,
>
Quote:

> > Ok, here is the example.
>
Quote: