What's the best way to automate secure FTP in

2019-01-16 11:58发布

问题:

I'd like to automate the FTP download of a database backup file using PowerShell. The file name includes the date so I can't just run the same FTP script every day. Is there a clean way to do this built into PowerShell or using the .Net framework?

UPDATE I forgot to mention that this is a through a secure FTP session.

回答1:

After some experimentation I came up with this way to automate a secure FTP download in PowerShell. This script runs off the public test FTP server administered by Chilkat Software. So you can copy and paste this code and it will run without modification.

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

I found a lot of helpful information at these links

  • FTP downloads: encoding problems
  • Simple FTP demo application
  • Very Simple FTP Client

If you want to use an SSL connection you need to add the line

$ftprequest.EnableSsl = $true

to the script before you call GetResponse(). Sometimes you may need to deal with a server security certificate that is expired (like I unfortunately do). There is a page at the PowerShell Code Repository that has a code snippet to do that. The first 28 lines are the most relevant for the purposes of downloading a file.



回答2:

Taken from 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)

Works for me



回答3:

As far as PowerShell goes, the /n Software NetCmdlets package includes FTP cmdlets (including support for both secure FTP types) that you could use pretty easily for this.



回答4:

This isn't as simple as I wish it was. There are three options that I know of.

1) .Net - You can use the .Net framework to do this in PS, but it involves raw socket manipulation that I wouldn't want to do in a script. If I were going this route, then I would wrap up all the ftp junk into a DLL in C# then use that DLL from powershell.

2) Manipulating a file - If you know the pattern of the name of the file that you need to get each day then you could just open your ftp script with PS and change the name of the file in the script. Then run the script.

3) Pipe text to FTP - The last option is to use powershell to pipe info into and out of your FTP session. See here.



回答5:

The JAMS Job Scheduler offers some cmdlets that would make this task simple. It has a variety of FTP cmdlets for secure sessions as well as date cmdlets for converting natural dates into .Net date objects such as "Last day of month":

JAMS Job Scheduler Cmdlets



回答6:

Something like this could work:

$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
}

Check this: Powershell: compress backups and FTP transfer



回答7:

I have successfully used the Indy Project .NET library to do FTP. And...ugh, looks like the hosted .NET build is no longer available.



回答8:

$realdate = (Get-Date).ToString("yyyyMMdd")

$path = "D:\samplefolderstructure\filename" + $realdate + ".msi"

ftps -f $path -s:D:\FTP.txt

ftp.txt is the way we keep all of our connection info to the ftp server. ftps is the client we use obviously, so might have to change a few things. But, this should help you ge the idea.