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.

Back from Microsoft Windows7 Launch

Posted by JohnP 09/28/2009 at 14:10

I just got back from the Microsoft Windows7 Launch in Atlanta for developers – the IT Professional track was full.
There wasn’t much new to see since there were only 2 tracks for developers (8am-noon) and I decided to learn more about MS-Server 2008-R2, 08R2

Session 1 What’s New in Server 2008 R2 – Murray Gordon

The main things covered that interested me about 08R2

  1. Upto 256 CPUs supported
  2. Only 64-bit (32-bit Server OS is dead)
  3. Win7 and 08R2 share the same core OS
  4. IIS 7.5 is included
  5. PowerShell with remote execution
  6. VHD file support included
  7. Virtualization is built-in, not an add-on
  8. Boot from an OS install or a VHD (this is nice)
  9. During installation, you decide how much of the Server OS you want even to the point of ZERO GUI installs

Session 2 Parallel Programming – Glenn Gordon

Glenn talked about the differences in sequential, threaded, and parallel/task programming.

  1. Win7 and 08R2 are needed for the new “Task” APIs
  2. VisualStudio 2010 includes .NET v4.0 with parallel extensions
  3. It is unclear on any backward compatibility to prior OSes with .NET 4
  4. Parallel Extensions make it easier to use whatever CPUs, Cores, hyper-threading an OS can support.
  5. There are 3 parallel programming models – I didn’t bother writing them down since the programming examples seemed contrived and didn’t include any concern about data homogeneity. Scary.

Were I windows-only developer, they certainly made using multi-core systems much easier. The downside appears to be that only Win7+ and 08R2+ can use these extensions. I could be wrong. Let me know if there will be backward compatible solutions.

Session 3 IIS 7.x Features – Glenn Gordon

Basically, IIS 7.5 is a modular system (gee, like apache?) now and you can place your code almost anywhere in the pipeline, extending IIS with your code. He went on and on about some trivial examples that placed copyright notices into JPG files as the file was being served. You could, for example, tag every image with the authenticated userid, website and timestamp without touching the source image files.

The thing that really seemed neato to me was that MS finally is building a web application easy-installation community that makes it trivial to package solutions, deploy them or simply publish them for others to use. http://www.microsoft.com/web/ is the community portal and Web Platform Installer v2 is the enabler. It wasn’t clear whether these tools worked with any IIS older than 08R2 or not. The packager creates a ZIP with everything needed from the development machine and is a 1 file package provided to the IT production support guys for deployment. It modifies those settings that are different between developer and production servers during installation. Nice.

To make a good point, Glenn pulled both a DasBlog engine and MediaWiki from the community and deployed both to his server in real-time. The blog engine came right up and was ready for blogging. I didn’t see whether MediaWiki worked as easily – I run a mediawiki for the company. There were many well know open source projects in the list.

Of course, there were the normal give aways – Win7 Ultimate license and all the 180 day server stuff you can stand. the 180 day stuff you are free to download anyway.

Nice job from Microsoft and the Gordons who presented. I wish I could have attended the afternoon sessions that were more about enterprise deployments.