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.
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
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.
I have successfully used the Indy Project .NET library to do FTP. And...ugh, looks like the hosted .NET build is no longer available.
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.
I found a lot of helpful information at these links
If you want to use an SSL connection you need to add the line
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.
$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.
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.