PowerShell: Remove an xml node if its childnodes matches specific criteria

mariu5

I have an xml file which is created by FileZilla and contains connection details 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'm working on a script to select ftp accounts which then get 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 child nodes of the selected server but not the parent node: which is what I'm aiming for. I would like to delete the entire node.

After running this script my xml looks like so:

<?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 facing is that every server has the same tag which is why I need to identify the correct server by its child nodes.

Thanks for any help.

har07

You can try by removing parent node from grand parent like so :

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

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Set x XML node to value in Powershell

분류에서Dev

XML grab attribute from specific element node

분류에서Dev

How to input strings into array until input string matches criteria?

분류에서Dev

Attempt to get next and previous sibling of a node after parsing xml for a specific node using java

분류에서Dev

Django restrict foreignkey to records meeting a specific criteria

분류에서Dev

sed/awk remove newline on two pattern matches

분류에서Dev

PowerShell Remove the last error

분류에서Dev

remove attribute on document node

분류에서Dev

Powershell XML 필터

분류에서Dev

Select a specific range from table using hibernate criteria

분류에서Dev

iterating the xml for specific values

분류에서Dev

Powershell regex Matches[0].Groups vs Matches.Groups key name based indexing

분류에서Dev

Edit xml property in node

분류에서Dev

Delphi find xml node

분류에서Dev

How to remove text in an element but show its children

분류에서Dev

Remove a Specific Part of The String In The Cell

분류에서Dev

how to remove specific rows in a matrix

분류에서Dev

Remove fields containing specific string

분류에서Dev

Return a Builder whose every relation's model matches a specific condition

분류에서Dev

How to give a specific user access only to a specific folder and its contents?

분류에서Dev

retrieve xml values using powershell

분류에서Dev

How to iterate through XML in Powershell?

분류에서Dev

중첩 된 XML Powershell

분류에서Dev

Powershell 및 XML 작업

분류에서Dev

Powershell XML 인코딩

분류에서Dev

xml select next node and change value of that node

분류에서Dev

delete a node of an xml file with php

분류에서Dev

Retrieve a node value from xml

분류에서Dev

Retrieve a node value from xml

Related 관련 기사

뜨겁다태그

보관