powershell和FTP,脚本不等待传输完成

时间:2017-07-27 03:42:31

标签: powershell ftp wait pause

我有一个脚本,简而言之,它执行以下操作:

  1. 将所需文件复制到临时文件夹
  2. 将临时文件夹中的文件压缩为.zip文件
  3. 将.zip文件FTP到我们的FTP服务器
  4. 整理并删除临时文件夹和.zip文件
  5. 我已经压缩了上一篇文章的FTP代码: Upload files with FTP using PowerShell 并在必要时进行修改(保持基本功能 - 我认为)。

    我遇到的问题是,当.zip文件是FTP时,脚本不会等到它完成。在它继续执行之前,它可以在20Mb到60Mb之间完成,整理并删除它正在传输的文件。

    临时文件夹始终相同,但.zip文件名因日期而异,因此我无法完全颠倒操作顺序。

    有人可以建议我如何让脚本等到FTP进程完成,成功或失败,然后再继续运行吗?

    干杯,

    安德鲁。

    编辑:对于那些问过......

        function FTPtoServer ()
    {
    <#
        What this function has to/should do:
        - accept the right number of parameters, 
                    minimum/mandatory: username, password, file
                    optional: proxy server address/port, proxy username and password
        - check that the source file exists, then extract the filename.
        - if a proxy is specified, set the appropriate parameters
        - transmit the file
        - if any errors occur, throw and return
    #>
    
        param(
              [string]$sourcefile=$(throw 'A sourcefile is required, -sourcefile'), <#fully qualified zip file name#>
              [string]$FTPUser   =$(throw 'An FTP username is required, -ftpuser'),
              [string]$FTPPass   =$(throw 'An FTP password is required, -ftppass'),
              #
              [string]$proxyServer,  #proxySocket?? it is an address and port
              [string]$proxyUser,
              [string]$proxyPass
              )
    
        #local variables
        $FTPserver = "ftp://ftp.servername.com.au"
    
        #check if the sourcefile exists, if not return/throw an error
        # The sourcefile should contain the full path to the file.
        if (-not (test-path $sourcefile)){
            throw "the source file could not be located: $sourcefile"
        }
    
        # extract the filename from the sourcefile.  
        $filename = split-path -path $sourcefile -leaf
    
        # create the FtpWebRequest and configure it
        $ftp = [System.Net.FtpWebRequest]::Create("$FTPserver/$filename")
        $ftp = [System.Net.FtpWebRequest]$ftp
        $ftp.Method = [System.Net.WebRequestMethods+Ftp]::UploadFile
        $ftp.Credentials = new-object System.Net.NetworkCredential($FTPUser,$FTPPass)
        $ftp.UseBinary = $true
        $ftp.UsePassive = $false
    
        #proxy info
        # ******** DANGER Will Robinson - this proxy config has not been
        #                                 tested and may not work.
        if ($proxyServer){
            $proxy = New-Object System.Net.WebProxy $proxyServer
    
            if ($proxyUser -and $proxyPass){
                $proxy.Credentials = new-object System.Net.NetworkCredential($proxyUser,$proxyPass)
            }
    
            $ftp.Proxy = $proxy
            $ftp.UsePassive = $true #apparently, must usePassive if using proxy
    
        }    
    
    #now we have checked and prepared everything, lets try and send the file.
    
        # read in the file to upload as a byte array
        try{
            #work out how much we are sending
            $content = [System.IO.File]::ReadAllBytes("$sourceFile")
            $ftp.ContentLength = $content.Length
    
            try {
                # get the request stream, and write the bytes into it
                $rs = $ftp.GetRequestStream()
                $rs.Write($content, 0, $content.Length)
    
                # be sure to clean up after ourselves
                $rs.Close()
                $rs.Dispose()
            }
            catch {
                $errorMessage = "FTP failed. " + $_.exception.message
                throw $errormessage
            }
        }
        catch {
            $errorMessage = "Unable to transmit file " + $sourceFile + "`r`n" + $_.exception.message
            throw $errormessage
        }
    }
    

    以上是单独的文件,但由以下内容调用:

    try {
        FTPtoServer -sourcefile $sourcefile -ftpuser $FTPUser -ftppass $FTPPass
    }    
    catch {
        $errorMessage = "FTPtoServer function failed with error: $_"
        finishFail -failmessage $errorMessage
    }
    

    干杯。

2 个答案:

答案 0 :(得分:1)

找到它。

我使用一个大文件(~140Mb)隔离执行上面的FTP代码并抛出错误; &#34;底层连接已关闭:接收时发生意外错误。&#34;

我重新启动了FTP服务器,检查了用户帐户等等。

我还使用相同的文件测试了M $ FTP客户端,并且它完全正确地传输。

无论如何,我发现这篇文章:https://www.codeproject.com/Questions/597175/FileplusUploadplustoplusFTPplusserver也有我收到的错误。

事实证明,FTPWebRequest的超时值不是像doco那样的-1,而是100秒。

我检查了我的FTP日志,果然,登录和注销之间的时间大约是100秒。

我在我的代码中添加了一行:$ ftp.Timeout = -1并首次尝试完全传输整个文件而没有错误。

之前的转移已经有效,因为它们低于100秒超时。

非常感谢帖子和帮助。

答案 1 :(得分:0)

我自己使用另一种oldschool方法,它应该适合你,并且不需要服务器上的任何额外组件。

$ftp_user = "username"
$ftp_password = "password"
$ftp_address = "ftp.someserver.com"

$ftp_commands = @"
open $ftp_address
$ftp_user
$ftp_password
lcd c:\jobs\output
put estate_data_current.xml
bye
"@

set-content -encoding "ASCII" -path ftp_commands.txt -value $ftp_commands

ftp -s:ftp_commands.txt