Windows Server 2008 R2 Unleashed (141 page)

702

CHAPTER 21

Automating Tasks Using PowerShell Scripting

Introduction to PowerShell

The introduction of WSH as a standard in the Windows operating system offered a robust

alternative to DOSShell scripting. Unfortunately, WSH presented a number of challenges,

discussed in the preceding section. Furthermore, WSH didn’t offer the CLI shell experience

that UNIX and Linux administrators had enjoyed for years, resulting in Windows adminis-

trators being made fun of by the other chaps for the lack of a CLI shell and its benefits.

Luckily, Jeffrey Snover (the architect of PowerShell) and others on the PowerShell team

realized that Windows needed a strong, secure, and robust CLI shell for systems manage-

ment. Enter PowerShell. PowerShell was designed as a shell with full access to the under-

pinnings of Windows via the .NET Framework, Component Object Model (COM) objects,

and other methods. It also provided an execution environment that’s familiar, easy, and

secure. PowerShell is aptly named, as it puts the power into the Windows shell. For users

wanting to automate their Windows systems, the introduction of PowerShell was exciting

because it combined “the power of WSH with the warm-fuzzy familiarity of a CLI shell.”

PowerShell provides a powerful native scripting language, so scripts can be ported to all

Windows systems without worrying about whether a particular language interpreter is

installed. In the past, an administrator might have gone through the rigmarole of script-

ing a solution with WSH in Perl, Python, VBScript, JScript, or another language, only to

ptg

find that the next system that they worked on didn’t have that interpreter installed. At

home, users can put whatever they want on their systems and maintain them however

they see fit, but in a workplace, that option isn’t always viable. PowerShell solves that

problem by removing the need for nonnative interpreters. It also solves the problem of

wading through websites to find command-line equivalents for simple GUI shell opera-

tions and coding them into .cmd files. Last, PowerShell addresses the WSH security

problem by providing a platform for secure Windows scripting. It focuses on security

features such as script signing, lack of executable extensions, and execution policies

(which are restricted by default).

For anyone who needs to automate administration tasks on a Windows system or a

Microsoft platform, PowerShell provides a much-needed injection of power. As such, for

Windows systems administrators or scripters, becoming a PowerShell expert is highly

recommended. After all, PowerShell can now be used to efficiently automate management

tasks for Windows, Active Directory, Terminal Services, SQL Server, Exchange Server,

Internet Information Services (IIS), and even a number of different third-party products.

As such, PowerShell is the approach Microsoft had been seeking as the automation and

management interface for their products. Thus, PowerShell is now the endorsed solution

for the management of Windows-based systems and server products. Over time, PowerShell

could even possibly replace the current management interfaces, such as cmd.exe, WSH, CLI

tools, and so on, while becoming even further integrated into the Windows operating

system. The trend toward this direction can be seen with the release of Windows Server

2008 R2 and Windows 7, in which PowerShell is part of the operating system.

Introduction to PowerShell

703

PowerShell Uses

21

In Windows, an administrator can complete a number of tasks using PowerShell. The

following list is a sampling of these tasks:

.
Manage the file system—
To create, delete, modify, and set permissions for files

and folders.

.
Manage services—
To list, stop, start, restart, and even modify services.

.
Manage processes—
To list (monitor), stop, and start processes.

.
Manage the Registry—
To list (monitor), stop, and start processes.

.
Use Windows Management Instrumentation (WMI)—
To manage not only

Windows, but also other platforms such as IIS and Terminal Services.

.
Use existing Component Object Model (COM) objects—
To complete a wide

range of automation tasks.

.
Manage a number of Windows roles and features—
To add or remove roles and

features.

ptg

PowerShell Features

PowerShell is a departure from the current management interfaces in Windows. As such, it

has been built from the ground up to include a number of features that make CLI and

script-based administration easier. Some of PowerShell’s more key features are as follows:

. It has 240 built-in command-line tools (referred to as cmdlets).

. The scripting language is designed to be readable and easy to use.

. PowerShell supports existing scripts, command-line tools, and automation interfaces,

such as WMI, ADSI, .NET Framework, ActiveX Data Objects (ADO), and so on.

. It follows a strict naming convention for commands based on a verb-noun format.

. It supports a number of different Windows operating systems: Windows XP SP2 or

later, Windows Server 2003 SP1 or later, Windows Vista, Windows Server 2008, and

now Windows Server 2008 R2 and Windows 7.

. It provides direct “access to and navigation of” the Windows Registry, certificate

store, and file system using a common set of commands.

. PowerShell is object based, which allows data (objects) to be piped between

commands.

. It is extensible, which allows third parties (as noted earlier) to build upon and

extend PowerShell’s already rich interfaces for managing Windows and other

Microsoft platforms.

704

CHAPTER 21

Automating Tasks Using PowerShell Scripting

PowerShell 2.0 Enhancements

Windows Server 2008 R2 has the Windows PowerShell 2.0 version built in to the operating

system. In this version of PowerShell, a number of enhancements have been made to both

PowerShell itself and the ability for managing Windows Server 2008 R2’s roles and features.

The following is a summary for some of the improvements in PowerShell 2.0 (these

features are talked about in greater detail later in this chapter and throughout this book):

. The number of built-in cmdlets has nearly doubled from 130 to 240.

. PowerShell 2.0 now includes the ability to manage a number of roles and features

such as the Active Directory Domain Services, Active Directory Rights Management

Services, AppLocker, Background Intelligent Transfer Service [BITS], Best Practices

Analyzer, Failover Clustering [WSFC], Group Policy, Internet Information Services

[IIS], Network Load Balancing [NLB], Remote Desktop Services [RDS], Server

Manager, Server Migration, and Windows Diagnostics roles and features.

. PowerShell 2.0 also includes the introduction of the Windows PowerShell debugger.

Using this feature, an administrator can identify errors or inefficiencies in scripts,

functions, commands, and expressions while they are being executed through a set

of debugging cmdlets or the Integrated Scripting Environment (ISE).

ptg

. The PowerShell Integrated Scripting Environment (ISE) is a multi-tabbed GUI-based

PowerShell development interface. Using the ISE, an administrator can write, test,

and debug scripts. The ISE includes such features as multiline editing, tab comple-

tion, syntax coloring, selective execution, context-sensitive help, and support for

right-to-left languages.

. Background jobs enable administrators to execute commands and scripts asynchro-

nously.

. Also through the inclusion of script functions, administrators can now create their

own cmdlets without having to write and compile the cmdlet using a managed-code

language like C#.

. PowerShell 2.0 also includes a new powerful feature, called modules, which allows

packages of cmdlets, providers, functions, variables, and aliases to be bundled and

then easily shared with others.

. The lack of remote command support has also been addressed in PowerShell 2.0

with the introduction of remoting. This feature enables an administrator to auto-

mate the management of many remote systems through a single PowerShell console.

However, with all of these features, the most important advancement that is found in

PowerShell 2.0 is the focus on what is called the Universal Code Execution model. The

core concept in this model is flexibility over how expressions, commands, and script-

blocks are executed across one or more machines.

Understanding the PowerShell Basics

705

Understanding the PowerShell Basics

21

To begin working with PowerShell, some of the basics like accessing PowerShell, working

from the command-line interface, and understanding the basic commands are covered in

this section of the book.

Accessing PowerShell

After logging in to your Windows interactive session, there are several methods to access and

use PowerShell. The first method is from the Start menu, as shown in the following steps:

1. Click Start, All Programs, Accessories, Windows PowerShell.

2. Choose either Windows PowerShell (x86) or Windows PowerShell.

To use the second method, follow these steps:

1. Click Start.

2. Type PowerShell in the Search Programs and Files text box and press Enter.

Both these methods open the PowerShell console, whereas the third method launches

PowerShell from a cmd command prompt:

ptg

1. Click Start, Run.

2. Type cmd and click OK to open a cmd command prompt.

3. At the command prompt, type powershell and press Enter.

Command-Line Interface (CLI)

The syntax for using PowerShell from the CLI is similar to the syntax for other CLI shells.

The fundamental component of a PowerShell command is, of course, the name of the

command to be executed. In addition, the command can be made more specific by using

parameters and arguments for parameters. Therefore, a PowerShell command can have the

following formats:

. [command name]

. [command name] -[parameter]

. [command name] -[parameter] -[parameter] [argument1]

. [command name] -[parameter] -[parameter] [argument1],[argument2]

When using PowerShell, a parameter is a variable that can be accepted by a command,

script, or function. An argument is a value assigned to a parameter. Although these terms

are often used interchangeably, remembering these definitions is helpful when discussing

their use in PowerShell.

706

CHAPTER 21

Automating Tasks Using PowerShell Scripting

Navigating the CLI

As with all CLI-based shells, an understanding is needed in how to effectively navigate

and use the PowerShell CLI. Table 21.1 lists the editing operations associated with various

keys when using the PowerShell console.

TABLE 21.1

PowerShell Console Editing Features

Keys

Editing Operation

Left and right

Move the cursor left and right through the current command line.

arrows

Up and down

Moves up and down through the list of recently typed commands.

arrows

PgUp

Displays the first command in the command history.

PgDn

Displays the last command in the command history.

Home

Moves the cursor to the beginning of the command line.

End

Moves the cursor to the end of the command line.

ptg

Insert

Switches between insert and overstrike text-entry modes.

Delete

Deletes the character at the current cursor position.

Backspace

Deletes the character immediately preceding the current cursor position.

F3

Displays the previous command.

F4

Deletes up to the specified number of characters from the current cursor.

F5

Moves backward through the command history.

F7

Displays a list of recently typed commands in a pop-up window in the

command shell. Use the up and down arrows to select a previously typed

command, and then press Enter to execute the selected command.

F8

Moves backward through the command history with commands that match

the text that has been entered at the command prompt.

F9

Prompts for a command number and executes the specified command from

the command history (command numbers refer to the F7 command list).

Tab

Auto-completes command-line sequences. Use the Shift+Tab sequence to

move backward through a list of potential matches.

Luckily, most of the features in Table 21.1 are native to the cmd command prompt, which

makes PowerShell adoption easier for administrators already familiar with the Windows

command line. The only major difference is that the Tab key auto-completion is enhanced

in PowerShell beyond what’s available with the cmd command prompt.

Understanding the PowerShell Basics

707

As with the cmd command prompt, PowerShell performs auto-completion for file and

directory names. So, if you enter a partial file or directory name and press Tab, PowerShell

21

returns the first matching file or directory name in the current directory. Pressing Tab

Other books

The No Cry Discipline Solution by Elizabeth Pantley
Flesh 02 Skin by Kylie Scott
A Prior Engagement by Scott, S. L.
Fifty Fifty by S. L. Powell
Johnny Cash: The Life by Hilburn, Robert
Relatos 1913-1927 by Bertolt Brecht
The Book of the Dead by Carriger, Gail, Cornell, Paul, Hill, Will, Headley, Maria Dahvana, Bullington, Jesse, Tanzer, Molly
Wilde Fire by Kat Austen