2017-07-27 74 views
0

我有一個腳本,概括地說,執行以下操作:文件需要一個臨時文件夾 PowerShell和FTP,腳本不會等待傳輸完成

    1. 副本壓縮在臨時文件文件夾到一個.zip文件
    2. Tilt點數的.zip文件到我們的FTP服務器
    3. 收拾並刪除臨時文件夾和.zip文件

    我已經捏了上一篇文章中的FTP代碼: Upload files with FTP using PowerShell 並在必要時對其進行了修改(保留基本知識 - 我認爲)。

    我的問題是,雖然.zip文件被FTP'd腳本不等待,直到它完成。它可以在20Mb到60Mb的任何地方中途完成,然後繼續執行,整理並刪除正在傳輸的文件。

    的臨時文件夾始終是相同的,但該.zip文件名取決於日期,所以我真的不能反向操作的順序。

    任何人都可以建議我怎麼可能拿到劇本等到FTP過程已完成,成功或失敗,它進行過嗎?

    乾杯,

    Andrew。

    編輯:對於那些問....

    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 
    } 
    

    乾杯。

  • +4

    你的腳本在哪裏? –

    +0

    您是否檢查了[wait-process](https://ss64.com/ps/wait-process.html)是否可以幫助您?不知道它如何工作,雖然只有一個PowerShell命令。 –

    +0

    我無法想象,您在答案中指向的代碼不會等待上傳完成。向我們展示您的代碼! –

    回答

    0

    找到它了。

    我使用大文件(〜140Mb)隔離執行上面的FTP代碼,並拋出錯誤; 「底層連接已關閉:接收時發生意外錯誤。」

    我重新啓動FTP服務器,檢查用戶帳戶等等等等

    我也使用相同的文件測試M $的FTP客戶端,它完全和正確傳輸。

    無論如何,我發現這篇文章:https://www.codeproject.com/Questions/597175/FileplusUploadplustoplusFTPplusserver其中也有我收到的錯誤。

    事實證明,的FtpWebRequest的超時值不是-1作爲DOCO但100秒。

    我檢查了我的FTP日誌,果然,登錄和註銷之間的時間約爲100秒。

    我加了一行:$ ftp。超時= -1到我的代碼,第一次嘗試完整傳輸整個文件沒有錯誤。

    之前的傳輸已經在低於100秒的超時時間內工作。

    非常感謝這些帖子和幫助。

    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