Reading settings.xml file using PowerShell

Need help with FileZilla Server? Something does not work as expected? In this forum you may find an answer.

Moderator: Project members

Post Reply
Message
Author
User avatar
ozdeadmeat
504 Command not implemented
Posts: 7
Joined: 2022-07-21 06:57
First name: Josh

Reading settings.xml file using PowerShell

#1 Post by ozdeadmeat » 2022-07-21 07:12

Hi,

First time posting. I am hoping to get some help. I was running Filezilla Server 1.3.0 and recently tried upgrading to a more recent version of 1.4.1. I used 1.3.0 to create an automation using PowerShell to open and close firewall ports based on what the configuration is in the settings.xml. For 1.3.0 this works perfectly, however, in 1.4.1 powershell is reporting that the xml is invalid. I think it stems from the <filezilla> tag at the start of the XML file. The newer versions seem to have some comments in the tag itself and that is making the xml file unreadable.

Full disclosure, I am not experienced in XML as most software I use has a json or ini configuration file. Here is the way I am getting the configuraton information out of Filezilla 1.3.0 settings.xml.

Code: Select all

    $FilezillaCFG = [xml](Get-Content "C:\Windows\System32\config\systemprofile\AppData\Local\filezilla-server\settings.xml")
    $FTP_Port    = $FilezillaCFG.filezilla.server.listener.port                             #FileZilla Config FTP Port
    $FTP_PASV_PORT_MIN = $FilezillaCFG.filezilla.server.session.pasv.port_range.min         #FileZilla Config FTP Lower Passive Mode Port
    $FTP_PASV_PORT_MAX = $FilezillaCFG.filezilla.server.session.pasv.port_range.max         #FileZilla Config FTP Upper Passive Mode Port
The above method does not work with newer versions of Filezilla. Has anyone had any success getting the newer Filezilla versions settings.xml file to load into a variable in PowerShell Successfully? I am building a new server with the new version of Filezilla so I can have both versions running at the same time to try and work out how to make this work.

Any help with this would be hugely appriciated, I know it is not specifically a Filezilla Server Support Problem and probably more a Powershell problem. But if anyone out there knows how to make it work I would appriciate it.

- OzDeaDMeaT

User avatar
ozdeadmeat
504 Command not implemented
Posts: 7
Joined: 2022-07-21 06:57
First name: Josh

Re: Reading settings.xml file using PowerShell

#2 Post by ozdeadmeat » 2022-07-21 07:38

OK, I have built the new server. Firstly it seems the settings.xml file has been moved to ProgramData\filezilla-server. (Filezilla 1.5.0) Secondly, the error seems to be around an undeclared prefix of 'fz'

Here is the new code I am attempting to use with version 1.5.0.

Code: Select all

$FilezillaCFG = [xml](Get-Content "C:\ProgramData\filezilla-server\settings.xml")
The error it is returning is:

Code: Select all

InvalidArgument: Cannot convert value "System.Object[]" to type "System.Xml.XmlDocument". Error: "'fz' is an undeclared prefix. Line 2, position 12.
The line in question (line 2) has this in the 1.5.0 settings.xml

Code: Select all

<filezilla fz:product_flavour="standard" fz:product_version="1.5.0">
Previous version of Filezilla (1.3.0) had this as line 2

Code: Select all

<filezilla>
Unfortunately removing the "fz:" stuff from the config file only returns when you change the configuration of the server in any way. So just hacking the xml file isn't a long term solution.

User avatar
ozdeadmeat
504 Command not implemented
Posts: 7
Joined: 2022-07-21 06:57
First name: Josh

Re: Reading settings.xml file using PowerShell

#3 Post by ozdeadmeat » 2022-07-21 07:50

just ran the 1.5.0 settings.xml file through an XML validator. It failed for the same reason.

Code: Select all

An error has been found!

Click on  to jump to the error. In the document, you can point at  with your mouse to see the error message.
Errors in the XML document:
	2:	69	The prefix "fz" for attribute "fz:product_flavour" associated with an element type "filezilla" is not bound.

XML document:
1	<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2	<filezilla fz:product_flavour="standard" fz:product_version="1.5.0">

User avatar
botg
Site Admin
Posts: 35566
Joined: 2004-02-23 20:49
First name: Tim
Last name: Kosse

Re: Reading settings.xml file using PowerShell

#4 Post by botg » 2022-07-21 08:36

Note sure if it persists, but try adding a namespace definition to the root element: xmlns:fz="dummyvalue"

User avatar
oibaf
Contributor
Posts: 404
Joined: 2021-07-16 21:02
First name: Fabio
Last name: Alemagna

Re: Reading settings.xml file using PowerShell

#5 Post by oibaf » 2022-07-21 08:39

The next nightly builds and release will contain a namespace declaration, which will fix this. For now, you might have some luck by fiddling with XmlNamespaceManager and XmlDocument rather than the [xml] type accelerator.

User avatar
ozdeadmeat
504 Command not implemented
Posts: 7
Joined: 2022-07-21 06:57
First name: Josh

Re: Reading settings.xml file using PowerShell

#6 Post by ozdeadmeat » 2022-07-21 09:08

