在Windows Server 2012中,使用PoweShell ,我通过ftp连接到远程设备,并运行get来检索文件。 该过程没有问题,但文件不会保存到我的本地计算机上。 该命令返回Operation Complete ,几秒钟后连接closures。
Operation CompleteConnection closed by remote host. ftp>
在目标位置,在进程开始时会创build大小为0的临时文件,并且该文件保持不变。 Tmp6A94.tmp
我试图打开防火墙按照如何为被动模式FTP服务器configurationWindows防火墙
netsh advfirewall firewall add rule name=”FTP Service” dir=in protocol=TCP enable=yes action=allow profile=any service=ftpsvc localport=any netsh advfirewall set global StatefulFTP disable
我错过了什么?
编辑1
我已经testing了另外一个WS2012和WS2012R2上的ftp行为,两者并没有相同的问题。 他们都没有防火墙的FTP被动模式。 我想知道是否可能有一些其他的防火墙规则,使ftp传输。
编辑2
这是我用来通过ftp从远程设备检索文件的PowerShell脚本:
function getFTPFile([String]$fileName) { $ftpUser = "user" $ftpPassword = "password" $ftpServer = "ftpServer" $webclient = New-Object System.Net.WebClient $webclient.Credentials = New-Object System.Net.NetworkCredential($ftpUser, $ftpPassword) $uri = New-Object System.Uri("$ftpServer/files") $webclient.DownloadFile($uri, $fileName) }
运行此脚本或从PowerShell控制台手动执行该脚本会产生相同的结果。 一切都将正常运行,直到文件需要保存在目的地。 我已经成功地在其他Windows服务器上使用这个脚本。
这是脚本引发的错误:
Exception calling "DownloadFile" with "2" argument(s): "The remote server returned an error: 150-Starting operation: STATUS: Getting logs ... Please wait... Please wait... STATUS: Finished getting logs STATUS: get logs operation is complete Size: 8262246 bytes Please wait for 8 seconds ... Operation Complete150-Accepted data connection 150 (8262246 bytes) to download ." At C:\Users\administrator\getFTPFile.ps1:73 char:2 + $webclient.DownloadFile($uri, $fileName) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : WebException
这是PowerShell提示符下的失败操作:
ftp> get logs logsFile 200 PORT command successful 150-Starting operation: STATUS: Getting logs ... Please wait... Please wait... STATUS: Finished getting logs STATUS: get logs operation is complete Size: 8283146 bytes Please wait for 8 seconds ... Operation CompleteConnection closed by remote host.
这是传输成功时的输出:
ftp> get logs logsFile 200 PORT command successful 150-Starting operation: STATUS: Getting logs ... Please wait... Please wait... STATUS: Finished getting logs STATUS: get logs operation is complete Size: 8283146 bytes Please wait for 8 seconds ... Operation Complete150-Connecting to port 63596 150 (8275012 bytes) to download 226-File successfully transferred 226 0.778 seconds (measured here), 10.15 Mbytes per second ftp: 8275012 bytes received in 0.76Seconds 10916.90Kbytes/sec.
另外,我还没有尝试过任何其他的FTP客户端。
编辑3
现在,它使用从PowerShellterminal的ftp ,而不是通过脚本工作,从PowerShell 。
我不确定WebClient::DownloadFile方法背后的过程究竟是什么,但在我看来,在下载传输完成之前它正好中断了ftp连接,导致操作失败。
通过使用asynchronous方法,我成功地下载了文件。
这是我使用WebClient::DownloadFileTaskAsync方法更新的函数。 我也执行了状态检查。
function getFTPfile([String]$fileName) { $ftpUser = "user" $ftpPassword = "password" $ftpServer = "ftpServer" $webclient = New-Object System.Net.WebClient $webclient.Credentials = New-Object System.Net.NetworkCredential($ftpUser, $ftpPassword) $uri = New-Object System.Uri("$ftpServer/files") $job = $webclient.DownloadFileTaskAsync($uri, $fileName) while(!$job.IsCompleted) { Write-Host "Downloading file..." sleep 10 } if($job.Status -ne "RanToCompletion") { Write-Host "Failed to download file." } else { Write-Host "Download successful." } }