3

I have following code to send files to a FTP server.

function FtpUploader(
  [string]$uri,
  [string]$localeFile,
  [string]$user = "ftp",
  [string]$password = "ftp",
  [int]   $timeout  = 20000
){
    trap {
      Write-Host ("ERROR: " + $_) -Foregroundcolor Red
      return $false 
    }

    $ftp             = [System.Net.FtpWebRequest]::Create($uri)
    $ftp             = [System.Net.FtpWebRequest]$ftp
    $ftp.Method      = [System.Net.WebRequestMethods+Ftp]::UploadFile
    $ftp.Credentials = new-object System.Net.NetworkCredential($user, $password)  
    $ftp.Timeout     = $timeout
    $ftp.UseBinary   = $false
    $ftp.UsePassive  = $true

    $content         = Get-Content -en byte $localeFile

    $rs              = $ftp.GetRequestStream()
    $rs.Write($content, 0, $content.Length)

    $rs.Close()
    $rs.Dispose()

    return $true
}

The URI I use is "ftp://xxx.xxx.xxx.xxx/aaa/bbb/ccc/R1ACTIVE.TXT". The FTP server is vsftpd

Most of the time, the file is uploaded. But sometime I get following error when it try to run $ftp.GetRequestStream():

The remote server returned an error: (500) Syntax error, command unrecognized.

Why???

magol
  • 6,135
  • 17
  • 65
  • 120
  • Which FTP server? Not all FTP servers support all FTP commands. – Seth Jan 14 '10 at 20:39
  • The server i vsftp on RedHat Linux – magol Jan 14 '10 at 20:44
  • @magol - I think I have the same problem. Were you getting this error only for the big files ? I have a related problem in C#. The language does not matter, only the FTP matters. Question - https://stackoverflow.com/questions/21221300/fixing-system-net-webexception-the-remote-server-returned-an-error-500-syn – Steam Feb 18 '14 at 22:06

1 Answers1

3

I solved it by using the following:

$ftp.KeepAlive = $false
Adrian Toman
  • 11,316
  • 5
  • 48
  • 62
magol
  • 6,135
  • 17
  • 65
  • 120