Righto, prepare for the worlds most hacky awful but it works bit of scripting....

So essentially what the problem is that the elements in the line 2 of the Filezilla's settings.xml are being referred to as Unbound Attribute Prefixes. I have not been able to find anything on how to read that using PowerShell. So the best way to get around it is to read the entire contents of the xml file into memory and then replace the line that is causing the issue and replacing it with something that can be read by PowerShell. It's ugly as sin, but it seems to be working. Hopefully the developers of Filezilla won't add more of this crap into the settings.xml or I am boned.

Below is a function I wrote to search and replace an entire line.

Code: Select all

Function replaceLine {
param(
    [Parameter(Mandatory=$true)][string]$File,
    [Parameter(Mandatory=$true)][string]$Match,
    [Parameter(Mandatory=$true)][string]$Replace
)
    if(Test-Path $file) {
        $Text = Get-Content $File
        $outputString = $null
        ForEach ($Line in $Text) {
            if($Line -match $Match) {
                $Line = $Replace
            }
            $outputString = $outputString + $Line
        }
        return $outputString
    } else {
        write-host "ERROR: $File file Not Found!" -ForegroundColor "RED"
    }
}
Here is the code needed to be able to read newer settings.xml files into powershell without it returning an error. Make sure you add the function above prior to running the script below.

Code: Select all


    $FilezillaCFGFile = "C:\ProgramData\filezilla-server\settings.xml"
    [xml]$FilezillaCFG = replaceLine -File $FilezillaCFGFile -Match "<filezilla fz:product_flavour=" -Replace "<filezilla>"
    $FTP_Port    = $FilezillaCFG.ftp_filezilla.server.listener.port                             #FileZilla Config FTP Port
    $FTP_PASV_PORT_MIN = $FilezillaCFG.filezilla.ftp_server.session.pasv.port_range.min         #FileZilla Config FTP Lower Passive Mode Port
    $FTP_PASV_PORT_MAX = $FilezillaCFG.filezilla.ftp_server.session.pasv.port_range.max         #FileZilla Config FTP Upper Passive Mode Port
As I said, hacky AF but it works. Hopefully someone who knows a thing or two about XML can set me straight at some point.
Last edited by ozdeadmeat on 2022-07-21 09:38, edited 1 time in total.

User avatar
oibaf
Contributor
Posts: 404
Joined: 2021-07-16 21:02
First name: Fabio
Last name: Alemagna

Re: Reading settings.xml file using PowerShell

#7 Post by oibaf » 2022-07-21 09:14

Notice that in the latest version there's no "server" element anymore, it has been superseded by "ftp_server".

User avatar
ozdeadmeat
504 Command not implemented
Posts: 7
Joined: 2022-07-21 06:57
First name: Josh

Re: Reading settings.xml file using PowerShell

#8 Post by ozdeadmeat » 2022-07-21 09:34

botg wrote:
2022-07-21 08:36
Note sure if it persists, but try adding a namespace definition to the root element: xmlns:fz="dummyvalue"
How do I do that?

User avatar
ozdeadmeat
504 Command not implemented
Posts: 7
Joined: 2022-07-21 06:57
First name: Josh

Re: Reading settings.xml file using PowerShell

#9 Post by ozdeadmeat » 2022-07-21 09:36

oibaf wrote:
2022-07-21 09:14
Notice that in the latest version there's no "server" element anymore, it has been superseded by "ftp_server".
Thanks mate, I will make the changes now.

User avatar
botg
Site Admin
Posts: 35566
Joined: 2004-02-23 20:49
First name: Tim
Last name: Kosse

Re: Reading settings.xml file using PowerShell

#10 Post by botg » 2022-07-21 09:50

ozdeadmeat wrote:
2022-07-21 09:34
botg wrote:
2022-07-21 08:36
Note sure if it persists, but try adding a namespace definition to the root element: xmlns:fz="dummyvalue"
How do I do that?
notepad

User avatar
ozdeadmeat
504 Command not implemented
Posts: 7
Joined: 2022-07-21 06:57
First name: Josh

Re: Reading settings.xml file using PowerShell

#11 Post by ozdeadmeat » 2022-07-21 10:17

botg wrote:
2022-07-21 09:50
ozdeadmeat wrote:
2022-07-21 09:34
botg wrote:
2022-07-21 08:36
Note sure if it persists, but try adding a namespace definition to the root element: xmlns:fz="dummyvalue"
How do I do that?
notepad
I am assuming I add it into the settings.xml. Do I add that to the <filezilla element?

I added it to the filezilla element, here is what I put

Code: Select all

<filezilla xmlns:fz="TEST" fz:product_flavour="standard" fz:product_version="1.5.0">
It reverted back to not having that information in the file when I changed the config.

User avatar
oibaf
Contributor
Posts: 404
Joined: 2021-07-16 21:02
First name: Fabio
Last name: Alemagna

Re: Reading settings.xml file using PowerShell

#12 Post by oibaf » 2022-07-21 10:34

It will revert back because the file gets generated on the fly on each new saving. As said, this has been fixed and the fix will be available in the next nightly build and releases.

For now, your solution to remove the version info when loading up the xml should suffice.

Post Reply