我有以下代码将文件发送到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/