Reputation: 18197
I am having a xml file similar to below.
<Parameters>
<Parameter IsEncrypted="False">
<ParameterName>pRdGpInstallOptions</ParameterName>
<ParameterValue>Custom</ParameterValue>
</Parameter>
<Parameter IsEncrypted="False">
<ParameterName>pRdAccept</ParameterName>
<ParameterValue>true</ParameterValue>
</Parameter>
<Parameter IsEncrypted="False">
<ParameterName>pCboEditSQLServer</ParameterName>
<ParameterValue>MachineName\SQLEXPRESS</ParameterValue>
</Parameter>
</Parameters>
I have copied only few nodes here. Actually the real file may be having hundreds of Parameter nodes.
We can't hard code the index as it may appear in any order. I am trying to retrieve SQLinstance name parameter value which is associated with Parameter Name "pCboEditSQLServer"
.
I would like to retrieve this value using powershell. How to parse this dynamic xml files?
Upvotes: 2
Views: 4239
Reputation: 126872
$sql = $xml.Parameters.Parameter | Where-Object {$_.ParameterName -eq 'pCboEditSQLServer'}
$sql.ParameterValue
MachineName\SQLEXPRESS
Here's another option (should be faster) using xpath:
$path = "/Parameters/Parameter[ParameterName='pCboEditSQLServer']/ParameterValue"
$xml.SelectSingleNode($path).'#text'
And there's the Select-Xml
cmdlet:
$sql = $xml | Select-Xml -XPath $path
$sql.Node.'#text'
Upvotes: 9
Reputation: 8679
Read XML:
$xml = [xml](gc test.xml -Encoding utf8)
And use object model to retrieve data:
$xml.Parameters.Parameter | ? { $_.ParameterName -eq "pCboEditSQLServer" } | Select-Object -Property ParameterName, ParameterValue
Upvotes: 3