Scripting on Windows with PowerShell

Posted by JD 09/12/2010 at 21:20

For the last few years, I’ve heard powershell for scripting by IT professionals in the MS-Windows community. Never looked at it until a few days ago. See, I confused cscript with PowerShell scripting. I couldn’t get the hang of cscript and wasn’t able to get it to accomplish what I needed the last time I tried. In June, I actually wrote some .BAT scripts for a client. If I knew then what I know now about PowerShell, I’d have been much happier and written those scripts in PowerShell, assuming it is pre-installed on Win2003 Server.

In my limited time with PowerShell, here are my impressions.

  • UNIX has text processing, MS-Windows uses object processing – sorta like the difference between PERL and Ruby scripting.
  • Most of the syntax is Perl-like – with some differences. Get used to $var.action calls.
  • Pipes work like you’d expect from UNIX, except ….
    • generally, we don’t use `grep`, rather, the regex is used as part of the prior command
    • | `wc -l` becomes action.count – basically, if you want to get a count of something.
    • functions are Perl/Bash-like, except the argument passing is C-like (int x, int y). That is better than Perl, IMHO.
  • Objects mean access to the registry, COM, and WMI. That’s a plus AND a minus. How to query a GUI program? That’s the problem.
  • As usual, rather than reuse existing commands, Microsoft decided to use, longer, more complex commands. `ls` is `get-selections`, but they did include aliases in the tool. That means that many of the normal UNIX commands used in shell scripts have aliases, but those are not the preferred method name to be used.

As long as you don’t need to press a button inside a GUI, PowerShell can probably be useful.

Mindset Change

To me, the main limitations of PowerShell really aren’t with the language, rather, it is with my mindset and having to change from UNIX/File processing to object processing. Also, the tools on UNIX/Linux systems tend to support command line options to get things done. In MS-Windows, command line options to GUI programs are usually extremely limited – next to worthless. In Linux/UNIX, there are hundreds and hundreds of small applications and tools that come with the system suitable for script use.

References

A few years ago, PowerShell was called msh, Microsoft Command Shell. Here’s an overview.

Here’s a PowerShell Tutorial if you are interested in going further.

Read where someone is porting PowerShell to UNIX/Linux systems. Yep, it is called Pash. According to the project page, it is 40-50% complete and running on Windows, Linux, Mac and Windows Mobile platforms.

VMware is using PowerShell (they call it PowerCLI) as an interface into their ESX virtualization system.

Summary

In summary, it seems that Microsoft has been working hard for years to remove some of the biggest complaints against their systems – the lack of real, usable scripting. Seems they could advertise it a little more widely. Personally, I’d prefer a Perl library that encapsulated all this and for Perl to be included with MS-Windows installations so I don’t need to learn yet another language. Still, msh/psh/pash or whatever they call it this year is a step in the right direction.

Trackbacks

Use the following link to trackback from your own site:
https://blog.jdpfu.com/trackbacks?article_id=792