Windows PowerShell Cookbook

Windows PowerShell Cookbook

Lee Holmes

O'REILLY'

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

Table of Contents

Foreword

xvii

Preface

xxi

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

Part II.

3

?

9

3

4

6

7

8

9

9

10

11

12

12

14

16

Fundamentals

1. The Windows PowerShell Interactive Shell

1.0 Introduction

1.1 Run Programs, Scripts, and Existing Tools

1.2 Run a PowerShell Command

19

19

19

21

1.3

1.4

1.5

1.6

1.7

1.8

1.9

1.10

1.11

1.12

1.13

1.14

1.15

1.16

1.17

1.18

1.19

1.20

1.21

1.22

1.23

Customize Your Shell, Profile, and Prompt

22

Find a Command to Accomplish a Task

25

Get Help on a Command

26

Program: Search Help for Text

28

Invoke a PowerShell Script From Outside PowerShell

29

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

Get the System Date and Time

32

Determine the Status of the Last Command

33

Measure the Duration of a Command

34

Customize the Shell to Improve Your Productivity

35

Program: Learn Aliases for Common Commands

36

Access and Manage Your Console History

38

Store the Output of a Command into a File

39

Add Information to the End of a File

41

Record a Transcript of Your Shell Session

41

Display the Properties of an Item As a List

42

Display the Properties of an Item As a Table

42

Manage the Error Output of Commands

44

Configure Debug, Verbose, and Progress Output

45

Extend Your Shell with Additional Snapins

47

Use Console Files to Load and Save Sets of Snapins

48

2. Pipelines

*.

2.0 Introduction

2.1 Filter Items in a List or Command Output

2.2 Program: Simplify Most Where-Object Filters

2.3 Program: Interactively Filter Lists of Objects

2.4 Work with Each Item in a List or Command Output

2.5 Automate Data-Intensive Tasks

.

49

49

50

51

52

54

56

3. Variables and Objects

3.0 Introduction

3.1 Store Information in Variables

3.2 Access Environment Variables

3.3 Control Access and Scope of Variables and Other Items

3.4 Work with .NET Objects

3.5 Create an Instance of a .NET Object

3.6 Program: Create Instances of Generic Objects

3.7 Reduce Typing for Long Class Names

61

61

62

63

65

67

71

73

74

Table of Contents

3.8

3.9

3.10

3.11

3.12

Use a COM Object

Learn About Types and Objects

Get Detailed Documentation About Types and Objects

Add Custom Methods and Properties to Objects

Add Custom Methods and Properties to Types

4. Looping and Flow Control

4.0

4.1

4.2

4.3

4.4

4.5

5.0

5.1

5.2

5.3

5.4

5.5

5.6

5.7

5.8

5.9

5.10

5.11

5.12

5.13

Introduction

Create a String

Create a Multiline or Formatted String

Place Special Characters in a String

Insert Dynamic Information in a String '

Prevent a String from Including Dynamic Information

Place Formatted Information in a String

Search a6tring for Text or a Pattern

Replace Text in a String

Convert a String to Upper/Lowercase

Trim a String '

"

,

Format a Date for Output^ '

Program: Convert Text Streams to Objects

Generate Large Reports'and Text Streams

6. Calculations and Math

6.0

6.1

6.2

6.3

6.4

6.5

6.6

87

Introduction

Make Decisions with Comparison and Logical Operators

Adjust Script Flow Using Conditional Statements

Manage Large Conditional Statements with Switches

Repeat Operations with Loops

Add a Pause or Delay

5. Strings and Unstructured Text..

Introduction

Perform Simple Arithmetic

*

Perform Complex Arithmetic

Measure Statistical Properties of a List

Work with Numbers As Binary

Simplify Math with Administrative Constants

Convert Numbers Between Bases

76

77

78

80

82

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

7.0 Introduction

7.1 Get the Content of a File

7.2 Search a File for Text or a Pattern

7.3 Parse and Manage Text-Based Logfiles

7.4 Parse and Manage Binary Files

7.5 Create a Temporary File

7.6 Search and Replace Text in a File

133

133

133

135

136

139

141

143

8. Structured Files

8.0 Introduction

8.1 Access Information in an XML File

8.2 Perform an XPath Query Against an XML File

8.3 Modify Data in an XML File

8.4 Easily Import and Export Your Structured Data

8.5 Store the Output of a Command in a CSV File

8.6 Import Structured Data from a CSV File

8.7 Use Excel to Manage Command Output

147

147

147

150

151

153

155

156

157

9.

160

160

160

161

163

166

170

170

172

;

10.

viii

Internet-Enabled Scrjpts

9.0 Introduction

9.1 Download a File from the Internet

9.2 Download a Web Pa'ge from the Internet

9.3 Program: Get-PageUrls

-'

9.4 Program: Connect-WebService .

9.5 Export Command Output As^a Web Page

9.6 Program: Send an Email

9.7 Program: Interact with Internet Protocols

"

Code Reuse

r.

10.0 Introduction

10.1 Write a Script

10.2 Write a Function

10.3 Write a Script Block

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

10.5 Place Common Functions in a Library

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

I Table of Contents

176

176

176

179

180

182

184

185

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

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

Google Online Preview   Download