Friday, March 30, 2007

Windows Powershell in Action

Windows Powershell in Action

Bruce Payette

February, 2007
576 pages
ISBN: 1932394-90-7

about this book

Windows PowerShell is the next-generation shell and scripting environment created by Microsoft. It’s intended to fix the weaknesses in existing Windows command-line and scripting solutions. The driving force behind its creation was the need to address the problems in automating Windows system management. Windows lacked the ad hoc management capabilities found in many competing systems. With PowerShell’s comprehensive support for .NET, it now has broad application potential beyond the system administration space. PowerShell can be used for text processing, general scripting, build management, creating test frameworks, and so on.

This book is written by one of principal creators of PowerShell. It uses many examples, both small and large, to illustrate the features of the language and its environment and shows how to compose those features into solutions, quickly and effectively.

Because of the broad focus of the PowerShell product, the book has a commensurately broad focus. It was not designed as a cookbook of preconstructed management examples, such as how to deal with Active Directory or how to script Exchange. Instead, it provides information about the core of the PowerShell runtime and how to compose solutions in the “PowerShell Way.” After reading this book, the reader should be able to take any example written in other languages, such as C# or Visual Basic, and leverage those examples to build solutions in PowerShell. (To facilitate this, appendix A in the book includes a number of discussions about the differences between PowerShell and other languages.)

The other thing this book doesn’t cover is the PowerShell SDK. PowerShell is both a hostable (that is, it can be embedded in other applications) as well as an extensible environment. This book doesn’t address these topics. Instead, it focuses on the shell user and scripter. (Though we do explain how to load and use .NET assemblies from a PowerShell script in chapter 11.)

Who should read this book?
This book is designed for anyone who wants to learn PowerShell and use it well. Rather than being a book of recipes, this book tries to give the reader deep knowledge about how PowerShell works and how to apply it. All users of PowerShell will find this book beneficial and useful.

So, if you’re a Windows sysadmin, this book is for you. If you’re a developer and you need to get things done in a hurry, if you're interested in .NET, or just if you like to experiment with computers, PowerShell is for you—and this book is for you!

The book is divided into two and a half parts. The two major parts of the book are “Learning PowerShell” and “Using PowerShell,” described below. The “half” part is primarily appendix B which contains examples showing how to use PowerShell to solve system administration problems. In appendix A we provide comparisons between PowerShell and other languages. Finally, in appendix C we present the grammar for the PowerShell language.

Part 1 “Learning PowerShell” is a comprehensive tour of the PowerShell language and runtime. The goal is to introduce new PowerShell users to the language as well as to provide experienced users with deep insight into how and why things are the way they are.

In part 1, we look at all aspects of the PowerShell language, including the syntax, the type system, and so on. Along the way, we’ll present examples showing how each feature works. Since the goal of the first part of the book is to focus on the individual features of the environment, most of the examples are small and are intended to be entered in an interactive session. The second part of this book focuses on larger examples that bring the individual features together to build larger applications.

Chapter 1 begins with some history and the rationale for why PowerShell was created in the first place, followed by a quick tour of the features of the environment. The remaining chapters in part 1 cover each element of the language, starting with basic PowerShell concepts in chapter 2.

Chapter 3 introduces the PowerShell type system and its relationship to .NET. This chapter also presents the syntax for each of the PowerShell literal data types.

The discussion of operators and expressions (PowerShell has lots of these) begins in chapter 4, which covers the basic arithmetic, comparison, and assignment operators. It also covers the wildcard and regular expression pattern matching operators.

Chapter 5 continues the discussion of operators with the advanced operations for working with arrays (indexing and slicing) and objects (properties and methods). It also covers output redirection and the formatting operator, and introduces PowerShell variables.

Chapter 6 covers the PowerShell language constructs such as if statement and loops.

Chapter 7 introduces programming in PowerShell and covers functions and scripts, variable scoping, and other programming-related topics.

Chapter 8 builds on the material in chapter 7 and introduces advanced programming techniques, such as object construction and extensions. It also covers first-class functions (scriptblocks) and shows how to extend the PowerShell language using these features.

Chapter 9 completes part 1, covering the features available in PowerShell for handling errors and debugging scripts.

In part 2 of the book, we shift our focus from individual features toward combining those features into larger examples. This part of the book looks at applying PowerShell in specific technology areas and problem domains.

We begin in chapter 10, looking at how PowerShell can be used to attack the kind of text processing tasks that have been the traditional domain of languages such as Perl. This chapter begins with basic string processing, then introduces file processing (including handling binary files), and finishes up with a section on working with XML documents.

Then, in chapter 11, we look at how we can discover and apply the vast capabilities of the .NET framework from .NET. We cover locating, exploring, and instantiating types in the .NET framework, including generic types. Then we look at a number of applications using these types, including network programming and graphical programming with WinForms.

In chapter 12, we look at how to use and apply other Microsoft object technologies, specifically COM and WMI. This includes using the application automation models to script applications such as Microsoft Word using PowerShell. We look at how to use WMI from the command line and in scripts to inspect, update, and manage a Windows system. We also spend some time looking at how to interact with VBScript, Microsoft’s previous-generation scripting tool.

Finally, in chapter 13, we introduce the security features in PowerShell, along with a general discussion of security. This is an important chapter to read. Like all powerful scripting tools (Perl, Python, and so forth), PowerShell can be used to create malware such as virus and worm programs. The PowerShell runtime contains features which allow you to deploy it in a manner that minimizes these risks.

That covers two out of the two and a half parts. Since the examples in part 2 of the book, while larger, still focus on particular technology areas, we have appendix B, which presents examples where we solve some common system administration tasks using PowerShell. While it’s not a complete management cookbook, it does show what can be done with PowerShell and how to do it.

In appendix A we present comparisons of PowerShell with other programming or scripting languages, including cmd.exe, UNIX shells and VBScript. This appendix includes tips for experienced users and highlights some potential problems users of other languages may run into with PowerShell. Finally, appendix C contains the annotated grammar and tokenization rules for the PowerShell Language along with syntax examples.

About the author
Bruce Payette is one of the founding members of the Windows PowerShell team. He is co-designer of the PowerShell language along with Jim Truher, and is the principal author of the language implementation. He joined Microsoft in 2001, working on Interix—the POSIX subsystem for Windows—and then moved to help found the PowerShell project shortly thereafter. Prior to joining Microsoft, he worked at various companies including Softway (the creators of Interix) and MKS (producers of the MKS Toolkit) building UNIX tools for Windows. He lives in Bellevue, Washington, with his wife, many computers, and three extremely over-bonded and very spoiled cats.





About this book

Part 1 Learning PowerShell

1 Welcome to PowerShell

1.1 What is PowerShell?

1.2 Soul of a new language

1.3 Brushing up on objects

1.4 Dude! Where’s my code?

1.5 Summary

2 The basics

2.1 Command concepts and terminology

2.2 Parsing and PowerShell

2.3 Pipelines and commands

2.4 Formatting and output

2.5 Summary

3 Working with types

3.1 Type management in the wild, wild west

3.2 Basic types and literals

3.3 Type conversions

3.4 Summary

4 Operators and expressions

4.1 Arithmetic operators

4.2 The assignment operators

4.3 Comparison operators

4.4 The pattern matching operators

4.5 Logical and bitwise operators

4.6 Summary

5 Advanced operators and variables

5.1 Operators for working with types

5.2 The unary operators

5.3 Grouping, subexpressions, and array subexpressions

5.4 Array operators

5.5 Property and method operators

5.6 The PowerShell format operator -F

5.7 Redirection and the redirection operators

5.8 Variables

5.9 Summary

6 Flow control in scripts

6.1 Using the if/elseif/else statement

6.2 The while loop

6.3 The do/while loop

6.4 The for loop

6.5 The foreach loop

6.6 Labels, break, and continue

6.7 The PowerShell switch statement

6.8 Flow control using cmdlets

6.9 The value of statements

6.10 Summary

7 Functions and scripts

7.1 Function basics

7.2 Formal parameters and the param statement

7.3 Returning values from functions

7.4 Using functions in a pipeline

7.5 Managing functions

7.6 Scripts at long last

7.7 Summary

8 Scriptblocks and objects

8.1 Scriptblock basics

8.2 Building and manipulating objects

8.3 A closer look at the type-system plumbing

8.4 Extending the PowerShell language

8.5 Type extension

8.6 Building code at runtime

8.7 Summary

9 Errors, exceptions, and script debugging

9.1 Error handling

9.2 Dealing with errors that terminate execution

9.3 Script debugging

9.4 Nested prompts and breakpoints

9.5 Low-level tracing

9.6 The PowerShell event log

9.7 Summary

Part 2 Using PowerShell

10 Processing text, files, and XML

10.1 Processing unstructured text

10.2 File processing

10.3 XML processing

10.4 Summary

11 Getting fancy—.NET and WinForms

11.1 Using .NET from PowerShell

11.2 PowerShell and the Internet

11.3 PowerShell and graphical user interfaces

11.4 Summary

12 Windows objects: COM and WMI

12.1 Working with COM in PowerShell

12.2 Working with WMI in PowerShell

12.3 So which object model should I choose?

12.4 Summary

13 Security, security, security

13.1 Introduction to security

13.2 Security modeling

13.3 Securing the PowerShell environment

13.4 Signing scripts

13.5 Writing secure scripts

13.6 Summary

Comparing PowerShell to other languages

Admin examples

The PowerShell grammar


Sample Chapters

Amazon Link: Windows Powershell in Action

No comments: