PowerShell XML Поведение отдельных узлов - PullRequest
1 голос
/ 19 марта 2020

У меня есть XML, из которого я хочу получить все узлы с DataSource = 'Action', где Task Name = 'AddUsersToDistributionList'.

Я попробовал приведенный ниже код, но что интересно, он дает мне все узлы, где DataSource = 'Action' для всех задач, даже если имя задачи упоминается конкретно в 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']")

Вывод

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

Ожидаемый вывод

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

1 Ответ

0 голосов
/ 19 марта 2020

Попробуйте следующее (обратите внимание на .):

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

Обратите внимание, что .SelectNodes() имеет доступ ко всему документу , независимо от того, на каком узле он вызывается.

Таким образом, //* ищет все элементы во всем документе , что противоречит цели вашего $XPath запроса.

Чтобы начать поиск с для узла, который вы вызываете .SelectNodes() на , начните свой запрос с ., как показано выше.

...