xmlpowershellselectnodes

PowerShell XML Select Nodes Behaviour


I have a XML from which I want to fetch all the Nodes with DataSource='Action' where Task Name='AddUsersToDistributionList'.

I tried the below code, but interestingly it is giving me all the Nodes where DataSource='Action' for all the tasks, even if the Task Name is mentioned specifically in the xpath.

[xml] $XMLInput =@'
<Process Name="AddUsersToDistributionList" Description="">
    <Tasks>
        <Task Enabled="True" Name="AddUsersToDistributionList" Description="Add users to Distribution List" Application="MS Exchange 2013+" OS="MS WIN Server 2008+" Enabled1="False">
            <TaskInputs>
                <Parameter Name="DistributionListName1" Mandatory="True" Datatype="Dictionary" DataSource="Action" Action="Get-DictionaryValue"/>
                <Parameter Name="DistributionListName2" Mandatory="True" Datatype="Array" DataSource="Action" Action="Get-ArrayValue"/>
                <Parameter Name="DistributionListName3" Mandatory="True" Datatype="String" DataSource="Action" Action="Get-StringValue"/>
                <Parameter Name="DistributionListName4" Mandatory="True" Datatype="String" DataSource="Fixed"/>
            </TaskInputs>
            <TaskFeatures>
                <Feature Name="Test" Enabled="true" Description="This allows the users outside organization to send Email to this Distribution List">
                    <Parameter Name="DistributionListName5" Mandatory="True" Datatype="String" DataSource="Action" Action="Get-StringValue"/>
                    <Parameter1 DataSource="Action" Action="Get-StringValue"/>
                </Feature>
            </TaskFeatures>
        </Task>
        <CustomTask Enabled="True" Name="RemoveUsersToDistributionList" Description="Add users to Distribution List" Application="MS Exchange 2013+" OS="MS WIN Server 2008+" Enabled1="False">
            <TaskInputs>
                <Parameter Name="DistributionListName401" Mandatory="True" Datatype="Dictionary" DataSource="Action" Action="Get-DictionaryValue"/>
                <Parameter Name="DistributionListName402" Mandatory="True" Datatype="Array" DataSource="Action" Action="Get-ArrayValue"/>
                <Parameter Name="DistributionListName403" Mandatory="True" Datatype="String" DataSource="Action" Action="Get-StringValue"/>
            </TaskInputs>
        </CustomTask>
    </Tasks>
</Process>
'@
$XPath = "//Task[@Name='AddUsersToDistributionList'] | //CustomTask[@Name='AddUsersToDistributionList']"

$XMLInput.selectnodes($XPath).selectnodes("//*[@DataSource='Action']")

Output

PS C:\Users\inspadhi\Desktop\Scripts> powershell.exe .\Test_Xpath.ps1                                                                                                                           

Name       : DistributionListName1
Mandatory  : True
Datatype   : Dictionary
DataSource : Action
Action     : Get-DictionaryValue

Name       : DistributionListName2
Mandatory  : True
Datatype   : Array
DataSource : Action
Action     : Get-ArrayValue

Name       : DistributionListName3
Mandatory  : True
Datatype   : String
DataSource : Action
Action     : Get-StringValue

Name       : DistributionListName401
Mandatory  : True
Datatype   : Dictionary
DataSource : Action
Action     : Get-DictionaryValue

Name       : DistributionListName402
Mandatory  : True
Datatype   : Array
DataSource : Action
Action     : Get-ArrayValue

Name       : DistributionListName403
Mandatory  : True
Datatype   : String
DataSource : Action
Action     : Get-StringValue

Expected Output

Name       : DistributionListName1
Mandatory  : True
Datatype   : Dictionary
DataSource : Action
Action     : Get-DictionaryValue

Name       : DistributionListName2
Mandatory  : True
Datatype   : Array
DataSource : Action
Action     : Get-ArrayValue

Name       : DistributionListName3
Mandatory  : True
Datatype   : String
DataSource : Action
Action     : Get-StringValue

Solution

  • Try the following (note the .):

    $XMLInput.SelectNodes($XPath).
      SelectNodes(".//*[@DataSource='Action']")
    

    Note that .SelectNodes() has access to the entire document, irrespective of what node it is invoked on.

    Therefore, //* searches all elements in the whole document, which defeats the purpose of your $XPath query.

    To start your search from the node you're invoking .SelectNodes() on, start your query with ., as shown above.