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.
I don't know if this will be read by you, James, but I have to say this about Powershell: It's still not as good as BASH. On tab, it will autocomplete for things in the directory you're in, but BASH will search the path for all the files that match what you've typed in, so far. If there's one, it will autocomplete and if there's more than one, it will list them.
I'll still take BASH/Cygwin.
Oh man.. I hear what you're saying, and yes the tab-completion has got some heat about being weak in comparison. Even Microsoft was pretty forward about that, and there's lots of profile-plugins and snippets for the tab lovers.
So, while I conceed to you about the tab (not that I personally care), the wickedness of powershell is that it uses full & live objects. not just that you can say "new-object System.FooBar.MyClass" and stuff, but the pipe-line is *not* text-based, but object-based.
When I do an "ls" and pipe it into something, I don't have to worry that it's characters 23-47 that make up the filename, and characters 10-22 that make up the filesize, or whatever. I just type $_.FullName or $_.Length, and it even knows the type! so I can say $_.FullName.SubString(3,21) or whatever.
Genious!! I have done more than enough string-parsing in my life, I'll skip it whenever possible. It's error prone and typically a pain in the ass.
Nevermind that I can do junk like
([xml](svn info --xml)) and turn svn's text-based xml output into a full xml document object that I can instantly navigate with xpath, or do any other xml stuff with it (modify it, save it back to disk, whatever)
HOT DAMN!!!!
No argument here. The thing is powerful. It's just frustrating when you don't know the exact name of a command.
Agreed basic Tabcompletion is a bit limited in PowerShell, but it is extendable.
For an Idea of what tabcompletion can ( or be made to ) do in PowerShell take a look a my PowerTab :
http://thepowershellguy.com/blogs/posh/pages/powertab.aspx
for a short flash demo, if your not using PowerShell Yet ;-) :
http://thepowershellguy.com/blogs/posh/archive/2007/06/02/powertab-flash-exampes.aspx
Enjoy,
Greetings /\/\o\/\/
I you guys are so on about the autocomplete thing... may i suggest powershellplus. Its an ide for powershell and has all the niceties (event its own script editor with a debugger!!! Veeeeeeery cool)
Just google powershellplus.
Its currently beta and free for non-commercial use