// you’re reading...

Featured

On Powershell

So Powershell is mostly awesome.  I realize I’m a little late to the game in talking about it.  Everyone seems to have gone silent on Powershell.  I wonder why?

Scripting is Awesome

I’m a huge fan.  The linux guys have known this for a while, and even the Mac folks.  The Windows folks, however, have long had a sour taste from both crappy BAT files and crappy VBScripts.

I’ve found a good scripting language for every platform I’ve worked on.

On Java it was Groovy.  I love Groovy.  It is dynamically typed, integrated with Java (making it mega-powerfull) compiles to Java Byte-Code.  I did some really cool stuff using Groovy to access APIs of big 3rd party Java applications like our document management system.

When I moved to .NET, I found Boo.  I really didn’t think there was hope of finding a “Groovy” for .NET, but it turns out the Boo is even better.  I don’t say that lightly. I should write a few posts about Boo.  The whole world is overlooking what a gem it is.  It’s a strongly typed language that looks and behaves like a dynamic one.  Boo looks like Python but has code-completion.  Digest that.  The language itself is also extensible, making it ideally suited for writing DSLs (although I’ve never actually had the need to do it).  The code-completion bit is really nice, especially when working with 3rd party libraries, which is what most programming, and especially scripting, is about.

Boo is great for working with applications, but not so much for working with files or doing the “traditional” type of scripting that, say, a sysadmin would do.  It does have an interactive console, but it doesn’t provide any good way to call DOS functions, etc.  Also Boo doesn’t have the pipelining concept, which I’ll talk more about later.

That brings us to PowerShell, which I was initially very skeptical about.  But over time, I’ve found it to be an exceptionally useful tool, and I’ve got a pretty advanced little powershell environment that I’ve stored in an SVN repository so that I can easily install it on any machine I need.  It consists PSCX (Powershell Community Extensions), several functions / filters that I’ve written, along with a bunch of aliases, and now I can’t live without it.  If I get some time to clean it up, maybe I’ll open-source it.

Anyway, I really like PowerShell, and so should you.

Awesomeness: It’s .NET

Powershell is a real programming language.  (Albeit, an ugly one)  Powershell can be compiled to DLLs and DLLs can be decompiled / reflected into Powershell syntax.

Under the covers, Powershell commands are running on the CLR, and that means that you can do some really cool stuff with Powershell.  Anything, really.  It’s an industrial-strength scripting language.

It’s object-oriented and nearly infinitely extensible, except for only running on Windows :(

Awesomeness: Pipelining and Weaving

Pipelines will change your life once you really understand them.  I don’t have time to provide a really great example now, but I’ll try to later.

Ok so I just made up the term Weaving.  Pipelining makes you think that everything moves from one pipe to the next, but what is really cool about Powershell is the way you can enumerate through a list of items and push the first item all the way through the pipeline rather than wait for each item to be processed.  It’s the concept of C#’s yield return taken to a whole new level.  It also means that you can easily insert things like diagnostic log messages, progress bars, and error handling into a whole pipeline of commands such that they perform very well, keep the user informed (no more “get a cup of coffee and see if anything’s happened”), and recover from errors just like you want them to.

Awesomeness: Filters

I really love filters.  I don’t see a bunch of people using them, but I always try to write a filter rather than a function, because filters are simple mechanisms for creating new pipelines.   Ah filters.  Soo good.

Awesomeness: PowerShell Community Extensions

Check them out.  It will make you appreciate PowerShell more.  So much cool stuff.

Not Awesome: Powershell Syntax

Some things are not awesome, however

The powershell language itself kinda sucks.  Method invocation syntax (use parentheses with commas between arguments vs no parentheses with spaces between arguments) is annoying to debug sometimes and reminds me of VB script.  Blech.

Not Awesome: Class-Loading

Class-loading kinda sucks too.  Powershell needs an easy mechanism for importing DLL files so that you can use it to Get Stuff Done.  I mean, this is really what I love about PowerShell, but sometimes I find it extremely challenging to get stuff done in PowerShell.  A few people have written “import” and “using” functions for powershell and from them I sort of cobbled together my own version.  But even after all that, I can’t get the classloader to load NHibernate, which means that I can’t use PowerShell to script stuff in most of my applications, which makes it useless.  It breaks my heart.  So I still use Boo for that stuff, and Boo handles dynamic loading of DLLs very well, and has a very friendly syntax for doing so.

Not Awesome: File Handling

Well, let’s just say it could be better.

One would think that PowerShell is ideally suited to doing a bulk regex find/replace on all the files in a directory.  Unfortunately it’s not that great at it.  Part of the problem is locks on files, and then there’s also encoding issues (ASCII vs UTF-8, etc).  I finally came up with a somewhat reusable solution (which I should probably publish), but it’s not bulletproof.

Not Awesome: Platform Lock-in

Finally, Powershell is Windows only, and that sucks.

Discussion

One comment for “On Powershell”

  1. Conclusion: does not worth the effort to learn.
    Go for real scripting languages which are truly portable, like python, ruby etc :)

    Posted by Vlad | July 22, 2011, 1:09 pm

Post a comment