PowerShell: remove xml node if its children meet certain criteria

I have an xml file that is generated by FileZilla and contains connection information for multiple ftp servers.

XML:

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<FileZilla3>
    <Servers>
        <Server>
            <Host>host</Host>
            <Port>1</Port>
            <Protocol>3</Protocol>
            <Type>0</Type>
            <User>SomeUser</User>
            <Pass>p455w0rd</Pass>
            <Logontype>1</Logontype>
            <TimezoneOffset>0</TimezoneOffset>
            <PasvMode>MODE_DEFAULT</PasvMode>
            <MaximumMultipleConnections>0</MaximumMultipleConnections>
            <EncodingType>Auto</EncodingType>
            <BypassProxy>0</BypassProxy>
            <Name>Server1</Name>
            <Comments>Comment</Comments>
            <LocalDir />
            <RemoteDir />
            <SyncBrowsing>0</SyncBrowsing>Server1
        </Server>
        <Server>
            <Host>host</Host>
            <Port>1</Port>
            <Protocol>3</Protocol>
            <Type>0</Type>
            <User>SomeOtherUser</User>
            <Pass>passw0rd</Pass>
            <Logontype>1</Logontype>
            <TimezoneOffset>0</TimezoneOffset>
            <PasvMode>MODE_DEFAULT</PasvMode>
            <MaximumMultipleConnections>0</MaximumMultipleConnections>
            <EncodingType>Auto</EncodingType>
            <BypassProxy>0</BypassProxy>
            <Name>Server2</Name>
            <Comments />
            <LocalDir />
            <RemoteDir />
            <SyncBrowsing>0</SyncBrowsing>Server2
        </Server>
    </Servers>
</FileZilla3>

      

Now I am working on a script to select ftp accounts which are then removed from this xml file. Here's what I have so far:

$SiteManager = "C:\Temp\SiteManager.xml"

[XML]$SiteManagerXMLContent = Get-Content $SiteManager -Encoding UTF8

#Account that gets removed
$FTPAccName     = "Server1"
$FTPAccUserName = "SomeUser"
$FTPAccPassWord = "p455w0rd"
$FTPAccComment  = "Comment"

ForEach($Server in $SiteManagerXMLContent.FileZilla3.Servers)
{
    $XMLServerName = $Server.SelectSingleNode("//Name[.='$FTPAccName']")
    $XMLUserName = $Server.SelectSingleNode("//User[.='$FTPAccUserName']")
    $XMLPassWord = $Server.SelectSingleNode("//Pass[.='$FTPAccPassWord']")
    $XMLComment = $Server.SelectSingleNode("//Comment[.='$FTPAccComment']")

    if($XMLServerName.'#text' -eq $FTPAccName -and $XMLUserName.'#text' -eq $FTPAccUserName -and $XMLPassWord.'#text' -eq $FTPAccPassWord -and $XMLComment.'#text' -eq $FTPAccComment)
    {
        $XMLPassWord.ParentNode.RemoveAll()
    }
}

$SiteManagerXMLContent.Save($sitemanager)

      

This removes all the child nodes of the selected server, but not the parent node: which is what I'm aiming for. I would like to remove all node.

After running this script, my xml looks like this:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<FileZilla3>
  <Servers>
    <Server>
    </Server>
    <Server>
      <Host>host</Host>
      <Port>1</Port>
      <Protocol>3</Protocol>
      <Type>0</Type>
      <User>SomeOtherUser</User>
      <Pass>passw0rd</Pass>
      <Logontype>1</Logontype>
      <TimezoneOffset>0</TimezoneOffset>
      <PasvMode>MODE_DEFAULT</PasvMode>
      <MaximumMultipleConnections>0</MaximumMultipleConnections>
      <EncodingType>Auto</EncodingType>
      <BypassProxy>0</BypassProxy>
      <Name>Server2</Name>
      <Comments />
      <LocalDir />
      <RemoteDir />
      <SyncBrowsing>0</SyncBrowsing>Server2
        </Server>
  </Servers>
</FileZilla3>

      

The problem I'm running into is that every server has the same tag, so I need to define the correct server by its child nodes.

Thanks for any help.

+3


source to share


1 answer


You can try by removing the parent node from the grand parent, for example:



$XMLPassWord.ParentNode.ParentNode.RemoveChild($XMLPassWord.ParentNode)

      

+2


source







All Articles