I'm trying to remove <Element>SlowPollGroup</Element> from the XML below. <Markers> is four levels deep in the XML file. I've already found <Markers> as $markersExists using $markersExists = $vendor.SelectSingleNode("./Markers") script.
<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>
I've tried the script below:
$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]")
}
}
Log file excerpt looks like below:
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]
However, when I tested the whole script in Windows PowerShell ISE on Win10, the output looks like below:
I've used the code below:
$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")
}
}
on the XML below
<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>
to remove the last Element, i.e.
<Element>
<Name>GVL_TC</Name>
<PollInterval>100</PollInterval>
</Element>
and it worked. I understand both XML structures are different and I suspect $element in the top script contains only 'SlowPollGroup', not <Element>SlowPollGroup</Element>. I've been at this over the weekend trying out various scripts offered from Google searches, but no dice. Any help is greatly appreciated.