[英]PowerShell XML Select Nodes Behaviour
我有一個 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
嘗試以下操作(注意.
):
$XMLInput.SelectNodes($XPath).
SelectNodes(".//*[@DataSource='Action']")
請注意, .SelectNodes()
可以訪問整個文檔,而不.SelectNodes()
在哪個節點上被調用。
因此, //*
搜索整個文檔中的所有元素,這違背了$XPath
查詢的目的。
要從調用.SelectNodes()
的節點開始搜索,請使用開始查詢.
,如上圖。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.