Look what you can do in Powershell: FTP!
Imagine my surprise when my friendly neighbourhood I.T. fellow told me a ftp command line client built into windows. Man, I haven't done command line FTP since high school. But after finding a good ftp command reference, well, of course, I couldn't resist trying to concoct an unholy marriage between ftp and powershell.
$listOfFiles = "open secret.ftplocation.com
user username password
binary
cd LocationICareAbout
mdir * -" | ftp -i -n | where {$_.Trim() -ne "" } | where {$_.SubString(24,5) -ne "<DIR>"} | %{ $_.SubString(39) }
To explain: FTP can accepted piped-input, but I realized instead of having to put my answerfile into an actual file, I could just use powershell's ability to have multi-line strings and variables pumped right in.
The above text is logging into the ftp location client of yer choice and getting a file listing (the msdir bit). As I only cared about files, I ignore the "<DIR>"s, and with a bit of basic string manipulation, viola! I've got a list of all files in a given directory
$cmd = "open secret.ftplocation.com
user username password
binary
cd LocationICareAbout
" +
($listOfFiles | %{ "get ""$_""`n" })
$cmd | ftp -i -n
To explain: This final bit turns the list of files into another literal string, which says
get FileX
get FileY
etc, which gets piped to ftp. the end result is a download of all the files in the directory to my hard drive. Of course, you could get creative and do all sorts of manipulations to $listOfFiles, if that's what turns you on.
I've got another nearly identical script where instead of "get" I say "delete", same concepts. And got another one that builds up $listOfFiles from a local directory (via ls) and uses "put" to upload all files from a local dir to the remote dir. You get the idea.
ftp supposedly supports a -s:filename but I couldn't get it to work. And really, who would want to? Inlining the ftp commands is just so damn sexy!
After Thoughts:
(wherein I blather on and on and get less technical)
Man I had a blast doing this. Within about two hours I'd gone from ftp n00b to having working scripts that actually helped me do my job much faster. I'd been using my favorite abadonware ftp gui to do uploads/downloads and now I had scripts that did it all faster and consistently.
A nice bite-sized problem involving unknown, but good, tools combined with the joy of the admin development model meant figuring it all out was fun for me. The scripts are pretty basic, really, but figuring junk out makes me go a big rubbery one :P
Really, it's that lovely admin development model more than anything else I think I love. (I've done lots of it over my life, but only ever heard the phrase thanks to powershell blogs.) I have built up so many extrodinarily useful scripts over the last few months, by typing commands into a stupid blue window until they worked, then pasting them into notepad and saving it.
Within another month or two I'll have fully automated processes at my job that used to take up days and days and days of peoples times. Of course, it would only take 1 or 2 weeks of dedicated time to do it all, but we all know how that goes.. no one's going to give you official time to fix anything, not that you've got time when everything needs to be done yesterday, so you gotta slip it under the wire. But then! To the rescue! Comes that lovely admin development model (some people might call it agile development too), where I don't have to do it all at once. I just have to add a few more lines to my powershell scripts here and there, and one day *poof* the entire process is a repeatable automated one-liner instead of a clusterfuck of a timewasting error prone inconsistent undocumented manual process.
All part of my grand plan for world domination. One dirty hack at a time.