Back in the old days of DOS, we could automate a series of tasks using a “batch file”. With the advent of Windows (or, at least, since Windows 95) we’ve still had batch file processing available, but have been able to use WSH (the Windows Script Host – a widget that executes VBScript/Jscript scripts under Windows) to do much more complex tasks – even instantiating COM objects in order to do clever things like querying databases.
Now we’re in the days of the .NET Framework, however, it would be nice if we could write relatively simple scripts that can use not only COM (i.e. pre-.NET) objects but also the new stuff that .NET gives us. PowerShell is Microsoft’s answer to this technology gap, though only if you’re a user of Windows XP Service Pack 2, WS2003, or something more modern. It’ll be included in the installer for Windows Server 2008, but you can download it for XP and WS2003.
If you’re used to DOS batch files, VBScript scripts or Unix shell scripts, you’ll find this experience to useless when trying to get to grips with PowerShell. The syntax is like nothing I’ve ever come across (and I’ve used all three of the above extensively), and nor is it even anything like, say, VB.NET. This is a shame when you consider that, for instance, VBScripts under Windows were almost identical to VBScripts run under WSH (i.e. as Windows scripts) – if I’d been in Microsoft’s position I’d probably have tried to make PowerShell look at least something like one of the .NET programming languages, for the sake of usability.
This said, it’s not the hardest thing in the world to get used to. It’s just a case of learning the keywords and, most importantly, figuring out how to find your way around the help system. The reason for the latter is simple: if, like me, you’re a Visual Studio developer, you’re used to having IntelliSense – a clever little gizmo that enumerates all the possible expansions whenever you type an object name and hit the “.” key, so you don’t have to remember all the members of an object. No IntelliSense means having to type “get-help” a lot – but at least the help function is pretty comprehensive, and the 116-page user guide (which comes as an RTF and is installed when you install PowerShell). Also, as with many OS shells, you can create aliases for commands you use a lot – and in fact, most of the common ones have been done for you so they look like DOS commands. So “Get-ChildItem” has been aliased to the more familiar “DIR”, for example, and “Remove-Item” to “DEL”.
PowerShell gives you a lot of virtual concepts. For instance, as well as navigating the various storage devices connected to the system, you can navigate other “virtual” repositories in a similar way – notably the registry, the environment variable repository and the digital certificate store. There are a few restrictions on navigation, particularly in the registry, but it’s generally pretty flexible.
The bulk of what you can do in PowerShell is, of course, writing and running scripts that actually do work. You can do stuff as individual commands inside an interactive PowerShell session, but most people will want to put together whole scripts and then run them just like batch files. Of course, you can do this with PowerShell – you create a file with a “.ps1” extension (note that’s a number one, not a small L) and then invoke PowerShell with that filename. For example, let’s say we want to ping a host using a .NET System.Net.NetworkInformation.Ping object. We’ll write a script and save it in c:\network.ps1:
$myaddr =”192.168.1.1”
$myping = new-object System.Net.NetworkInformation.Ping
$myresult = $myping.send($ip)
write-host $myresult.status.tostring()
… and then run it from a CMD window or a Start->Run box by doing:
powershell c:\network.ps1
There’s a pile of other useful tools in PowerShell, too. We’ve talked about navigating the registry and the environment, but you also have commands for managing processes and Windows services, digging into system settings, working with the Windows installer, as well as printer and network control. And, of course, it has decent backward compatibility (so although you can now work with .NET objects, you’re not precluded from playing with traditional COM objects if you so wish).
PowerShell is a welcome evolution of Windows’ scripting facilities, and although it’s initially pretty cryptic to work with, you’ll find that you gradually build up your knowledge of commands, syntax and objects.
Reference:http://review.techworld.com/applications/529/microsoft-windows-powershell-10/
Retrieval Date:04/12/10
No comments:
Post a Comment