使用 PowerShell 删除 XML 节点
Remove an XML node using PowerShell
我正在尝试从下面的 XML 中删除 <Element>SlowPollGroup</Element>
。 <Markers>
在 XML 文件中有四层。我已经使用 $markersExists = $vendor.SelectSingleNode("./Markers")
脚本找到 <Markers>
作为 $markersExists。
<Markers>
<Element>Group2</Element>
<Element>Group3</Element>
<Element>Group4</Element>
<Element>HWSimSubscription</Element>
<Element>DIO_TC</Element>
<Element>CmdSubGroup</Element>
<Element>FastPollGroup</Element>
<Element>SlowPollGroup</Element>
<Element>HWSimPoll</Element>
<Element>WriteOnly</Element>
</Markers>
我试过下面的脚本:
$groupNameSPG = "SlowPollGroup"
$delMarkerElem = $groupNameSPG
LogWrite ("Searching for Markers")
$markersExists = $vendor.SelectSingleNode("./Markers")
if ($null -ne $markersExists) {
LogWrite ("Markers found")
foreach($element in $markersExists.Element) {
if ( $element -eq $delMarkerElem) {
LogWrite ($delMarkerElem + " found")
$delElem = $element.ParentNode
$delMarkerElemFound = $true
break
}
LogWrite ("Searching for [$delMarkerElem] again")
}
if ($delMarkerElemFound) {
$markersExists.RemoveChild($element) | Out-Null
LogWrite ("Removed => [$delMarkerElem]")
}
else {
LogWrite ("delMarkerElemFound => [$delMarkerElemFound]")
}
}
日志文件摘录如下所示:
Searching for Markers
Markers found
Searching for [SlowPollGroup] again
Searching for [SlowPollGroup] again
Searching for [SlowPollGroup] again
Searching for [SlowPollGroup] again
Searching for [SlowPollGroup] again
Searching for [SlowPollGroup] again
Searching for [SlowPollGroup] again
SlowPollGroup found
Removed => [SlowPollGroup]
然而,当我在 Win10 上的 Windows PowerShell ISE 中测试整个脚本时,输出如下所示:
PowerShell Error v01
我使用了下面的代码:
$groupsExists = $vendor.SelectSingleNode("./Groups")
if ($null -ne $groupsExists) {
LogWrite ("Groups found")
foreach($element in $groupsExists.Element) {
if ( $element.Name -eq $groupElemName) {
LogWrite ($groupElemName + " found")
$groupElemNameFound = $true
break
}
LogWrite ("Searching for " + $groupElemName + " again")
}
if ($groupElemNameFound) {
$groupsExists.RemoveChild($element) | Out-Null
LogWrite ("Removed => $groupElemName")
}
}
下面XML
<Groups>
<Element>
<Name>Group1</Name>
<PollInterval>200</PollInterval>
<UseDataSubscription>True</UseDataSubscription>
</Element>
<Element>
<Name>AlarmGroup</Name>
<PollInterval>1000</PollInterval>
</Element>
<Element>
<Name>GVL_TC</Name>
<PollInterval>100</PollInterval>
</Element>
</Groups>
删除最后一个元素,即
<Element>
<Name>GVL_TC</Name>
<PollInterval>100</PollInterval>
</Element>
它奏效了。我知道 XML 结构不同,我怀疑顶部脚本中的 $element 只包含 'SlowPollGroup',而不包含 <Element>SlowPollGroup</Element>
。周末我一直在尝试 Google 搜索提供的各种脚本,但没有骰子。非常感谢任何帮助。
显然,这个脚本可以正常工作:
LogWrite ("Searching for Markers")
$markersExists = $vendor.SelectSingleNode("./Markers")
if ($null -ne $markersExists) {
LogWrite ("Markers found")
# $delMarkerElem needs to be within ''
$node = $markersExists.SelectSingleNode("./Element[.='$delMarkerElem']")
if ($null -ne $node) {
$node.ParentNode.RemoveChild($node)
LogWrite ("Removed => Element node=[$node] with delMarkerElem=[$delMarkerElem]")
}
else {
LogWrite ("Not found - Element node=[$node] with delMarkerElem=[$delMarkerElem]")
}
}
根据此脚本修改:
$node = $conns.SelectSingleNode("//Description[.='HP Command View EVA']")
while ($node -ne $null) {
$node.ParentNode.RemoveChild($node)
$node = $conns.SelectSingleNode("//Description[.='HP Command View EVA']")
}
来自 http://jon.netdork.net/2013/01/09/removing-xml-elements-using-powershell/
我正在尝试从下面的 XML 中删除 <Element>SlowPollGroup</Element>
。 <Markers>
在 XML 文件中有四层。我已经使用 $markersExists = $vendor.SelectSingleNode("./Markers")
脚本找到 <Markers>
作为 $markersExists。
<Markers>
<Element>Group2</Element>
<Element>Group3</Element>
<Element>Group4</Element>
<Element>HWSimSubscription</Element>
<Element>DIO_TC</Element>
<Element>CmdSubGroup</Element>
<Element>FastPollGroup</Element>
<Element>SlowPollGroup</Element>
<Element>HWSimPoll</Element>
<Element>WriteOnly</Element>
</Markers>
我试过下面的脚本:
$groupNameSPG = "SlowPollGroup"
$delMarkerElem = $groupNameSPG
LogWrite ("Searching for Markers")
$markersExists = $vendor.SelectSingleNode("./Markers")
if ($null -ne $markersExists) {
LogWrite ("Markers found")
foreach($element in $markersExists.Element) {
if ( $element -eq $delMarkerElem) {
LogWrite ($delMarkerElem + " found")
$delElem = $element.ParentNode
$delMarkerElemFound = $true
break
}
LogWrite ("Searching for [$delMarkerElem] again")
}
if ($delMarkerElemFound) {
$markersExists.RemoveChild($element) | Out-Null
LogWrite ("Removed => [$delMarkerElem]")
}
else {
LogWrite ("delMarkerElemFound => [$delMarkerElemFound]")
}
}
日志文件摘录如下所示:
Searching for Markers
Markers found
Searching for [SlowPollGroup] again
Searching for [SlowPollGroup] again
Searching for [SlowPollGroup] again
Searching for [SlowPollGroup] again
Searching for [SlowPollGroup] again
Searching for [SlowPollGroup] again
Searching for [SlowPollGroup] again
SlowPollGroup found
Removed => [SlowPollGroup]
然而,当我在 Win10 上的 Windows PowerShell ISE 中测试整个脚本时,输出如下所示:
PowerShell Error v01
我使用了下面的代码:
$groupsExists = $vendor.SelectSingleNode("./Groups")
if ($null -ne $groupsExists) {
LogWrite ("Groups found")
foreach($element in $groupsExists.Element) {
if ( $element.Name -eq $groupElemName) {
LogWrite ($groupElemName + " found")
$groupElemNameFound = $true
break
}
LogWrite ("Searching for " + $groupElemName + " again")
}
if ($groupElemNameFound) {
$groupsExists.RemoveChild($element) | Out-Null
LogWrite ("Removed => $groupElemName")
}
}
下面XML
<Groups>
<Element>
<Name>Group1</Name>
<PollInterval>200</PollInterval>
<UseDataSubscription>True</UseDataSubscription>
</Element>
<Element>
<Name>AlarmGroup</Name>
<PollInterval>1000</PollInterval>
</Element>
<Element>
<Name>GVL_TC</Name>
<PollInterval>100</PollInterval>
</Element>
</Groups>
删除最后一个元素,即
<Element>
<Name>GVL_TC</Name>
<PollInterval>100</PollInterval>
</Element>
它奏效了。我知道 XML 结构不同,我怀疑顶部脚本中的 $element 只包含 'SlowPollGroup',而不包含 <Element>SlowPollGroup</Element>
。周末我一直在尝试 Google 搜索提供的各种脚本,但没有骰子。非常感谢任何帮助。
显然,这个脚本可以正常工作:
LogWrite ("Searching for Markers")
$markersExists = $vendor.SelectSingleNode("./Markers")
if ($null -ne $markersExists) {
LogWrite ("Markers found")
# $delMarkerElem needs to be within ''
$node = $markersExists.SelectSingleNode("./Element[.='$delMarkerElem']")
if ($null -ne $node) {
$node.ParentNode.RemoveChild($node)
LogWrite ("Removed => Element node=[$node] with delMarkerElem=[$delMarkerElem]")
}
else {
LogWrite ("Not found - Element node=[$node] with delMarkerElem=[$delMarkerElem]")
}
}
根据此脚本修改:
$node = $conns.SelectSingleNode("//Description[.='HP Command View EVA']")
while ($node -ne $null) {
$node.ParentNode.RemoveChild($node)
$node = $conns.SelectSingleNode("//Description[.='HP Command View EVA']")
}
来自 http://jon.netdork.net/2013/01/09/removing-xml-elements-using-powershell/