2008-11-05 170 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

相关问题