Powershell FTP脚本

时间:2015-07-14 11:57:00

标签: powershell ftp

我在循环中使用此脚本从服务器下载大量文件,效果非常好。

$sourceuri = "ftp://ftp.secureftp-test.com/hamlet.zip"
$targetpath = "C:\hamlet.zip"
$username = "test"
$password = "test"

# Create a FTPWebRequest object to handle the connection to the ftp server
$ftprequest = [System.Net.FtpWebRequest]::create($sourceuri)

# set the request's network credentials for an authenticated connection
$ftprequest.Credentials =
New-Object System.Net.NetworkCredential($username,$password)

$ftprequest.Method = [System.Net.WebRequestMethods+Ftp]::DownloadFile
$ftprequest.UseBinary = $true
$ftprequest.KeepAlive = $false

# send the ftp request to the server
$ftpresponse = $ftprequest.GetResponse()

# get a download stream from the server response
$responsestream = $ftpresponse.GetResponseStream()

# create the target file on the local system and the download buffer
$targetfile = New-Object IO.FileStream ($targetpath,[IO.FileMode]::Create)
[byte[]]$readbuffer = New-Object byte[] 1024

# loop through the download stream and send the data to the target file
do{
    $readlength = $responsestream.Read($readbuffer,0,1024)
    $targetfile.Write($readbuffer,0,$readlength)
}
while ($readlength -ne 0)

$targetfile.close()

但有时目标文件不在那里,然后这个脚本开始刷新我的命令提示符并出现错误。这有点像调试,缺少哪些文件。有没有办法,只要有一个丢失的文件转到这个脚本并跳到下一个?

这是错误:

Exception calling "GetResponse" with "0" argument(s): "The remote server returned a
n error: (550) File unavailable (e.g., file not found, no access)."
At D:\powershell\IPO_preveri_CMD_ALL\ftp.ps1:37 char:5
+     $ftpresponse = $ftprequest.GetResponse()
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : WebException

You cannot call a method on a null-valued expression.
At D:\powershell\IPO_preveri_CMD_ALL\ftp.ps1:40 char:5
+     $responsestream = $ftpresponse.GetResponseStream()
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

最后6行然后重复...

1 个答案:

答案 0 :(得分:1)

听起来你只想要一个简单的尝试。我不知道错误处理[System.Net.FtpWebRequest]必须提供什么,所以这种通用方法是一个很好的起点。

$SavedErrorActionPreference = $ErrorActionPreference
$ErrorActionPreference = "Stop" 

Try{
    # send the ftp request to the server
    $ftpresponse = $ftprequest.GetResponse()

    # get a download stream from the server response
    $responsestream = $ftpresponse.GetResponseStream()

    # create the target file on the local system and the download buffer
    $targetfile = New-Object IO.FileStream ($targetpath,[IO.FileMode]::Create)
    [byte[]]$readbuffer = New-Object byte[] 1024

    # loop through the download stream and send the data to the target file
    do{
        $readlength = $responsestream.Read($readbuffer,0,1024)
        $targetfile.Write($readbuffer,0,$readlength)
    }
    while ($readlength -ne 0)

    $targetfile.close()
} Catch {
    # Notify user an error occured. 
    Write-Warning "Unable to download '$sourceuri' because: $($_.exception)"
}

$ErrorActionPreference = $SavedErrorActionPreference 

因此,如果在try块中发生任何终止错误,则停止处理并执行catch块。从它的外观来看,这些错误并没有终止,因此我们需要暂时将其更改为Stop以确保try块功能正常。

然后在catch块中,我们采用最简单的方法,只写出失败的文件路径,同时提供与错误相关的消息。使用上面的例子,你应该得到类似的东西:

  

无法下载' ftp://ftp.secureftp-test.com/hamlet.zip'因为:远程服务器返回错误:(550)文件不可用(例如,找不到文件,没有访问权限。)

这应该在第一个错误时停止。通过停止处理,它将避免空表达错误。