Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Pulling data from a XML type variable

703 views
Skip to first unread message

OldDog

unread,
Jun 15, 2010, 5:00:08 PM6/15/10
to
Hi,

I have a command line utility that queries the HBA's on a server and
return all sorts of usefull information.
What I am looking for is the World Wide Name of each port.


The Output looks like this:


<?xml version="1.0" encoding="ISO-8859-1"?>
<QLogic>
<AppName>SANsurfer FC CLI</AppName>
<AppVersion>v1.06.14 Build 25</AppVersion>
<HBA>
<GeneralInfo Number="0"
Model="QLA2312"
WWNN="##-##-##-##-##-##-##-##"
WWPN="##-##-##-##-##-##-##-##"


</HBA>
<HBA>
<GeneralInfo Number="1"


Model="QLA2312"
WWNN="##-##-##-##-##-##-##-##"
WWPN="##-##-##-##-##-##-##-##"
</HBA>


I get this info by running this:


$psinfoOutput = c:\psTools\psexec.exe \\$strComputer -w "C:\Program
Files\SANsurferCLI" "C:\Program Files\SANsurferCLI\scli.exe" -I ALL -
X


So, I need to parse $psinfoOutput for my data.


I tried this:


$xd.load($psinfoOutput)
$nodelist = $xd.selectnodes("//*[@Name=SANsurfer FC CLI']\HBA")
foreach ($testCaseNode in $nodelist) {
$WWNN = $testCaseNode.getAttribute("WWNN")
$WWPN = $testCaseNode.getAttribute("WWPN")
$ws.Cells.Item($rRow,16) = $WWNN
$ws.Cells.Item($rRow,17) = $WWPN


But it's not working. I think I need to get down intop the
GeneralInfo
level, But I don't know how.


TIA,


OLDDOG


PaulChavez

unread,
Jun 15, 2010, 5:54:03 PM6/15/10
to

That XPath looks funky to me, but much like regex I can't really tell if it's
wrong or not.

If you run this in session or with a breakpoint what does the selectnodes()
method return?


"OldDog" wrote:

> .
>

OldDog

unread,
Jun 16, 2010, 2:30:56 PM6/16/10
to
On Jun 15, 4:54 pm, PaulChavez <PaulCha...@discussions.microsoft.com>
wrote:
> > .- Hide quoted text -
>
> - Show quoted text -

Hi,

I won't "Load":

$xd.load($psinfoOutput)

You cannot call a method on a null-valued expression.
At line:1 char:9
+ $xd.load <<<< ($psinfoOutput)
+ CategoryInfo : InvalidOperation: (load:String) [],
RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull

Looks like I need to pull the data in another way. I can creat a
temporary xml file.

But when I do that, I can get any further down the tree than:

GeneralInfo
-----------
GeneralInfo
GeneralInfo


PS C:\temp> cd scripts:
PS Scripts:\> $xml=[xml](Get-Content \\MYTESTSERVER\c$\temp
\output.xml)
PS Scripts:\> $a = $xml.SelectNodes("//*[@Name='QLogic']/HBA/
GeneralInfo")
PS Scripts:\> $a | select Number,Model,WWNN,WWPN
PS Scripts:\> $a
PS Scripts:\>
PS Scripts:\> $a.number
PS Scripts:\> $xml

xml QLogic
--- ------
version="1.0" encoding="ISO-8859-1" QLogic

If I do this:

$xml.QLogic

I get this:

AppName : SANsurfer FC CLI
AppVersion : v1.06.14 Build 25
HBA : {HBA, HBA}
Status : 0
Reboot : 0

And this:

$xml.QLogic.HBA

Gets this

GeneralInfo
-----------
GeneralInfo
GeneralInfo

If i try this

$xml.QLogic.HBA.GeneralInfo

I get nothing

PaulChavez

unread,
Jun 16, 2010, 3:59:57 PM6/16/10
to
As for loading the string into an XML object, you should be able to do this:

$xml = [xml]$psinfoOutput

As far as the actual XML, I don't know how much you edited what you actually
are getting back but the sample you posted is not well formed. The
<GeneralInfo> tag is not closed.

Assuming that is just your editing, then your final example
$xml.QLogic.HBA.GeneralInfo still won't return anything because there are two
HBA tags so you'd need to index into one:

$xml.QLogic.HBA[0].GeneralInfo

Running with a fixed up version of your example the following sample code
returned expected data:

$xml.SelectNodes('//GeneralInfo') | %{ $_.WWNN; $_.WWPN }

Hope that helps,
paul

"OldDog" wrote:

> .
>

OldDog

unread,
Jun 16, 2010, 4:25:04 PM6/16/10
to
On Jun 16, 2:59 pm, PaulChavez <PaulCha...@discussions.microsoft.com>
wrote:

PS Scripts:\> $xml = [xml]$psinfoOutput
Cannot convert value "System.Object[]" to type
"System.Xml.XmlDocument". Error: "Unexpected XML dec
laration. The XML declaration must be the first node in the document,
and no white space characters
are allowed to appear before it. Line 2, position 3."
At line:1 char:26
+ $xml = [xml]$psinfoOutput <<<<
+ CategoryInfo : NotSpecified: (:) [], RuntimeException
+ FullyQualifiedErrorId : RuntimeException


Here is my full XML file:

<?xml version="1.0" encoding="ISO-8859-1"?>
<QLogic>
<AppName>SANsurfer FC CLI</AppName>
<AppVersion>v1.06.14 Build 25</AppVersion>
<HBA>
<GeneralInfo Number="0"
Model="QLA2312"
WWNN="##-##-##-##-##-##-##-##"
WWPN="##-##-##-##-##-##-##-##"

PortID="61-4E-13"
SerialNumber="N40090"
DriverVersion="9.1.0.13 (w32 IP)"
BIOSVersion="1.45"
FirmwareVersion="3.03.18"
TargetCount="1"
PCIBus="1"
PCIDevice="3"
ActualConnectionMode="Point To Point"
ActualDataRate="2 Gbps"
PortType="NPort"
Status="Online" />


</HBA>
<HBA>
<GeneralInfo Number="1"
Model="QLA2312"
WWNN="##-##-##-##-##-##-##-##"
WWPN="##-##-##-##-##-##-##-##"

PortID="62-4E-13"
SerialNumber="N40346"
DriverVersion="9.1.0.13 (w32 IP)"
BIOSVersion="1.45"
FirmwareVersion="3.03.18"
TargetCount="1"
PCIBus="1"
PCIDevice="35"
ActualConnectionMode="Point To Point"
ActualDataRate="2 Gbps"
PortType="NPort"
Status="Online" />
</HBA>
<Status> 0 </Status>
<Reboot> 0 </Reboot>
</QLogic>


PaulChavez

unread,
Jun 16, 2010, 6:18:42 PM6/16/10
to
Getting closer! Does this work?

$xml = [xml]($psinfoOutput | where{$_})

That should strip out the leading blank line and allow the XML to load.

Paul

Message has been deleted

OldDog

unread,
Jun 17, 2010, 10:45:33 AM6/17/10
to
On Jun 16, 5:18 pm, PaulChavez <PaulCha...@discussions.microsoft.com>
wrote:
> >     + FullyQualifiedErrorId : RuntimeException- Hide quoted text -

>
> - Show quoted text -

And we have a Winner! What do we have for our lucky constestant
Johnny?

How about a working Function?


Here you go!

#<---- Begin Function

Function SanCLI($strComputer) {
If (Test-Path "\\$strComputer\C$\Program Files\SANsurferCLI
\scli.exe")
{
$xRow = $rRow


$psinfoOutput = c:\psTools\psexec.exe \\$strComputer -w "C:\Program
Files\SANsurferCLI" "C:\Program Files\SANsurferCLI\scli.exe" -I ALL -
X

$xml = [xml]($psinfoOutput | where{$_})

$a = $xml.QLogic.HBA[0].GeneralInfo
$b = $xml.QLogic.HBA[1].GeneralInfo
$ws.Cells.Item($rRow,16) = $a.WWNN
$ws.Cells.Item($rRow,17) = $a.WWPN
$rRow++
$ws.Cells.Item($rRow,16) = $b.WWNN
$ws.Cells.Item($rRow,17) = $b.WWPN
}
$rRow = $xRow

}

#<---- End Function

PaulChavez

unread,
Jun 17, 2010, 3:00:32 PM6/17/10
to
I love it when a plan comes together!

Paul


"OldDog" wrote:

> And we have a Winner! What do we have for our lucky constestant
> Johnny?
>
> How about a working Function?
>
>
> Here you go!
>
> #<---- Begin Function
>
> Function SanCLI($strComputer) {
> If (Test-Path "\\$strComputer\C$\Program Files\SANsurferCLI
> \scli.exe")
> {
> $xRow = $rRow
> $psinfoOutput = c:\psTools\psexec.exe \\$strComputer -w "C:\Program
> Files\SANsurferCLI" "C:\Program Files\SANsurferCLI\scli.exe" -I ALL -
> X
> $xml = [xml]($psinfoOutput | where{$_})
> $a = $xml.QLogic.HBA[0].GeneralInfo
> $b = $xml.QLogic.HBA[1].GeneralInfo
> $ws.Cells.Item($rRow,16) = $a.WWNN
> $ws.Cells.Item($rRow,17) = $a.WWPN
> $rRow++
> $ws.Cells.Item($rRow,16) = $b.WWNN
> $ws.Cells.Item($rRow,17) = $b.WWPN
> }
> $rRow = $xRow
>
> }
>
> #<---- End Function
>

> .
>

OldDog

unread,
Jun 17, 2010, 10:15:49 PM6/17/10
to
On Jun 17, 2:00 pm, PaulChavez <PaulCha...@discussions.microsoft.com>
wrote:
> > .- Hide quoted text -

>
> - Show quoted text -

Thanks for all your help. BTW: can you explain this bit?

where{$_})

I am not sure what or how that works and I don't belive I have seen it
before.

OldDog

PaulChavez

unread,
Jun 18, 2010, 11:47:04 AM6/18/10
to
The Where-Object cmdlet will filter out empty strings, which seemed to be the
problem preventing you from loading the XML string into an XMLDoc object.

It *won't* filter out whitespace, so if there had been even a single space
or tab on that first line it wouldn't have worked.

#This works
"","foo" | where {$_}

#this doesn't
" ","foo" | where {$_}

Paul

"OldDog" wrote:

> .
>

OldDog

unread,
Jun 19, 2010, 3:47:01 PM6/19/10
to
On Jun 18, 10:47 am, PaulChavez <PaulCha...@discussions.microsoft.com>
wrote:

> The Where-Object cmdlet will filter out empty strings, which seemed to be the
> problem preventing you from loading the XML string into an XMLDoc object.
>
> It *won't* filter out whitespace, so if there had been even a single space
> or tab on that first line it wouldn't have worked.
>
> #This works
> "","foo" | where {$_}
>
> #this doesn't
> " ","foo" | where {$_}
>
> Paul
>
>
>
> "OldDog" wrote:
> > On Jun 17, 2:00 pm, PaulChavez <PaulCha...@discussions.microsoft.com>
> > wrote:
> > > I love it when a plan comes together!
>
> > > Paul
>
> > > "OldDog" wrote:
> > > > And we have a Winner! What do we have for our lucky contestant
> > I am not sure what or how that works and I don't believe I have seen it
> > before.
>
> > OldDog

> > .- Hide quoted text -
>
> - Show quoted text -

Cool, thanks for that. I have learned another new trick. Not bad for
an OldDog!

0 new messages