从 Windows Server 2012,使用PoweShell
,我通过连接到远程设备ftp
并运行get
以检索文件。该过程顺利完成,但文件未保存到我的本地计算机上。命令返回Operation Complete
,几秒钟后连接关闭。
Operation CompleteConnection closed by remote host.
ftp>
在目标位置,0
在过程开始时会创建一个大小为的临时文件,并且该文件保持不变。Tmp6A94.tmp
我尝试按照以下方法打开防火墙如何为被动模式 FTP 服务器配置 Windows 防火墙
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
我已经ftp
在另一台 WS2012 和一台 WS2012R2 上测试了该行为,这两台机器没有同样的问题。它们都没有防火墙 ftp 被动模式。我想知道是否还有其他防火墙规则可以启用 ftp 传输。
编辑2
这是PowerShell
我用来通过以下方式从远程设备检索文件的脚本ftp
:
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
ftp
现在,当从终端使用时它可以工作PowerShell
,而不能通过脚本工作,从运行PowerShell
。
答案1
我不确定该方法背后的具体过程WebClient::DownloadFile
,但在我看来,它会在下载传输完成之前中断 ftp 连接,导致操作失败。
通过使用异步方法,我成功下载了文件。
这是我使用以下更新的函数: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." }
}