Saturday, November 29, 2008

How things sometimes need a little "kick"


It's amazing how some things need a little kick to get enough of my attention and motivation to make me spend extra time exploring them. It must be a special kind of a kick since some "forgotten" things get to wait years.

Almost always when I find a tool or technology that intrigues me I jump right on exploring it. Right away I'd give it a few minutes to a few hours of my time, I'd read about it, play around with it, do something with it. When I could not devote instant attention immediately I would save it for later, often print a few pages and take it home to read, or just put it on hold with a thought to get back to it the very next available moment. The time usually comes, sooner or later, though something of course I never get back to...

Powershell had to wait a long time to finally get through to me. I looked at it briefly when it first came out in 2006 and I thought it's great and definitely worth looking at. I downloaded it but then put it "on hold" ("refrigerate or freeze after opening") and never got back to it. I knew it was out there, I thought I knew what it was (it turned out I did not), but until very recently I had no motivation to try it out and see for myself.

Last week my son brought home this nasty stomach flu virus and we had a few very rough days watching him recover. Health care is extremely powerful these days compared to 100 year ago but we still can't do much about viruses. Too bad. Human body has to get over it by itself, the only help it can get is in fighting symptoms.

It's all long gone now, but when virus was still alive, Denis wouldn't sleep well. I had to spend half the night close to him just watching him breathe, being ready to help if he wakes up. They say, there are three things a person can keep staring at for hours, fascinated: a burning fire, a flowing water, and other people working :) I now know that a parent can do the same just watching his child sleep...

Then, I figured, this extra time staying up at night was a great opportunity to catch up on some of the technology innovations that I left behind unexplored and thus unknown to me. This is how I ended up spending a few nights watching podcasts and reading blogs - a luxury I rarely get.

This year PDC announced a few very interesting innovations. I already knew what's coming in C# 4.0 from Eric Lippert's blog though listening to Anders Hejlsberg was definitely worth the time. Then there was a demo of CCR and DSS from robotics. This one impressed me a lot. Very nice and clean approach to true concurrent programming (Erlang, beware!) that is not done on a language level but instead delivered as a library, pure managed code! The guys said it would make its way into the core framework one day. There were a few other very interesting videos including introduction to F#, and then I spotted "Powershell" in the keywords (I was selecting what video to watch by tags that appealed to me from this list).

- Kick!

There was 2.0 to the familiar name that I thought I knew but never really looked at.

- Kick!

It's not fair to watch Powershell 2.0 video if I didn't look at the original stuff; I figured I could not appreciate new features unless I knew old ones.

- Kick!

Enough kicks. Poweershell indeed wanted me to look at it. I had motivation and I had time too.

--

I knew that Powershell was a much more powerful shell language than the old good MSDOS batch, I knew it had pipelines similar to Unix shell, and this was pretty much all I knew about it. When I needed something really simple scripted I could use batch and do *.cmd. For more complex stuff I would use JScript as I did once for bulk visio backwards compatibility conversion automation. Powershell never came out of the freezer I put it in and I was missing 2 out of 3 its key features. I knew the pipeline but without the other 2 it was "just" a pipeline. We have a tiny little pipelie in regular cmd today (netstat | find "80"), so why get too much excited about a more powerful one? here's why:

  • Powershell script operates cmdlets [command-lets]. It does not really have a "language" to it. Each command has a cmdlet behind it. And then cmdlet is just a .NET class that fulfills a few contracts. One can build a new one in a matter of minutes and register it with Powershell runtime

  • but more importantly cmdlets operate with objects, "real" objects with attributes and methods. One command pipes in objects into another one and off it goes. The command implementation can read incoming objects, run them, do anything a language cmdlet is written in lets you do with objects.

Let me show you. As you can imagine Powershell does provide equivalents of well known DOS commands, ones like "dir". In Powershell it's now the Get-ChildItem cmdlet that can do any listings thru providers, not only filesystem, but it does not matter for what I want to show you. And one can do "dir" as Powershell supports aliasing:

PS > dir | get-member

what I asked Powershell to do is to explain me the objects that "dir" cmdlet returns. here's the output:



you see? now we can exploit the pipe, join a few other cmdlets, and do stuff like this:

PS > Get-ChildItem C:\2send\4Max | Select-Object FullName, Extension, Length | Where-Object {$_.Length -gt 1000}

or in a much simpler form

PS > dir C:\2send\4Max | select FullName, Extension, Length | where {$_.Length -gt 1000}

and then one can do:

PS > dir [..] | where [..] | copy [..]

and this is all very basic stuff! I bet one can do a much, much better use out of it. I now smile when I look at this post where I truly thought the dir /a /b /s > filelisting.txt was the nicest way of doing recursive filelistings :)

Next time I need to script something I will definitely try Powershell. If it turns out to be not as good as I now think it is I will make sure I blog about it. stay tuned.

No comments: