Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

powershell - Get "The remote server returned an error: (500) Syntax error, command unrecognized" when I try to run FtpWebRequest.GetRequestStream

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???

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I solved it by using the following:

$ftp.KeepAlive = $false

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...