Scott Duncan
Scott Duncan

Reputation: 1151

Powershell SelectSingleNode on a child node

I'm using Powershell to install an SSIS Project and create a Environment for it. I am reading the project parameters from the Project.Params XML file to create the environment variables & their values. I am able to use the following code snippet to read the parameters & their values, but I'd like to use SelectSingleNode instead of the second foreach loop. My XPath skills are not up to it. Here's the working code:

# load the Params file into memory
[xml]$ParamsDoc = Get-Content -Path $ParamsFilePath

# for each parameter in the Params file, add an environment variable if it doesn't already exist
foreach ($Param in $ParamsDoc.Parameters.Parameter)
{
    $ParamName = $Param.Name

    # use the value in the Params file as the default value for the environment variable
    foreach ($Prop in $Param.Properties.Property)
    {
        if ($Prop.Name -eq 'value')
        {
            $ParamValue = $Prop.InnerText
            break
        }
    }
    AddEnvironmentVariableIfMissing $ParamName $ParamValue
}

AddEnvironmentVariableIfMissing is a separate function I've written. Now, here's the code that doesn't work:

# load the Params file into memory
[xml]$ParamsDoc = Get-Content $ParamsFilePath

$nsMgr = New-Object System.Xml.XmlNamespaceManager($ParamsDoc.NameTable)
$nsMgr.AddNamespace("SSIS", $ParamsDoc.DocumentElement.NamespaceURI)

foreach ($Param in $ParamsDoc.Parameters.Parameter)
{
    $ParamName = $Param.Name

    # NONE of these work!
    $ParamValue = $Param.Properties.SelectSingleNode("SSIS:Property[@Name='Value']",$nsMgr)
    $ParamValue = $Param.Properties.SelectSingleNode("Name[.='Value']",$nsMgr)
    $ParamValue = $Param.Properties.SelectSingleNode("SSIS:Property/Name[.='Value']",$nsMgr)
    $ParamValue = $Param.Properties.Property.SelectSingleNode("Name[.='Value']",$nsMgr)
    $ParamValue = $Param.SelectSingleNode("SSIS:Properties/SSIS:Property[@Name='Value']",$nsMgr)
    $ParamValue = $Param.SelectSingleNode("SSIS:Properties/SSIS:Property/Name[.='Value']",$nsMgr)
    $ParamValue = $Param.SelectSingleNode("SSIS:Property[@Name='Value']",$nsMgr)
    $ParamValue = $Param.SelectSingleNode("SSIS:Property/Name[.='Value']",$nsMgr)

    AddEnvironmentVariableIfMissing $ParamName $ParamValue
}

What is the correct XPath syntax to get the child node with Name = 'Value'?

Here's a sample of an SSIS Project.params file

<?xml version="1.0"?>
<SSIS:Parameters xmlns:SSIS="www.microsoft.com/SqlServer/SSIS">
  <SSIS:Parameter
    SSIS:Name="Client">
    <SSIS:Properties>
      <SSIS:Property
        SSIS:Name="ID">{144b0266-9e94-4821-bb68-f020436d5df5}</SSIS:Property>
      <SSIS:Property
        SSIS:Name="CreationName"></SSIS:Property>
      <SSIS:Property
        SSIS:Name="Description"></SSIS:Property>
      <SSIS:Property
        SSIS:Name="IncludeInDebugDump">0</SSIS:Property>
      <SSIS:Property
        SSIS:Name="Required">0</SSIS:Property>
      <SSIS:Property
        SSIS:Name="Sensitive">0</SSIS:Property>
      <SSIS:Property
        SSIS:Name="Value">sampleclient</SSIS:Property>
      <SSIS:Property
        SSIS:Name="DataType">18</SSIS:Property>
    </SSIS:Properties>
  </SSIS:Parameter>
  <SSIS:Parameter
    SSIS:Name="MSSQLServer">
    <SSIS:Properties>
      <SSIS:Property
        SSIS:Name="ID">{b4e6c5e1-ef85-4df3-b2dc-db37971d081d}</SSIS:Property>
      <SSIS:Property
        SSIS:Name="CreationName"></SSIS:Property>
      <SSIS:Property
        SSIS:Name="Description"></SSIS:Property>
      <SSIS:Property
        SSIS:Name="IncludeInDebugDump">0</SSIS:Property>
      <SSIS:Property
        SSIS:Name="Required">0</SSIS:Property>
      <SSIS:Property
        SSIS:Name="Sensitive">0</SSIS:Property>
      <SSIS:Property
        SSIS:Name="Value">MSSQLSERVER</SSIS:Property>
      <SSIS:Property
        SSIS:Name="DataType">18</SSIS:Property>
    </SSIS:Properties>
  </SSIS:Parameter>
</SSIS:Parameters>

Upvotes: 1

Views: 3873

Answers (1)

Scott Duncan
Scott Duncan

Reputation: 1151

So PetSerAI had the right idea. The namespace needed to be in the XPath syntax, including the Name element. The two syntax that work are:

    $ParamValue = $Param.Properties.SelectSingleNode("SSIS:Property[@SSIS:Name='Value']",$nsMgr)
    $ParamValue = $Param.SelectSingleNode("SSIS:Properties/SSIS:Property[@SSIS:Name='Value']",$nsMgr)

Thank you, PetSerAI !

Upvotes: 2

Related Questions