【发布时间】:2015-12-16 22:24:30
【问题描述】:
我是 PowerShell 和 FTP 脚本的新手,所以请善待。我已经搜索了很多,但找不到使用脚本从 FTP 服务器简单地下载文件的方法。我从cmd.exe 开始并尝试了ftp 子命令,但没有成功。所以我切换到 PowerShell (v4.0)。我看过各种下载 FTP 文件的示例,但没有一个对我有用。这是我最近的尝试:
$Username = "username"
$Password = "password"
$LocalFile = "C:\Users\myuser\MyFile.xlsx"
$RemoteFile = "ftp://ftp.example.com/MyFile.xlsx"
$req = [System.Net.FtpWebRequest]::Create($RemoteFile)
$req.Credentials = New-Object System.Net.NetworkCredential($Username, $Password)
$req.Method = [System.Net.WebRequestMethods+Ftp]::DownloadFile
$req.UseBinary = $true
$req.UsePassive = $true
$req.KeepAlive = $false
# Everything works to this point.
try {
# This throws exception.
$resp = [System.Net.FtpWebResponse]$req.GetResponse()
}
catch {
$msg = $_.Exception
Write-Host "Exception: $msg" -ForegroundColor Red
}
# More code will go here later to write the data to the local file.
# Clean up.
if ($resp -ne $null) {
$resp.Close()
$resp = $null
}
我得到的错误是:
System.Management.Automation.MethodInvocationException: Exception calling "GetResponse" with "0" argument(s): "The remote server returned an error: 227 Entering Passive Mode (172,16,13,205,5,250).
." ---> System.Net.WebException: The remote server returned an error: 227 Entering Passive Mode (172,16,13,205,5,250).
. ---> System.Net.Sockets.SocketException: A socket operation was attempted to an unreachable network 172.16.13.205:1530
at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
at System.Net.Sockets.Socket.Connect(EndPoint remoteEP)
at System.Net.FtpControlStream.QueueOrCreateDataConection(PipelineEntry entry, ResponseDescription response, Boolean timeout, Stream& stream, Boolean& isSocketReady)
at System.Net.FtpControlStream.PipelineCallback(PipelineEntry entry, ResponseDescription response, Boolean timeout, Stream& stream)
at System.Net.CommandStream.PostReadCommandProcessing(Stream& stream)
at System.Net.CommandStream.ContinueCommandPipeline()
at System.Net.FtpWebRequest.TimedSubmitRequestHelper(Boolean async)
at System.Net.FtpWebRequest.SubmitRequest(Boolean async)
--- End of inner exception stack trace ---
at System.Net.FtpWebRequest.SyncRequestCallback(Object obj)
at System.Net.CommandStream.Dispose(Boolean disposing)
at System.IO.Stream.Close()
at System.Net.ConnectionPool.Destroy(PooledStream pooledStream)
at System.Net.ConnectionPool.PutConnection(PooledStream pooledStream, Object owningObject, Int32 creationTimeout, Boolean canReuse)
at System.Net.FtpWebRequest.FinishRequestStage(RequestStage stage)
at System.Net.FtpWebRequest.GetResponse()
at CallSite.Target(Closure , CallSite , Object )
--- End of inner exception stack trace ---
at System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(FunctionContext funcContext, Exception exception)
at System.Management.Automation.Interpreter.ActionCallInstruction`2.Run(InterpretedFrame frame)
at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
我知道该文件存在并且可用,因为当我将ftp://username:password@ftp.example.com/MyFile.xlsx 粘贴到浏览器中时,系统会提示我下载该文件,并且一切正常。
FTP 服务器在我们的网络中,但我想要一个也适用于我们网络之外的 FTP 服务器的解决方案。
我做错了什么?
【问题讨论】:
标签: powershell ftp ftpwebrequest