Wednesday, May 16, 2007

Powershell is the ultimate one-liner scripting language

Behold! XML parsing from the last post is now doing sub-expressions, type conversions and formatting all in one line.

$nav.Select("/Season/Games/Game") | select-object -first 5 | Format-Table -property @{Label="Date";Expression={[DateTime]::Parse($_.SelectSingleNode("Date").Value).ToShortDateString()};Width=10},@{Label="AwayTeam";Expression={$_.SelectSingleNode("AwayTeam/Name").Value};Width=15},@{Label="AwayScore";Expression={$_.SelectSingleNode("AwayScore").Value};Width=10},@{Label="HomeTeam";Expression={$_.SelectSingleNode("HomeTeam/Name").Value};Width=15},@{Label="HomeScore";Expression={$_.SelectSingleNode("HomeScore").Value};Width=10},@{Label="Winner";Expression={if([Int32]::Parse($_.SelectSingleNode("AwayScore").Value) -gt [Int32]::Parse($_.SelectSingleNode("HomeScore").Value)) { $_.SelectSingleNode("AwayTeam/Name").Value } else { $_.SelectSingleNode("HomeTeam/Name").Value }};Width=15}

Date AwayTeam AwayScore HomeTeam HomeScore Winner
---- -------- --------- -------- --------- ------
11/7/2006 Vermont 82 New Orleans 65 Vermont
11/7/2006 Hampton 75 Maryland 102 Maryland
11/8/2006 Hampton 74 New Orleans 73 Hampton
11/8/2006 Youngstown St. 74 Central Mich... 50 Youngstown St.
11/8/2006 Brown 34 Michigan St. 45 Michigan St.

Obviously, this script could really use some carriage returns.

What I like about this example is that it proves how much I can prototype using a command shell. This is something I've always loved about Python. Prototype a script in the interactive Python shell and once you've worked out exactly how you want to do something, copy your work into the full-fledged script. And as you write the script, all along you can test its inputs with the interactive shell.

Of course, shell scripts should be using sparingly, right? That's generally true, but I'm not sure how people will react to Powershell. The fact that it pipes objects around fixes a lot of the string-munging crap in regular old shell scripts. Remember when we started using Perl because otherwise we'd be writing sh that had a bunch of calls to awk in it? Perl was a response to all the things that people wanted to do with bash or ksh but it would be too ugly to do because of sed and awk.

As a shell language, the "PS1" language is otherwise pretty standard. I think two categories of people will feel right at home: unix shell scripters who now work on Windows and .NET junkies. I happen to come from both lineages.

Sadly, Powershell is not aimed at replacing automation done with VBS -- at least not easily. I have some VBS scripts I'd love to get rid of, but Powershell

  1. Doesn't let you run scripts unless you do a bunch of security settings
  2. Doesn't allow you to double click to run scripts.

Combined, these don't make PS very viable as a replacement for VBScript. Right now it's the kind of thing that you set up and configure only for yourself to use to save time. You can't distribute scripts to users and have a prayer without doing a bunch of additional busywork. One way to get around 1. is to sign scripts, but as one person replied to a blog elsewhere about signing powershell scripts, "How many people are really going to run Powershell in restricted mode for more than five minutes?" It's just not worth it if you have to go through a certificate rigmarole just to run a simple script.

Anyway, this first version is pretty promising. I can see myself using it to save some time here and there.

No comments: