Sunday, June 26, 2005

More on WMI Scripting With MSH

In my last blog post, I got quite excited about the ability to do WMI scripting from MSH. That post generated a lot of feedback - Mark Allen's post was, as ever on the money. No, I was not trying to say short is better because it's short. APL proved that readability was more important than pure brevity. But what impressed me about WMI scripting using MSH was that it is so easy and obvious when compared to VBScript. I can see that part of my message was lost when the mail to blog translation happened - the script examples looked a lot better in my mail than they did when first rendered on this site. I've tidied up the layout a bit on that post to make the intention clearer.

I also got mail from the MSH development team pointing out I could have made the scripts shorter still, They are, of course, right! The two scripts I posted were meant to show a straightforward comparision between WMI scripts on the Script Centre, and ones you could easily write with MSH. I was trying to show MSH scripts were shorter, simpler to understand and faster/easier to develop.

Of course if you want terseness, the following script prints out the logged on user, all in one one line (well one line when I typed it!):

foreach ($comp in Get-WmiObject Win32_ComputerSystem -comp "kapoho61") {"Logged-on user: $($comp.UserName)"}

Or to simplify every further, here's an even simpler "script":

$(get-WmiObject Win32_ComputerSystem -comp "kapoho61").UserName

So to answer Mark Allen - no, I'm not advocating shortness just for the sake of it. For any production script, you have to have readibilty (and comments) and adopt a simple to maintain style. But when it's a simple as the examples here and before, you can see how much easier MSH can make both scripting WMI and scripting in general. What continues to amaze me about MSH is that there are so many ways to achieve the same end - and just about all of them shorter, and simpler than VBScript.

1 comment:

Mark-Allen said...


I sit corrected.

The new examples look very interesting and have the possibility to be easily understood. My team member, Andrew, and I are beginning to learn MSH and hope to make some intelligent posts in the future.

Keep up the great work. We're reading you every day.