0

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:

PowerShell Error v01

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.

0

1 Answer 1

1

Apparently, this script works as I want it to:

        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]")
            }
        }

Modified from this script:

$node = $conns.SelectSingleNode("//Description[.='HP Command View EVA']")
while ($node -ne $null) {
    $node.ParentNode.RemoveChild($node)
    $node = $conns.SelectSingleNode("//Description[.='HP Command View EVA']")
}

found from http://jon.netdork.net/2013/01/09/removing-xml-elements-using-powershell/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.