我有一個腳本,概括地說,執行以下操作:文件需要一個臨時文件夾 PowerShell和FTP,腳本不會等待傳輸完成
- 副本壓縮在臨時文件文件夾到一個.zip文件
- Tilt點數的.zip文件到我們的FTP服務器
- 收拾並刪除臨時文件夾和.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
}
乾杯。
你的腳本在哪裏? –
您是否檢查了[wait-process](https://ss64.com/ps/wait-process.html)是否可以幫助您?不知道它如何工作,雖然只有一個PowerShell命令。 –
我無法想象,您在答案中指向的代碼不會等待上傳完成。向我們展示您的代碼! –