Help with Select-Xml

I’m having problems getting Select-Xml to work. The xml file is converted from a .INI file. I want to extract the information for a repeated Section /Objs/Obj/MS/Obj where the Attribute is “PortForwarding”. Under /Objs/Obj/MS/Obj, there is /Objs/Obj/MS/Obj/MS/S where there are multiple rows for <S> … </S> each with an array of values I want to gather into a data table.

[xml file]

So… In Powershell, I can get a datatable to use …

 

Datatable:-

Column1 Column2 Column3 Column4 Column5 Column6 Column7 Column8
“0000.ACQ-DEV-ABC1”>Local, fred.smith@10.100.1.222:22, 10.111.1.212:1433;11111, -1, C:\Keys\PrivateKey.ppk, 0.0.0.0, No proxy selected, 0
“0001.ACQ-QA-ABC1”>Local, fred.smith@10.100.1.222:22, 10.111.1.212:1433;11112, -1, C:\Keys\PrivateKey.ppk, 0.0.0.0, No proxy selected, 0

Thanks in advance!

 

 

 

 

The part that you are probably missing would be the XML namespace try the code fragment below:

$ns = @{"NS"="http://schemas.microsoft.com/powershell/2004/04"}
$o = Select-Xml  -Path .\forumq.xml -Namespace $ns -XPath "//NS:Obj[@N='PortForwarding']/NS:MS/NS:S"
$o.node.'#text'

That should give you the results below

Local;fred.smith@10.100.1.111:22;10.111.4.212:1433;11111;-1;C:\Keys\PrivateKey.ppk;0.0.0.0;No proxy selected;0
Local;fred.smith@10.100.1.111:22;10.111.4.12:1433;11112;-1;C:\Keys\PrivateKey.ppk;0.0.0.0;No proxy selected;0

You could also make life easier on yourself by just using import-clixml cmdlet to get the same result.

$o= Import-Clixml .\forumq.xml
$o.PortForwarding.psobject.Properties.value