powershell - 当我尝试运行FtpWebRequest.GetRequestStream时获取 “The remote server returned an error: (500) Syntax error, command unrecognized”

标签 powershell ftp ftpwebrequest

我有以下代码将文件发送到FTP服务器。

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
}

我使用的URI是“ftp://xxx.xxx.xxx.xxx/aaa/bbb/ccc/R1ACTIVE.TXT”。
FTP服务器是vsftpd

大多数情况下,文件已上传。但是有时候我尝试运行$ftp.GetRequestStream()时遇到以下错误:

远程服务器返回错误:(500)语法错误,无法识别命令。

为什么???

最佳答案

我通过使用以下方法解决了它:

$ftp.KeepAlive = $false

关于powershell - 当我尝试运行FtpWebRequest.GetRequestStream时获取 “The remote server returned an error: (500) Syntax error, command unrecognized”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2067313/

相关文章:

Powershell New-ScheduledTaskSettingsSet

powershell - 在 PowerShell 中重用正在运行的 PuTTY 代理(选美)

java - Apache Commons FTPClient,检查远程目录是否存在并获取权限(linux - unix)

c# - 使用 FtpWebRequest 查找 FTP 服务器的子文件夹

powershell - 将写入进度添加到 Get-Job/Wait-Job

html - 在 PowerShell 管道中有选择地格式化数据并输出为 HTML 的技术

c - 如果 sftp 无法将文件放入 sftp 服务器,如何获取并处理 C 程序中的返回代码?

c# - FTP上传文件 使用HTTP代理时不支持请求的FTP命令

c# - FtpWebRequest ListDirectory 不返回所有文件

.net - FtpWebRequest/FtpWebResponse 上的 OPTS 命令