2008-11-05 108 views
26

我想使用PowerShell自動完成數據庫備份文件的FTP下載。文件名包括日期,所以我不能每天運行相同的FTP腳本。有沒有一種乾淨的方式來做到這一點內置於PowerShell或使用.Net框架?在PowerShell中自動安全FTP的最佳方式是什麼?

UPDATE我忘了提及這是一個通過安全的FTP會話。

回答

1

這並不像我希望的那麼簡單。我知道有三個選項。 1).Net - 您可以使用.Net框架在PS中執行此操作,但它涉及我不想在腳本中執行的原始套接字操作。如果我走這條路線,那麼我會把所有的ftp垃圾包裝到C#中的一個DLL中,然後從powershell使用該DLL。

2)操作文件 - 如果您知道每天需要獲取的文件名稱模式,則可以使用PS打開您的ftp腳本並更改腳本中文件的名稱。然後運行腳本。

3)將文本傳送到FTP - 最後一個選項是使用powershell將信息傳入和傳出FTP會話。請參閱here

7

here

$source = "ftp://ftp.microsoft.com/ResKit/win2000/dureg.zip" 
$target = "c:\temp\dureg.zip" 
$WebClient = New-Object System.Net.WebClient 
$WebClient.DownloadFile($source, $target) 

作品兩者對我來說

+0

這是我應該有思想的有效第四選項。如果您提前知道文件的名稱,那麼這當然是一個很好的解決方案,您應該接受它作爲答案。 – EBGreen 2008-11-05 15:31:13

0

我已經成功地使用了印項目.NET庫做的FTP。並且...呃,看起來託管的.NET構建不再可用。

0

$ realdate =(獲取最新)的ToString( 「年月日」)

$ PATH = 「d:\ samplefolderstructure \文件名」 + $ realdate + 「的.msi」

FTPS -f $路徑-s:D:\ FTP.txt

ftp.txt是我們將所有連接信息保存到ftp服務器的方式。 ftps是我們明顯使用的客戶端,因此可能需要更改一些內容。但是,這應該有助於你創造這個想法。

4

就PowerShell而言,/ n軟件NetCmdlets軟件包包含FTP cmdlet(包括支持兩種安全FTP類型),您可以非常輕鬆地使用它。

24

經過一番實驗後,我想出了用這種方法在PowerShell中自動安全FTP下載。該腳本運行由Chilkat Software管理的public test FTP server。因此,您可以複製並粘貼此代碼,並且它將不加修改地運行。

$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() 

我發現了很多有用的信息,在這些鏈接

如果你想使用你需要添加SSL連接該行

$ftprequest.EnableSsl = $true 

在調用GetResponse()之前的腳本。有時你可能需要處理已過期的服務器安全證書(就像我不幸的那樣)。在PowerShell Code Repository有一個頁面,它有一個代碼片段來完成這個任務。前28行對於下載文件來說是最相關的。

+1

+1,用於查找和發佈代碼解決方案。但是,在代碼中看到一些評論會很高興。通過安全我假設你的意思是一個用戶/通行證提供,而不一定是SFTP? – 2008-11-06 15:48:33

1

JAMS Job Scheduler提供了一些使這個任務變得簡單的cmdlet。它有多種FTP的cmdlet用於安全會議以及最新的cmdlet的轉換自然日期,日期的.Net對象,如「上個月的一天」:

JAMS Job Scheduler Cmdlets

1

像這樣的東西可以工作:

$bkdir = "E:\BackupsPWS" #backups location directory 
$7Zip = 'C:\"Program Files"\7-Zip\7z.exe' #compression utility 
$files_to_transfer = New-Object System.Collections.ArrayList #list of zipped files to be transferred over FTP 
$ftp_uri="myftpserver" 
$user="myftpusername" 
$pass="myftppassword" 

#find .bak files not zipped yet, zip them, add them to the list to be transferrd 
get-childitem -path $bkdir | Sort-Object length | 
where { $_.extension -match ".(bak)" -and 
-not (test-path ($_.fullname -replace "(bak)", "7z")) } | 
foreach { 
$zipfilename = ($_.fullname -replace "bak", "7z") 
Invoke-Expression "$7Zip a $zipfilename $($_.FullName)" 
$files_to_transfer.Add($zipfilename) 
} 

#find .bak files, if they've been zipped, delete the .bak file 
get-childitem -path $bkdir | 
where { $_.extension -match ".(bak)" -and 
(test-path ($_.fullname -replace "(bak)", "7z")) } | 
foreach { del $_.fullname } 

#transfer each zipped file over FTP 
foreach ($file in $files_to_transfer) 
{ 
$webclient = New-Object System.Net.WebClient 
$webclient.Credentials = New-Object System.Net.NetworkCredential($user,$pass) # FTP credentials 
$ftp_urix = $ftp_uri + "/" + $file.Substring($bkdir.Length + 1) # ftp address where to transfer the file 
$uri=[system.URI] $ftp_urix 
$webclient.UploadFile($uri, $file) #transfer the file 
} 

檢查:Powershell: compress backups and FTP transfer

相關問題