简体   繁体   中英

PowerShell FTP Downloading Files Returns 421 Service Not Avaliable Error

I have built a script for downloading files from an FTP server. The script is working well with all the servers I have tested expect for one. For this server I am able to get the directory listing, but when I attempt to download a file it fails and returns a "421 Service Not Available, closing control connection" error. Below is the code I am using for downloading a file.

function Get-FtpFile
{
    Param ([string]$fileUrl, $credentials, [string]$destination)
    try
    {
        $FTPRequest = [System.Net.FtpWebRequest]::Create($fileUrl)
        if ($credentials)
        {
            $FTPRequest.Credentials = $credentials
        }
        $FTPRequest.Method = [System.Net.WebRequestMethods+Ftp]::DownloadFile
        $FTPRequest.UseBinary = $true
        $FTPRequest.UsePassive = $true

        # Send the ftp request
        $FTPResponse = $FTPRequest.GetResponse()

        # Get a download stream from the server response
        $ResponseStream = $FTPResponse.GetResponseStream()

        # Create the target file on the local system and the download buffer
        $LocalFile = New-Object IO.FileStream ($destination,[IO.FileMode]::Create)
        [byte[]]$ReadBuffer = New-Object byte[] 1024

        # Loop through the download
        do {
            $ReadLength = $ResponseStream.Read($ReadBuffer,0,1024)
            $LocalFile.Write($ReadBuffer,0,$ReadLength)
        }
        while ($ReadLength -ne 0)

        $ResponseStream.Close()
        $ReadBuffer.clear()
        $LocalFile.Close()
        $FTPResponse.Close()
    }
    catch [Net.WebException]
    {
        return "Unable to download because: $($_.exception)"
    }
}

I am able to download files from this server using the Windows File Explorer FTP so I do not think this is a problem with the server itself.

One interesting thing I have noticed through testing is that when the server returns the directory listing each filename has a trailing NULL included. I have tried downloading files both including and not including this trailing NULL, both attempts produce the same error code.

Has anyone seen a similar error before? Or is anyone aware of the differences in the way that Windows File Explorer uses FTP compared with the PowerShell script I have listed above?

This issue I had was I was not correctly removing the trailing NULL in each filename. Once I was able to correctly remove the NULLs I no longer had any issues.

If anyone runs into this problem in the future, encountering 421 errors when attempting to download files, check if the filenames being provided by the directory listing have trailing NULL characters. If so, delete these characters from the filename strings before attempting to download each file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM