Windows PowerShell Cookbook

Windows PowerShell Cookbook

Lee Holmes O'REILLY'

Beijing ? Cambridge ? Farnham ? Koln ? Paris ? Sebastopol ? Taipei ? Tokyo

Table of Contents

Foreword Preface

Part I. Tour

A Guided Tour of Windows PowerShell

Introduction

An Interactive Shell

Structured Commands (Cmdlets)

Deep Integration of Objects "

Administrators As First-Class Users

Composable Commands

.

Techniques to Protect You from Yourself

Common Discovery Commands '

Ubiquitous Scripting

Ad Hoc Development

Bridging Technologies

Namespace Navigation Through Providers

Much, Much More

9

Part II. Fundamentals

1. The Windows PowerShell Interactive Shell

1.0 Introduction 1.1 Run Programs, Scripts, and Existing Tools 1.2 Run a PowerShell Command

xvii xxi

3

3

4

6

7

8

9

9

10

11

12

12

?

14

16

19

19 19 21

1.3 Customize Your Shell, Profile, and Prompt

22

1.4 Find a Command to Accomplish a Task

25

1.5 Get Help on a Command

26

1.6 Program: Search Help for Text

28

1.7 Invoke a PowerShell Script From Outside PowerShell

29

1.8 Program: Retain Changes to Environment Variables Set by a Batch File30

1.9 Get the System Date and Time

32

1.10 Determine the Status of the Last Command

33

1.11 Measure the Duration of a Command

34

1.12 Customize the Shell to Improve Your Productivity

35

1.13 Program: Learn Aliases for Common Commands

36

1.14 Access and Manage Your Console History

38

1.15 Store the Output of a Command into a File

39

1.16 Add Information to the End of a File

41

1.17 Record a Transcript of Your Shell Session

41

1.18 Display the Properties of an Item As a List

42

1.19 Display the Properties of an Item As a Table

42

1.20 Manage the Error Output of Commands

44

1.21 Configure Debug, Verbose, and Progress Output

45

1.22 Extend Your Shell with Additional Snapins

47

1.23 Use Console Files to Load and Save Sets of Snapins

48

2. Pipelines

*.

49

2.0 Introduction

49

2.1 Filter Items in a List or Command Output

50

2.2 Program: Simplify Most Where-Object Filters

51

2.3 Program: Interactively Filter Lists of Objects

52

2.4 Work with Each Item in a List or Command Output

54

2.5 Automate Data-Intensive Tasks

.

56

3. Variables and Objects

61

3.0 Introduction

61

3.1 Store Information in Variables

62

3.2 Access Environment Variables

63

3.3 Control Access and Scope of Variables and Other Items

65

3.4 Work with .NET Objects

67

3.5 Create an Instance of a .NET Object

71

3.6 Program: Create Instances of Generic Objects

73

3.7 Reduce Typing for Long Class Names

74

Table of Contents

3.8 Use a COM Object 3.9 Learn About Types and Objects 3.10 Get Detailed Documentation About Types and Objects 3.11 Add Custom Methods and Properties to Objects 3.12 Add Custom Methods and Properties to Types

4. Looping and Flow Control

4.0 Introduction 4.1 Make Decisions with Comparison and Logical Operators 4.2 Adjust Script Flow Using Conditional Statements 4.3 Manage Large Conditional Statements with Switches 4.4 Repeat Operations with Loops 4.5 Add a Pause or Delay

5. Strings and Unstructured Text..

5.0 Introduction

5.1 Create a String

5.2 Create a Multiline or Formatted String

5.3 Place Special Characters in a String

5.4 Insert Dynamic Information in a String '

5.5 Prevent a String from Including Dynamic Information

5.6 Place Formatted Information in a String

5.7 Search a6tring for Text or a Pattern

5.8 Replace Text in a String

5.9 Convert a String to Upper/Lowercase

5.10 Trim a String '

"

,

5.11 Format a Date for Output^ '

5.12 Program: Convert Text Streams to Objects

5.13 Generate Large Reports'and Text Streams

6. Calculations and Math

6.0 Introduction

6.1 Perform Simple Arithmetic

*

6.2 Perform Complex Arithmetic

6.3 Measure Statistical Properties of a List

6.4 Work with Numbers As Binary

6.5 Simplify Math with Administrative Constants

6.6 Convert Numbers Between Bases

76 77 78 80 82

87

87 87 89 90 91 93

95

95 95 97 98 99 100 101 102 105 106 107 108 110 114

117

117 117 119 121 123 127 128

Table of Contents I vii

Part III. Common Tasks

7. Simple Files

133

7.0 Introduction

133

7.1 Get the Content of a File

133

7.2 Search a File for Text or a Pattern

135

7.3 Parse and Manage Text-Based Logfiles

136

7.4 Parse and Manage Binary Files

139

7.5 Create a Temporary File

141

7.6 Search and Replace Text in a File

143

8. Structured Files

147

8.0 Introduction

147

8.1 Access Information in an XML File

147

8.2 Perform an XPath Query Against an XML File

150

8.3 Modify Data in an XML File

151

8.4 Easily Import and Export Your Structured Data

153

8.5 Store the Output of a Command in a CSV File

155

8.6 Import Structured Data from a CSV File

156

8.7 Use Excel to Manage Command Output

157

9. Internet-Enabled Scrjpts

160

9.0 Introduction

160

9.1 Download a File from the Internet

160

9.2 Download a Web Pa'ge from the Internet

161

9.3 Program: Get-PageUrls

-'

163

;

9.4 Program: Connect-WebService .

166

9.5 Export Command Output As^a Web Page

170

9.6 Program: Send an Email

"

170

9.7 Program: Interact with Internet Protocols

172

10. Code Reuse

r.

176

10.0 Introduction

176

10.1 Write a Script

176

10.2 Write a Function

179

10.3 Write a Script Block

180

10.4 Return Data from a Script, Function, or Script Block

182

10.5 Place Common Functions in a Library

184

10.6 Access Arguments of a Script, Function, or Script Block

185

viii I Table of Contents

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download