Professional Windows PowerShell Programming

Snapins, Cmdlets, Hosts and Providers
By Arul Kumaravel Jon White Michael Naixin Li Scott Happell Guohui Xie Krishna C. Vutukuri

John Wiley & Sons

Copyright © 2008 Arul Kumaravel
All right reserved.

ISBN: 978-0-470-17393-0


Chapter One

Introduction to PowerShell

Welcome to Windows PowerShell, the new object-based command-line interface shell and scripting language built on top of .NET. PowerShell provides improved control and automation of IT administration tasks for the Windows platform. It is designed to make IT professionals and developers more productive.

Several books that introduce end-user IT professionals to Windows PowerShell are already available, but PowerShell development from the perspective of cmdlet, provider, and host developers has gone largely unmentioned. This book attempts to fill that gap by introducing the reader to the concepts, components, and development techniques behind building software packages that leverage Windows PowerShell. This book is written for developers who want to extend the functionality of Windows PowerShell and extend their applications using PowerShell.

Traditionally, when developers write a command-line utility, they have to write code for parsing the parameters, binding the argument values to parameters during runtime. In addition, they have to write code for formatting the output generated by the command. Windows PowerShell makes that easy by providing a runtime engine with its own parser. It also provides functionality that enables developers to add custom formatting when their objects are displayed. By performing the common tasks associated with writing command-line utilities, Windows PowerShell enables developers to focus on the business logic of their application, rather than spend development time solving universal problems.

Windows PowerShell Design Principles

Windows PowerShell was designed in response to years of customer feedback about the administrative experience on Microsoft Windows. Early on, many users asked why some of the traditional Unix shells weren't licensed and included in Windows, and it became apparent that the right answer was to produce a whole new kind of shell that would leave these legacy technologies behind. This thinking was distilled into four guiding principles that provided the foundation for PowerShell's design effort.

Preserve the Customer's Existing Investment

When a new technology is rolled out, it takes time for the technology to be adopted. Moreover, customers are likely to have already invested a lot in existing technologies. It's unreasonable to expect people to throw out their existing investments, which is why PowerShell was designed from the ground up to be compatible with existing Windows Management technologies.

In fact, PowerShell runs existing commands and scripts seamlessly. You can make use of PowerShell's integration with COM, WMI, and ADSI technologies alongside its tight integration with .NET. Indeed, PowerShell is the only technology that enables you to create and work with objects from these various technologies in one environment. You can see examples of this and other design principles in a quick tour of PowerShell later in the chapter.

Provide a Powerful, Object-Oriented Shell

CMD.exe and other traditional shells are text-based, meaning that the commands in these shells take text as input and produce text as output. Even if these commands convert the text internally into objects, when they produce output they convert it back to text. In traditional shells, when you want to put together simple commands in the pipeline, a lot of text processing is done between commands to produce desired output. Tools such as SED, AWK, and Perl became popular among command-line scripters because of their powerful text-processing capabilities.

PowerShell is built on top of .NET and is an object-based shell and scripting language. When you pipe commands, PowerShell passes objects between commands in the pipeline. This enables objects to be manipulated directly and to be passed to other tools. PowerShell's tight integration with .NET brings the functionality and consistency of .NET to IT professionals without requiring them to master a high-level programming language such as C# or VB.NET.

Extensibility, Extensibility, Extensibility

This design principle aims to make the IT administrator more productive by providing greater control over the Windows environment and accelerating the automation of system administration. Administrators can start PowerShell and use it immediately without having to learn anything because it runs existing commands and scripts, and is therefore easy to adopt. It is an easy to use shell and language for administrators.

All commands in PowerShell are called cmdlets (pronounced "commandlet"), and they use verb-noun syntax - for example, Start-Service, Stop-Service or Get-Process, Get-WMIObject, and so on. The intuitive nature of verb-noun syntax makes learning commands easy for administrators. PowerShell includes more than 100 commands and utilities that are admin focused. In addition, PowerShell provides a powerful scripting language that supports a wide range of scripting styles, from simple to sophisticated. This enables administrators to write simple scripts and learn the language as they go. With this combined functionality and ease of use, PowerShell provides a powerful environment for administrators to perform their daily tasks.

Tear Down the Barriers to Development

Another design principle of PowerShell is to make it easy for developers to create command-line tools and utilities. It provides common argument parsing code, parameter binding code that enables developers to write code only for the admin functionality they are providing. The PowerShell development model separates the processing of objects from formatting and outputting. PowerShell provides a set of cmdlets for manipulating objects, formatting objects, and outputting objects. This eliminates the need for developers to write this code. PowerShell leverages the power of .NET, which enables develop- ers to take advantage of the vast library of this framework. It provides common functionality for logging, error handling, and debugging and tracing capabilities.

A Quick Tour of Windows PowerShell

This section presents a quick tour of Windows PowerShell. We'll start with a brief look at installing the program, and then move right into a discussion of cmdlets.

You start Windows PowerShell either by clicking the Windows PowerShell shortcut link or by typing PowerShell in the Run dialog box (see Figure 1-1).

Cmdlets

Windows PowerShell enables access to several types of commands, including functions, filters, scripts, aliases, cmdlets, and executables (applications). PowerShell's native command type is the cmdlet. A cmdlet is a simple command used for interacting with any management entity, including the operating system. You can think of a cmdlet as equivalent to a built-in command in another shell. The traditional shell generally processes commands as separate executables, but a cmdlet is an instance of a .NET class, and runs within PowerShell's process.

Windows PowerShell provides a rich set of cmdlets, including several that enhance the discoverability of the shell's features. We begin our tour of Windows PowerShell by learning about a few cmdlets that will help you get started in this environment. The first cmdlet you need to know about is get-help:

PS C:\> get-help TOPIC Get-Help

SHORT DESCRIPTION Displays help about PowerShell cmdlets and concepts.

LONG DESCRIPTION

SYNTAX get-help { | } help { | } -?

"Get-help" and "-?" display help on one page. "Help" displays help on multiple pages.

Examples: get-help get-process : Displays help about the get-process cmdlet. get-help about-signing : Displays help about the signing concept. help where-object : Displays help about the where-object cmdlet. help about_foreach : Displays help about foreach loops in PowerShell. match-string -? : Displays help about the match-string cmdlet.

You can use wildcard characters in the help commands (not with -?). If multiple help topics match, PowerShell displays a list of matching topics. If only one help topic matches, PowerShell displays the topic.

Examples: get-help * : Displays all help topics. get-help get-* : Displays topics that begin with get-. help *object* : Displays topics with "object" in the name. get-help about* : Displays all conceptual topics.

For information about wildcards, type: get-help about_wildcard

REMARKS To learn about PowerShell, read the following help topics: get-command : Displays a list of cmdlets. about_object : Explains the use of objects in PowerShell. get-member : Displays the properties of an object.

Conceptual help files are named "about_", such as: about_regular_expression.

The help commands also display the aliases on the system. For information about aliases, type:

get-help about_alias

PS C:\>

As you can see, get-help provides information about how to get help on PowerShell cmdlets and concepts. This is all well and good, but you also need to be able to determine what commands are available for use. The get-command cmdlet helps you with that:

PS C:\> get-command

CommandType Name Definition --- - --- Cmdlet Add-Content Add-Content [-P Cmdlet Add-History Add-History [[- Cmdlet Add-Member Add-Member [-Me Cmdlet Add-PSSnapin Add-PSSnapin [- Cmdlet Clear-Content Clear-Content [ Cmdlet Clear-Item Clear-Item [-Pa Cmdlet Clear-ItemProperty Clear-ItemPrope Cmdlet Clear-Variable Clear-Variable Cmdlet Compare-Object Compare-Object Cmdlet ConvertFrom-SecureString ConvertFrom-Sec Cmdlet Convert-Path Convert-Path [- Cmdlet ConvertTo-Html ConvertTo-Html Cmdlet ConvertTo-SecureString ConvertTo-Secur Cmdlet Copy-Item Copy-Item [-Pat

...

As shown in the preceding output, get-command returns all the available commands. You can also find cmdlets with a specific verb or noun:

PS C:\> get-command -verb get

CommandType Name Definition --- - --- Cmdlet Get-Acl Get-Acl [[-Path] Cmdlet Get-Alias Get-Alias [[-Nam Cmdlet Get-AuthenticodeSignature Get-Authenticode Cmdlet Get-ChildItem Get-ChildItem [[ Cmdlet Get-Command Get-Command [[-A Cmdlet Get-Content Get-Content [-Pa Cmdlet Get-Credential Get-Credential [ Cmdlet Get-Culture Get-Culture [-Ve Cmdlet Get-Date Get-Date [[-Date Cmdlet Get-EventLog Get-EventLog [-L Cmdlet Get-ExecutionPolicy Get-ExecutionPol Cmdlet Get-Help Get-Help [[-Name Cmdlet Get-History Get-History [[-I Cmdlet Get-Host Get-Host [-Verbo Cmdlet Get-Item Get-Item [-Path] Cmdlet Get-ItemProperty Get-ItemProperty Cmdlet Get-Location Get-Location [-P Cmdlet Get-Member Get-Member [[-Na Cmdlet Get-PfxCertificate Get-PfxCertifica Cmdlet Get-Process Get-Process [[-N Cmdlet Get-PSDrive Get-PSDrive [[-N Cmdlet Get-PSProvider Get-PSProvider [ Cmdlet Get-PSSnapin Get-PSSnapin [[- Cmdlet Get-Runspace Get-Runspace [[- Cmdlet Get-Service Get-Service [[-N Cmdlet Get-TraceSource Get-TraceSource Cmdlet Get-UICulture Get-UICulture [- Cmdlet Get-Unique Get-Unique [-Inp Cmdlet Get-Variable Get-Variable [[- Cmdlet Get-WmiObject Get-WmiObject [-

(Continues...)



Excerpted from Professional Windows PowerShell Programming by Arul Kumaravel Jon White Michael Naixin Li Scott Happell Guohui Xie Krishna C. Vutukuri Copyright © 2008 by Arul Kumaravel. Excerpted by permission.
All rights reserved. No part of this excerpt may be reproduced or reprinted without permission in writing from the publisher.
Excerpts are provided by Dial-A-Book Inc. solely for the personal use of visitors to this web site.