1. Trang chủ
  2. » Công Nghệ Thông Tin

windows powershell cookbook second edition

884 4.7K 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Cấu trúc

  • Table of Contents

  • Foreword

  • Foreword to the First Edition

    • Glue, Enablers, and a WSH

    • That Lee Guy

  • Preface

    • Who This Book Is For

    • How This Book Is Organized

      • Part I: Tour

      • Part II: Fundamentals

      • Part III: Common Tasks

      • Part IV: Administrator Tasks

      • Part V: References

    • What You Need to Use This Book

    • Conventions Used in This Book

    • Code Examples

      • Obtaining Code Examples

      • Using Code Examples

    • Comments and Questions

    • Safari® Books Online

    • Acknowledgments

  • 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. Fundamentals

    • Chapter 1. The Windows PowerShell Interactive Shell

      • 1.0  Introduction

      • 1.1  Run Programs, Scripts, and Existing Tools

        • Problem

        • Solution

        • Discussion

        • See Also

      • 1.2  Resolve Errors Calling Native Executables

        • Problem

        • Solution

        • Discussion

        • See Also

      • 1.3  Run a PowerShell Command

        • Problem

        • Solution

        • Discussion

        • See Also

      • 1.4  Invoke a Long-Running or Background Command

        • Problem

        • Solution

        • Discussion

        • See Also

      • 1.5  Notify Yourself of Job Completion

        • Problem

        • Solution

        • Discussion

        • See Also

      • 1.6  Customize Your Shell, Profile, and Prompt

        • Problem

        • Solution

        • Discussion

        • See Also

      • 1.7  Find a Command to Accomplish a Task

        • Problem

        • Solution

        • Discussion

        • See Also

      • 1.8  Get Help on a Command

        • Problem

        • Solution

        • Discussion

        • See Also

      • 1.9  Program: Search Help for Text

        • See Also

      • 1.10  Program: View PowerShell’s HTML Help

        • See Also

      • 1.11  Launch PowerShell at a Specific Location

        • Problem

        • Solution

        • Discussion

      • 1.12  Invoke a PowerShell Command or Script from Outside PowerShell

        • Problem

        • Solution

        • Discussion

        • See Also

      • 1.13  Customize the Shell to Improve Your Productivity

        • Problem

        • Solution

        • Discussion

        • See Also

      • 1.14  Program: Learn Aliases for Common Commands

        • See Also

      • 1.15  Program: Learn Aliases for Common Parameters

        • Problem

        • Solution

        • Discussion

        • See Also

      • 1.16  Access and Manage Your Console History

        • Problem

        • Solution

        • Discussion

        • See Also

      • 1.17  Program: Create Scripts from Session History

        • See Also

      • 1.18  Invoke a Command from Your Session History

        • Problem

        • Solution

        • Discussion

        • See Also

      • 1.19  Program: Search Formatted Output for a Pattern

        • See Also

      • 1.20  Interactively View and Process Command Output

        • Problem

        • Solution

        • Discussion

          • Processing output

        • See Also

      • 1.21  Store the Output of a Command into a File

        • Problem

        • Solution

        • Discussion

        • See Also

      • 1.22  Add Information to the End of a File

        • Problem

        • Solution

        • Discussion

        • See Also

      • 1.23  Record a Transcript of Your Shell Session

        • Problem

        • Solution

        • Discussion

      • 1.24  Extend Your Shell with Additional Commands

        • Problem

        • Solution

        • Discussion

        • See Also

      • 1.25  Use Commands from Customized Shells

        • Problem

        • Solution

        • Discussion

          • Detecting loaded snapins

          • Detecting loaded modules

        • See Also

      • 1.26  Save State Between Sessions

        • Problem

        • Solution

        • Discussion

        • See Also

    • Chapter 2. Pipelines

      • 2.0  Introduction

      • 2.1  Filter Items in a List or Command Output

        • Problem

        • Solution

        • Discussion

        • See Also

      • 2.2  Group and Pivot Data by Name

        • Problem

        • Solution

        • Discussion

        • See Also

      • 2.3  Program: Simplify Most Where-Object Filters

        • See Also

      • 2.4  Program: Interactively Filter Lists of Objects

        • See Also

      • 2.5  Work with Each Item in a List or Command Output

        • Problem

        • Solution

        • Discussion

        • See Also

      • 2.6  Automate Data-Intensive Tasks

        • Problem

        • Solution

        • Discussion

        • See Also

      • 2.7  Program: Simplify Most Foreach-Object Pipelines

        • Problem

        • Solution

        • Discussion

        • See Also

      • 2.8  Intercept Stages of the Pipeline

        • Problem

        • Solution

        • Discussion

        • See Also

      • 2.9  Automatically Capture Pipeline Output

        • Problem

        • Solution

        • Discussion

        • See Also

      • 2.10  Capture and Redirect Binary Process Output

        • Problem

        • Solution

        • Discussion

        • See Also

    • Chapter 3. Variables and Objects

      • 3.0  Introduction

      • 3.1  Display the Properties of an Item as a List

        • Problem

        • Solution

        • Discussion

      • 3.2  Display the Properties of an Item as a Table

        • Problem

        • Solution

        • Discussion

        • See Also

      • 3.3  Store Information in Variables

        • Problem

        • Solution

        • Discussion

        • See Also

      • 3.4  Access Environment Variables

        • Problem

        • Solution

        • Discussion

        • See Also

      • 3.5  Program: Retain Changes to Environment Variables Set by a Batch File

        • See Also

      • 3.6  Control Access and Scope of Variables and Other Items

        • Problem

        • Solution

        • Discussion

          • Variables

          • Functions

          • Aliases and drives

        • See Also

      • 3.7  Program: Create a Dynamic Variable

      • 3.8  Work with .NET Objects

        • Problem

        • Solution

        • Discussion

          • Static methods

          • Instance methods

          • Static properties

          • Instance properties

        • See Also

      • 3.9  Create an Instance of a .NET Object

        • Problem

        • Solution

        • Discussion

          • Load types from another assembly

        • See Also

      • 3.10  Program: Create Instances of Generic Objects

      • 3.11  Reduce Typing for Long Class Names

        • Problem

        • Solution

        • Discussion

        • See Also

      • 3.12  Use a COM Object

        • Problem

        • Solution

        • Discussion

        • See Also

      • 3.13  Learn About Types and Objects

        • Problem

        • Solution

        • Discussion

        • See Also

      • 3.14  Get Detailed Documentation About Types and Objects

        • Problem

        • Solution

        • Discussion

          • Public constructors

          • Public fields/public properties

          • Public methods

        • See Also

      • 3.15  Add Custom Methods and Properties to Objects

        • Problem

        • Solution

        • Discussion

          • Calculated properties

        • See Also

      • 3.16  Create and Initialize Custom Objects

        • Problem

        • Solution

        • Discussion

        • See Also

      • 3.17  Add Custom Methods and Properties to Types

        • Problem

        • Solution

        • Discussion

          • Getting started

          • Add a ScriptProperty

          • Add an AliasProperty

          • Add a ScriptMethod

          • Add other extension points

    • Chapter 4. Looping and Flow Control

      • 4.0  Introduction

      • 4.1  Make Decisions with Comparison and Logical Operators

        • Problem

        • Solution

        • Discussion

        • See Also

      • 4.2  Adjust Script Flow Using Conditional Statements

        • Problem

        • Solution

        • Discussion

      • 4.3  Manage Large Conditional Statements with Switches

        • Problem

        • Solution

        • Discussion

        • See Also

      • 4.4  Repeat Operations with Loops

        • Problem

        • Solution

        • Discussion

        • See Also

      • 4.5  Add a Pause or Delay

        • Problem

        • Solution

        • Discussion

        • See Also

    • Chapter 5. Strings and Unstructured Text

      • 5.0  Introduction

      • 5.1  Create a String

        • Problem

        • Solution

        • Discussion

        • See Also

      • 5.2  Create a Multiline or Formatted String

        • Problem

        • Solution

        • Discussion

        • See Also

      • 5.3  Place Special Characters in a String

        • Problem

        • Solution

        • Discussion

        • See Also

      • 5.4  Insert Dynamic Information in a String

        • Problem

        • Solution

        • Discussion

        • See Also

      • 5.5  Prevent a String from Including Dynamic Information

        • Problem

        • Solution

        • Discussion

        • See Also

      • 5.6  Place Formatted Information in a String

        • Problem

        • Solution

        • Discussion

        • See Also

      • 5.7  Search a String for Text or a Pattern

        • Problem

        • Solution

        • Discussion

        • See Also

      • 5.8  Replace Text in a String

        • Problem

        • Solution

        • Discussion

        • See Also

      • 5.9  Split a String on Text or a Pattern

        • Problem

        • Solution

        • Discussion

        • See Also

      • 5.10  Combine Strings into a Larger String

        • Problem

        • Solution

        • Discussion

        • See Also

      • 5.11  Convert a String to Upper/Lowercase

        • Problem

        • Solution

        • Discussion

        • See Also

      • 5.12  Trim a String

        • Problem

        • Solution

        • Discussion

        • See Also

      • 5.13  Format a Date for Output

        • Problem

        • Solution

        • Discussion

        • See Also

      • 5.14  Program: Convert Text Streams to Objects

        • See Also

      • 5.15  Generate Large Reports and Text Streams

        • Problem

        • Solution

        • Discussion

          • Creating large text reports

      • 5.16  Generate Source Code and Other Repetitive Text

        • Problem

        • Solution

        • Discussion

    • Chapter 6. Calculations and Math

      • 6.0  Introduction

      • 6.1  Perform Simple Arithmetic

        • Problem

        • Solution

        • Discussion

        • See Also

      • 6.2  Perform Complex Arithmetic

        • Problem

        • Solution

        • Discussion

          • Working with any root

          • Working with degrees instead of radians

        • See Also

      • 6.3  Measure Statistical Properties of a List

        • Problem

        • Solution

        • Discussion

      • 6.4  Work with Numbers as Binary

        • Problem

        • Solution

        • Discussion

        • See Also

      • 6.5  Simplify Math with Administrative Constants

        • Problem

        • Solution

        • Discussion

        • See Also

      • 6.6  Convert Numbers Between Bases

        • Problem

        • Solution

        • Discussion

        • See Also

    • Chapter 7. Lists, Arrays, and Hashtables

      • 7.0  Introduction

      • 7.1  Create an Array or List of Items

        • Problem

        • Solution

        • Discussion

        • See Also

      • 7.2  Create a Jagged or Multidimensional Array

        • Problem

        • Solution

        • Discussion

        • See Also

      • 7.3  Access Elements of an Array

        • Problem

        • Solution

        • Discussion

        • See Also

      • 7.4  Visit Each Element of an Array

        • Problem

        • Solution

        • Discussion

        • See Also

      • 7.5  Sort an Array or List of Items

        • Problem

        • Solution

        • Discussion

      • 7.6  Determine Whether an Array Contains an Item

        • Problem

        • Solution

        • Discussion

        • See Also

      • 7.7  Combine Two Arrays

        • Problem

        • Solution

        • Discussion

        • See Also

      • 7.8  Find Items in an Array That Match a Value

        • Problem

        • Solution

        • Discussion

        • See Also

      • 7.9  Compare Two Lists

        • Problem

        • Solution

        • Discussion

        • See Also

      • 7.10  Remove Elements from an Array

        • Problem

        • Solution

        • Discussion

        • See Also

      • 7.11  Find Items in an Array Greater or Less Than a Value

        • Problem

        • Solution

        • Discussion

        • See Also

      • 7.12  Use the ArrayList Class for Advanced Array Tasks

        • Problem

        • Solution

        • Discussion

        • See Also

      • 7.13  Create a Hashtable or Associative Array

        • Problem

        • Solution

        • Discussion

        • See Also

      • 7.14  Sort a Hashtable by Key or Value

        • Problem

        • Solution

        • Discussion

        • See Also

    • Chapter 8. Utility Tasks

      • 8.0  Introduction

      • 8.1  Get the System Date and Time

        • Problem

        • Solution

        • Discussion

        • See Also

      • 8.2  Measure the Duration of a Command

        • Problem

        • Solution

        • Discussion

        • See Also

      • 8.3  Read and Write from the Windows Clipboard

        • Problem

        • Solution

        • Discussion

        • See Also

      • 8.4  Generate a Random Number or Object

        • Problem

        • Solution

        • Discussion

        • See Also

      • 8.5  Program: Search the Windows Start Menu

        • See Also

      • 8.6  Program: Show Colorized Script Content

        • Discussion

        • See Also

  • Part III. Common Tasks

    • Chapter 9. Simple Files

      • 9.0  Introduction

      • 9.1  Get the Content of a File

        • Problem

        • Solution

        • Discussion

        • See Also

      • 9.2  Search a File for Text or a Pattern

        • Problem

        • Solution

        • Discussion

        • See Also

      • 9.3  Parse and Manage Text-Based Logfiles

        • Problem

        • Solution

        • Discussion

        • See Also

      • 9.4  Parse and Manage Binary Files

        • Problem

        • Solution

        • Discussion

        • See Also

      • 9.5  Create a Temporary File

        • Problem

        • Solution

        • Discussion

        • See Also

      • 9.6  Search and Replace Text in a File

        • Problem

        • Solution

        • Discussion

          • Work with files encoded in Unicode or another (OEM) code page

          • Replace text using a pattern instead of plain text

          • Replace text that spans multiple lines

          • Replace text in large files

        • See Also

      • 9.7  Program: Get the Encoding of a File

        • See Also

      • 9.8  Program: View the Hexadecimal Representation of Content

        • See Also

    • Chapter 10. Structured Files

      • 10.0  Introduction

      • 10.1  Access Information in an XML File

        • Problem

        • Solution

        • Discussion

        • See Also

      • 10.2  Perform an XPath Query Against XML

        • Problem

        • Solution

        • Discussion

        • See Also

      • 10.3  Convert Objects to XML

        • Problem

        • Solution

        • Discussion

        • See Also

      • 10.4  Modify Data in an XML File

        • Problem

        • Solution

        • Discussion

      • 10.5  Easily Import and Export Your Structured Data

        • Problem

        • Solution

        • Discussion

      • 10.6  Store the Output of a Command in a CSV or Delimited File

        • Problem

        • Solution

        • Discussion

        • See Also

      • 10.7  Import CSV and Delimited Data from a File

        • Problem

        • Solution

        • Discussion

        • See Also

      • 10.8  Use Excel to Manage Command Output

        • Problem

        • Solution

        • Discussion

        • See Also

      • 10.9  Parse and Interpret PowerShell Scripts

        • Problem

        • Solution

        • Discussion

        • See Also

    • Chapter 11. Code Reuse

      • 11.0  Introduction

      • 11.1  Write a Script

        • Problem

        • Solution

        • Discussion

        • See Also

      • 11.2  Write a Function

        • Problem

        • Solution

        • Discussion

        • See Also

      • 11.3  Find a Verb Appropriate for a Command Name

        • Problem

        • Solution

        • Discussion

        • See Also

      • 11.4  Write a Script Block

        • Problem

        • Solution

        • Discussion

        • See Also

      • 11.5  Return Data from a Script, Function, or Script Block

        • Problem

        • Solution

        • Discussion

        • See Also

      • 11.6  Package Common Commands in a Module

        • Problem

        • Solution

        • Discussion

        • See Also

      • 11.7  Write Commands That Maintain State

        • Problem

        • Solution

        • Discussion

        • See Also

      • 11.8  Selectively Export Commands from a Module

        • Problem

        • Solution

        • Discussion

        • See Also

      • 11.9  Diagnose and Interact with Internal Module State

        • Problem

        • Solution

        • Discussion

        • See Also

      • 11.10  Handle Cleanup Tasks When a Module Is Removed

        • Problem

        • Solution

        • Discussion

        • See Also

      • 11.11  Access Arguments of a Script, Function, or Script Block

        • Problem

        • Solution

        • Discussion

          • Supporting PowerShell’s common parameters

          • Using the $args array

        • See Also

      • 11.12  Add Validation to Parameters

        • Problem

        • Solution

        • Discussion

          • Defining parameter behavior

          • Defining parameter validation

        • See Also

      • 11.13  Accept Script Block Parameters with Local Variables

        • Problem

        • Solution

        • Discussion

        • See Also

      • 11.14  Dynamically Compose Command Parameters

        • Problem

        • Solution

        • Discussion

        • See Also

      • 11.15  Provide -WhatIf, -Confirm, and Other Cmdlet Features

        • Problem

        • Solution

        • Discussion

        • See Also

      • 11.16  Add Help to Scripts or Functions

        • Problem

        • Solution

        • Discussion

        • See Also

      • 11.17  Add Custom Tags to a Function or Script Block

        • Problem

        • Solution

        • Discussion

        • See Also

      • 11.18  Access Pipeline Input

        • Problem

        • Solution

        • Discussion

        • See Also

      • 11.19  Write Pipeline-Oriented Scripts with Cmdlet Keywords

        • Problem

        • Solution

        • Discussion

        • See Also

      • 11.20  Write a Pipeline-Oriented Function

        • Problem

        • Solution

        • Discussion

        • See Also

      • 11.21  Organize Scripts for Improved Readability

        • Problem

        • Solution

        • Discussion

        • See Also

      • 11.22  Invoke Dynamically Named Commands

        • Problem

        • Solution

        • Discussion

        • See Also

      • 11.23  Program: Enhance or Extend an Existing Cmdlet

        • See Also

    • Chapter 12. Internet-Enabled Scripts

      • 12.0  Introduction

      • 12.1  Download a File from the Internet

        • Problem

        • Solution

        • Discussion

        • See Also

      • 12.2  Download a Web Page from the Internet

        • Problem

        • Solution

        • Discussion

        • See Also

      • 12.3  Program: Get-PageUrls

        • See Also

      • 12.4  Connect to a Web Service

        • Problem

        • Solution

        • Discussion

        • See Also

      • 12.5  Program: Connect-WebService

        • See Also

      • 12.6  Export Command Output as a Web Page

        • Problem

        • Solution

        • Discussion

      • 12.7  Send an Email

        • Problem

        • Solution

        • Discussion

        • See Also

      • 12.8  Program: Send-MailMessage

        • See Also

      • 12.9  Program: Interact with Internet Protocols

        • See Also

    • Chapter 13. User Interaction

      • 13.0  Introduction

      • 13.1  Read a Line of User Input

        • Problem

        • Solution

        • Discussion

        • See Also

      • 13.2  Read a Key of User Input

        • Problem

        • Solution

        • Discussion

      • 13.3  Program: Display a Menu to the User

        • See Also

      • 13.4  Display Messages and Output to the User

        • Problem

        • Solution

        • Discussion

        • See Also

      • 13.5  Provide Progress Updates on Long-Running Tasks

        • Problem

        • Solution

        • Discussion

      • 13.6  Write Culture-Aware Scripts

        • Problem

        • Solution

        • Discussion

          • Date, time, and number formats

          • Complexity of user input and file content

          • Capitalization rules

          • Sorting rules

          • Other guidelines

        • See Also

      • 13.7  Support Other Languages in Script Output

        • Problem

        • Solution

        • Discussion

        • See Also

      • 13.8  Program: Invoke a Script Block with Alternate Culture Settings

        • See Also

      • 13.9  Access Features of the Host’s User Interface

        • Problem

        • Solution

        • Discussion

      • 13.10  Program: Add a Graphical User Interface to Your Script

        • See Also

      • 13.11  Interact with UI Frameworks and STA Objects

        • Problem

        • Solution

        • Discussion

        • See Also

    • Chapter 14. Debugging

      • 14.0  Introduction

      • 14.1  Prevent Common Scripting Errors

        • Problem

        • Solution

        • Discussion

        • See Also

      • 14.2  Trace Script Execution

        • Problem

        • Solution

        • Discussion

        • See Also

      • 14.3  Set a Script Breakpoint

        • Problem

        • Solution

        • Discussion

        • See Also

      • 14.4  Debug a Script When It Encounters an Error

        • Problem

        • Solution

        • Discussion

        • See Also

      • 14.5  Create a Conditional Breakpoint

        • Problem

        • Solution

        • Discussion

        • See Also

      • 14.6  Investigate System State While Debugging

        • Problem

        • Solution

        • Discussion

        • See Also

      • 14.7  Program: Watch an Expression for Changes

        • See Also

      • 14.8  Program: Get Script Code Coverage

        • See Also

    • Chapter 15. Tracing and Error Management

      • 15.0  Introduction

      • 15.1  Determine the Status of the Last Command

        • Problem

        • Solution

        • Discussion

      • 15.2  View the Errors Generated by a Command

        • Problem

        • Solution

        • Discussion

        • See Also

      • 15.3  Manage the Error Output of Commands

        • Problem

        • Solution

        • Discussion

        • See Also

      • 15.4  Program: Resolve an Error

        • See Also

      • 15.5  Configure Debug, Verbose, and Progress Output

        • Problem

        • Solution

        • Discussion

        • See Also

      • 15.6  Handle Warnings, Errors, and Terminating Errors

        • Problem

        • Solution

        • Discussion

        • See Also

      • 15.7  Output Warnings, Errors, and Terminating Errors

        • Problem

        • Solution

        • Discussion

        • See Also

      • 15.8  Program: Analyze a Script’s Performance Profile

        • See Also

    • Chapter 16. Environmental Awareness

      • 16.0  Introduction

      • 16.1  View and Modify Environment Variables

        • Problem

        • Solution

        • Discussion

        • See Also

      • 16.2  Access Information About Your Command’s Invocation

        • Problem

        • Solution

        • Discussion

          • Scripts

          • Functions

          • Script blocks

      • 16.3  Program: Investigate the InvocationInfo Variable

        • See Also

      • 16.4  Find Your Script’s Name

        • Problem

        • Solution

        • Discussion

        • See Also

      • 16.5  Find Your Script’s Location

        • Problem

        • Solution

        • Discussion

        • See Also

      • 16.6  Find the Location of Common System Paths

        • Problem

        • Solution

        • Discussion

        • See Also

      • 16.7  Get the Current Location

        • Problem

        • Solution

        • Discussion

        • See Also

      • 16.8  Safely Build File Paths Out of Their Components

        • Problem

        • Solution

        • Discussion

      • 16.9  Interact with PowerShell’s Global Environment

        • Problem

        • Solution

        • Discussion

        • See Also

      • 16.10  Determine PowerShell Version Information

        • Problem

        • Solution

        • Discussion

    • Chapter 17. Extend the Reach of Windows PowerShell

      • 17.0  Introduction

      • 17.1  Automate Programs Using COM Scripting Interfaces

        • Problem

        • Solution

        • Discussion

        • See Also

      • 17.2  Program: Query a SQL Data Source

        • See Also

      • 17.3  Access Windows Performance Counters

        • Problem

        • Solution

        • Discussion

      • 17.4  Access Windows API Functions

        • Problem

        • Solution

        • Discussion

        • See Also

      • 17.5  Program: Invoke Simple Windows API Calls

        • See Also

      • 17.6  Define or Extend a .NET Class

        • Problem

        • Solution

        • Discussion

        • See Also

      • 17.7  Add Inline C# to Your PowerShell Script

        • Problem

        • Solution

        • Discussion

        • See Also

      • 17.8  Access a .NET SDK Library

        • Problem

        • Solution

        • Discussion

        • See Also

      • 17.9  Create Your Own PowerShell Cmdlet

        • Problem

        • Solution

        • Discussion

          • Step 1: Download the PowerShell SDK

          • Step 2: Create a file to hold the cmdlet source code

          • Step 3: Compile the DLL

          • Step 4: Load the module

          • Step 6: Use the module

        • See Also

      • 17.10  Add PowerShell Scripting to Your Own Program

        • Problem

        • Solution

        • Discussion

          • Step 1: Download the PowerShell SDK

          • Step 2: Create a file to hold the hosting source code

          • Step 3: Compile and run the example

        • See Also

    • Chapter 18. Security and Script Signing

      • 18.0  Introduction

      • 18.1  Enable Scripting Through an Execution Policy

        • Problem

        • Solution

        • Discussion

          • Execution policies are not user restrictions

        • See Also

      • 18.2  Disable Warnings for UNC Paths

        • Problem

        • Solution

        • Discussion

        • See Also

      • 18.3  Sign a PowerShell Script, Module, or Formatting File

        • Problem

        • Solution

        • Discussion

        • See Also

      • 18.4  Program: Create a Self-Signed Certificate

        • Discussion

        • See Also

      • 18.5  Manage PowerShell Security in an Enterprise

        • Problem

        • Solution

        • Discussion

          • Apply PowerShell’s Group Policy templates

          • Deploy Microsoft Certificate Services

        • See Also

      • 18.6  Block Scripts by Publisher, Path, or Hash

        • Problem

        • Solution

        • Discussion

        • See Also

      • 18.7  Verify the Digital Signature of a PowerShell Script

        • Problem

        • Solution

        • Discussion

      • 18.8  Securely Handle Sensitive Information

        • Problem

        • Solution

        • Discussion

        • See Also

      • 18.9  Securely Request Usernames and Passwords

        • Problem

        • Solution

        • Discussion

        • See Also

      • 18.10  Program: Start a Process as Another User

        • See Also

      • 18.11  Program: Run a Temporarily Elevated Command

        • See Also

      • 18.12  Securely Store Credentials on Disk

        • Problem

        • Solution

          • Save the credential’s password to disk

          • Recreate the credential from the password stored on disk

        • Discussion

        • See Also

      • 18.13  Access User and Machine Certificates

        • Problem

        • Solution

        • Discussion

        • See Also

      • 18.14  Program: Search the Certificate Store

        • See Also

      • 18.15  Add and Remove Certificates

        • Problem

        • Solution

        • Discussion

        • See Also

      • 18.16  Manage Security Descriptors in SDDL Form

        • Problem

        • Solution

        • Discussion

        • See Also

    • Chapter 19. Integrated Scripting Environment

      • 19.0  Introduction

      • 19.1  Debug a Script

        • Problem

        • Solution

        • Discussion

        • See Also

      • 19.2  Customize Text and User Interface Colors

        • Problem

        • Solution

        • Discussion

        • See Also

      • 19.3  Connect to a Remote Computer

        • Problem

        • Solution

        • Discussion

        • See Also

      • 19.4  Extend ISE Functionality Through Its Object Model

        • Problem

        • Solution

        • Discussion

        • See Also

      • 19.5  Add an Item to the Tools Menu

        • Problem

        • Solution

        • Discussion

        • See Also

  • Part IV. Administrator Tasks

    • Chapter 20. Files and Directories

      • 20.0  Introduction

      • 20.1  Determine the Current Location

        • Problem

        • Solution

        • Discussion

      • 20.2  Get the Files in a Directory

        • Problem

        • Solution

        • Discussion

        • See Also

      • 20.3  Find All Files Modified Before a Certain Date

        • Problem

        • Solution

        • Discussion

        • See Also

      • 20.4  Clear the Content of a File

        • Problem

        • Solution

        • Discussion

        • See Also

      • 20.5  Manage and Change the Attributes of a File

        • Problem

        • Solution

        • Discussion

        • See Also

      • 20.6  Find Files That Match a Pattern

        • Problem

        • Solution

        • Discussion

        • See Also

      • 20.7  Manage Files That Include Special Characters

        • Problem

        • Solution

        • Discussion

      • 20.8  Program: Get Disk Usage Information

        • See Also

      • 20.9  Monitor a File for Changes

        • Problem

        • Solution

        • Discussion

      • 20.10  Get the Version of a DLL or Executable

        • Problem

        • Solution

        • Discussion

        • See Also

      • 20.11  Program: Get the MD5 or SHA1 Hash of a File

        • See Also

      • 20.12  Create a Directory

        • Problem

        • Solution

        • Discussion

      • 20.13  Remove a File or Directory

        • Problem

        • Solution

        • Discussion

        • See Also

      • 20.14  Rename a File or Directory

        • Problem

        • Solution

        • Discussion

        • See Also

      • 20.15  Move a File or Directory

        • Problem

        • Solution

        • Discussion

        • See Also

      • 20.16  Program: Move or Remove a Locked File

        • See Also

      • 20.17  Get the ACL of a File or Directory

        • Problem

        • Solution

        • Discussion

        • See Also

      • 20.18  Set the ACL of a File or Directory

        • Problem

        • Solution

        • Discussion

        • See Also

      • 20.19  Program: Add Extended File Properties to Files

        • See Also

      • 20.20  Program: Create a Filesystem Hard Link

        • See Also

      • 20.21  Program: Create a ZIP Archive

        • See Also

    • Chapter 21. The Windows Registry

      • 21.0  Introduction

      • 21.1  Navigate the Registry

        • Problem

        • Solution

        • Discussion

        • See Also

      • 21.2  View a Registry Key

        • Problem

        • Solution

        • Discussion

      • 21.3  Modify or Remove a Registry Key Value

        • Problem

        • Solution

        • Discussion

      • 21.4  Create a Registry Key Value

        • Problem

        • Solution

        • Discussion

      • 21.5  Remove a Registry Key

        • Problem

        • Solution

        • Discussion

        • See Also

      • 21.6  Safely Combine Related Registry Modifications

        • Problem

        • Solution

        • Discussion

        • See Also

      • 21.7  Add a Site to an Internet Explorer Security Zone

        • Problem

        • Solution

        • Discussion

        • See Also

      • 21.8  Modify Internet Explorer Settings

        • Problem

        • Solution

        • Discussion

        • See Also

      • 21.9  Program: Search the Windows Registry

        • See Also

      • 21.10  Get the ACL of a Registry Key

        • Problem

        • Solution

        • Discussion

        • See Also

      • 21.11  Set the ACL of a Registry Key

        • Problem

        • Solution

        • Discussion

        • See Also

      • 21.12  Work with the Registry of a Remote Computer

        • Problem

        • Solution

        • Discussion

        • See Also

      • 21.13  Program: Get Registry Items from Remote Machines

        • See Also

      • 21.14  Program: Get Properties of Remote Registry Keys

        • See Also

      • 21.15  Program: Set Properties of Remote Registry Keys

        • See Also

      • 21.16  Discover Registry Settings for Programs

        • Problem

        • Solution

        • Discussion

          • Launch and configure Process Monitor

          • Prepare to manually set the configuration option

          • Tell Process Monitor to begin capturing information

          • Manually set the configuration option

          • Tell Process Monitor to stop capturing information

          • Review the capture logs for registry modification

          • Automate these registry writes

        • See Also

    • Chapter 22. Comparing Data

      • 22.0  Introduction

      • 22.1  Compare the Output of Two Commands

        • Problem

        • Solution

        • Discussion

      • 22.2  Determine the Differences Between Two Files

        • Problem

        • Solution

        • Discussion

      • 22.3  Verify Integrity of File Sets

        • Problem

        • Solution

        • Discussion

        • See Also

    • Chapter 23. Event Logs

      • 23.0  Introduction

      • 23.1  List All Event Logs

        • Problem

        • Solution

        • Discussion

        • See Also

      • 23.2  Get the Newest Entries from an Event Log

        • Problem

        • Solution

        • Discussion

        • See Also

      • 23.3  Find Event Log Entries with Specific Text

        • Problem

        • Solution

        • Discussion

        • See Also

      • 23.4  Retrieve and Filter Event Log Entries

        • Problem

        • Solution

        • Discussion

          • Efficiently processing simple queries

          • Automating GUI-generated searches

          • Performing complex event analysis and correlation

        • See Also

      • 23.5  Find Event Log Entries by Their Frequency

        • Problem

        • Solution

        • Discussion

        • See Also

      • 23.6  Back Up an Event Log

        • Problem

        • Solution

        • Discussion

      • 23.7  Create or Remove an Event Log

        • Problem

        • Solution

        • Discussion

        • See Also

      • 23.8  Write to an Event Log

        • Problem

        • Solution

        • Discussion

        • See Also

      • 23.9  Run a PowerShell Script for Windows Event Log Entries

        • Problem

        • Solution

        • Discussion

        • See Also

      • 23.10  Clear or Maintain an Event Log

        • Problem

        • Solution

        • Discussion

        • See Also

      • 23.11  Access Event Logs of a Remote Machine

        • Problem

        • Solution

        • Discussion

        • See Also

    • Chapter 24. Processes

      • 24.0  Introduction

      • 24.1  List Currently Running Processes

        • Problem

        • Solution

        • Discussion

        • See Also

      • 24.2  Launch the Application Associated with a Document

        • Problem

        • Solution

        • Discussion

        • See Also

      • 24.3  Launch a Process

        • Problem

        • Solution

        • Discussion

        • See Also

      • 24.4  Stop a Process

        • Problem

        • Solution

        • Discussion

      • 24.5  Debug a Process

        • Problem

        • Solution

        • Discussion

        • See Also

    • Chapter 25. System Services

      • 25.0  Introduction

      • 25.1  List All Running Services

        • Problem

        • Solution

        • Discussion

        • See Also

      • 25.2  Manage a Running Service

        • Problem

        • Solution

        • Discussion

        • See Also

      • 25.3  Configure a Service

        • Problem

        • Solution

        • Discussion

        • See Also

    • Chapter 26. Active Directory

      • 26.0  Introduction

      • 26.1  Test Active Directory Scripts on a Local Installation

        • Problem

        • Solution

        • Discussion

          • Verify prerequisites

          • Install ADAM

          • Create a test instance

        • See Also

      • 26.2  Create an Organizational Unit

        • Problem

        • Solution

        • Discussion

        • See Also

      • 26.3  Get the Properties of an Organizational Unit

        • Problem

        • Solution

        • Discussion

      • 26.4  Modify Properties of an Organizational Unit

        • Problem

        • Solution

        • Discussion

      • 26.5  Delete an Organizational Unit

        • Problem

        • Solution

        • Discussion

      • 26.6  Get the Children of an Active Directory Container

        • Problem

        • Solution

        • Discussion

        • See Also

      • 26.7  Create a User Account

        • Problem

        • Solution

        • Discussion

        • See Also

      • 26.8  Program: Import Users in Bulk to Active Directory

        • See Also

      • 26.9  Search for a User Account

        • Problem

        • Solution

        • Discussion

      • 26.10  Get and List the Properties of a User Account

        • Problem

        • Solution

        • Discussion

      • 26.11  Modify Properties of a User Account

        • Problem

        • Solution

        • Discussion

      • 26.12  Change a User Password

        • Problem

        • Solution

        • Discussion

        • See Also

      • 26.13  Create a Security or Distribution Group

        • Problem

        • Solution

        • Discussion

        • See Also

      • 26.14  Search for a Security or Distribution Group

        • Problem

        • Solution

        • Discussion

      • 26.15  Get the Properties of a Group

        • Problem

        • Solution

        • Discussion

      • 26.16  Find the Owner of a Group

        • Problem

        • Solution

        • Discussion

      • 26.17  Modify Properties of a Security or Distribution Group

        • Problem

        • Solution

        • Discussion

      • 26.18  Add a User to a Security or Distribution Group

        • Problem

        • Solution

        • Discussion

        • See Also

      • 26.19  Remove a User from a Security or Distribution Group

        • Problem

        • Solution

        • Discussion

        • See Also

      • 26.20  List a User’s Group Membership

        • Problem

        • Solution

        • Discussion

        • See Also

      • 26.21  List the Members of a Group

        • Problem

        • Solution

        • Discussion

        • See Also

      • 26.22  List the Users in an Organizational Unit

        • Problem

        • Solution

        • Discussion

        • See Also

      • 26.23  Search for a Computer Account

        • Problem

        • Solution

        • Discussion

      • 26.24  Get and List the Properties of a Computer Account

        • Problem

        • Solution

        • Discussion

    • Chapter 27. Enterprise Computer Management

      • 27.0  Introduction

      • 27.1  Join a Computer to a Domain or Workgroup

        • Problem

        • Solution

        • Discussion

        • See Also

      • 27.2  Remove a Computer from a Domain

        • Problem

        • Solution

        • Discussion

        • See Also

      • 27.3  Program: List Logon or Logoff Scripts for a User

        • See Also

      • 27.4  Program: List Startup or Shutdown Scripts for a Machine

        • See Also

      • 27.5  Deploy PowerShell-Based Logon Scripts

        • Problem

        • Solution

        • Discussion

        • See Also

      • 27.6  Enable or Disable the Windows Firewall

        • Problem

        • Solution

        • Discussion

        • See Also

      • 27.7  Open or Close Ports in the Windows Firewall

        • Problem

        • Solution

        • Discussion

        • See Also

      • 27.8  Program: List All Installed Software

        • See Also

      • 27.9  Uninstall an Application

        • Problem

        • Solution

        • Discussion

        • See Also

      • 27.10  Manage Computer Restore Points

        • Problem

        • Solution

        • Discussion

      • 27.11  Reboot or Shut Down a Computer

        • Problem

        • Solution

        • Discussion

        • See Also

      • 27.12  Determine Whether a Hotfix Is Installed

        • Problem

        • Solution

        • Discussion

      • 27.13  Manage Scheduled Tasks on a Computer

        • Problem

        • Solution

        • Discussion

        • See Also

      • 27.14  Retrieve Printer Information

        • Problem

        • Solution

        • Discussion

        • See Also

      • 27.15  Retrieve Printer Queue Statistics

        • Problem

        • Solution

        • Discussion

        • See Also

      • 27.16  Manage Printers and Print Queues

        • Problem

        • Solution

        • Discussion

        • See Also

      • 27.17  Program: Summarize System Information

        • See Also

      • 27.18  Renew a DHCP Lease

        • Problem

        • Solution

        • Discussion

          • Use the Win32_NetworkAdapterConfiguration WMI class

          • Run ipconfig on the remote computer

        • See Also

      • 27.19  Assign a Static IP Address

        • Problem

        • Solution

        • Discussion

        • See Also

      • 27.20  List All IP Addresses for a Computer

        • Problem

        • Solution

        • Discussion

          • Use the Win32_NetworkAdapterConfiguration WMI class

          • Run ipconfig on the remote computer

        • See Also

      • 27.21  List Network Adapter Properties

        • Problem

        • Solution

        • Discussion

        • See Also

    • Chapter 28. Windows Management Instrumentation

      • 28.0  Introduction

      • 28.1  Access Windows Management Instrumentation Data

        • Problem

        • Solution

        • Discussion

        • See Also

      • 28.2  Modify the Properties of a WMI Instance

        • Problem

        • Solution

        • Discussion

        • See Also

      • 28.3  Invoke a Method on a WMI Class

        • Problem

        • Solution

        • Discussion

        • See Also

      • 28.4  Program: Determine Properties Available to WMI Filters

        • See Also

      • 28.5  Program: Search for WMI Classes

        • See Also

      • 28.6  Use .NET to Perform Advanced WMI Tasks

        • Problem

        • Solution

          • Advanced instance features

          • Advanced class features

          • Advanced query feature

        • Discussion

        • See Also

      • 28.7  Improve the Performance of Large-Scale WMI Operations

        • Problem

        • Solution

        • Discussion

        • See Also

      • 28.8  Convert a VBScript WMI Script to PowerShell

        • Problem

        • Solution

        • Discussion

          • Retrieving data

          • Calling methods on an instance

          • Calling methods on a class

        • See Also

    • Chapter 29. Remoting

      • 29.0  Introduction

      • 29.1  Find Commands That Support Their Own Remoting

        • Problem

        • Solution

        • Discussion

        • See Also

      • 29.2  Program: Invoke a PowerShell Expression on a Remote Machine

        • See Also

      • 29.3  Test Connectivity Between Two Computers

        • Problem

        • Solution

        • Discussion

        • See Also

      • 29.4  Limit Networking Scripts to Hosts That Respond

        • Problem

        • Solution

        • Discussion

        • See Also

      • 29.5  Enable PowerShell Remoting on a Computer

        • Problem

        • Solution

        • Discussion

          • Enable remoting on a single local machine

          • Enable remoting on a remote machine

          • Enable remoting in an enterprise

        • See Also

      • 29.6  Enable Remote Desktop on a Computer

        • Problem

        • Solution

        • Discussion

        • See Also

      • 29.7  Program: Remotely Enable PowerShell Remoting

        • See Also

      • 29.8  Configure User Permissions for Remoting

        • Problem

        • Solution

        • Discussion

        • See Also

      • 29.9  Enable Remoting to Workgroup Computers

        • Problem

        • Solution

        • Discussion

      • 29.10  Interactively Manage a Remote Computer

        • Problem

        • Solution

        • Discussion

        • See Also

      • 29.11  Invoke a Command on a Remote Computer

        • Problem

        • Solution

        • Discussion

        • See Also

      • 29.12  Implicitly Invoke Commands from a Remote Computer

        • Problem

        • Solution

        • Discussion

        • See Also

      • 29.13  Create Sessions with Full Network Access

        • Problem

        • Solution

        • Discussion

        • See Also

      • 29.14  Pass Variables to Remote Sessions

        • Problem

        • Solution

        • Discussion

        • See Also

      • 29.15  Configure Advanced Remoting Options

        • Problem

        • Solution

        • Discussion

        • See Also

      • 29.16  Invoke a Command on Many Computers

        • Problem

        • Solution

        • Discussion

        • See Also

      • 29.17  Run a Local Script on a Remote Computer

        • Problem

        • Solution

        • Discussion

        • See Also

      • 29.18  Program: Transfer a File to a Remote Computer

        • See Also

      • 29.19  Determine Whether a Script Is Running on a Remote Computer

        • Problem

        • Solution

        • Discussion

        • See Also

      • 29.20  Program: Create a Task-Specific Remoting Endpoint

        • See Also

    • Chapter 30. Transactions

      • 30.0  Introduction

      • 30.1  Safely Experiment with Transactions

        • Problem

        • Solution

        • Discussion

        • See Also

      • 30.2  Change Error Recovery Behavior in Transactions

        • Problem

        • Solution

        • Discussion

        • See Also

    • Chapter 31. Event Handling

      • 31.0  Introduction

      • 31.1  Respond to Automatically Generated Events

        • Problem

        • Solution

        • Discussion

        • See Also

      • 31.2  Create and Respond to Custom Events

        • Problem

        • Solution

        • Discussion

        • See Also

      • 31.3  Create a Temporary Event Subscription

        • Problem

        • Solution

        • Discussion

        • See Also

      • 31.4  Forward Events from a Remote Computer

        • Problem

        • Solution

        • Discussion

        • See Also

      • 31.5  Investigate Internal Event Action State

        • Problem

        • Solution

        • Discussion

        • See Also

      • 31.6  Use a Script Block as a .NET Delegate or Event Handler

        • Problem

        • Solution

        • Discussion

        • See Also

  • Part V. References

    • Appendix A. PowerShell Language and Environment

      • Commands and Expressions

      • Comments

      • Variables

      • Booleans

      • Strings

        • Literal and Expanding Strings

        • Here Strings

        • Escape Sequences

      • Numbers

        • Simple Assignment

        • Administrative Numeric Constants

        • Hexadecimal and Other Number Bases

      • Arrays and Lists

        • Array Definitions

        • Array Access

        • Array Slicing

      • Hashtables (Associative Arrays)

        • Hashtable Definitions

        • Hashtable Access

      • XML

      • Simple Operators

        • Arithmetic Operators

        • Logical Operators

        • Binary Operators

        • Other Operators

      • Comparison Operators

      • Conditional Statements

        • if, elseif, and else Statements

        • switch Statements

      • Looping Statements

        • for Statement

        • foreach Statement

        • while Statement

        • do … while Statement/do … until Statement

        • Flow Control Statements

          • break

          • continue

      • Working with the .NET Framework

        • Static Methods

        • Instance Methods

        • Static Properties

        • Instance Properties

        • Learning About Types

          • The Get-Member cmdlet

          • .NET Framework documentation

        • Type Shortcuts

        • Creating Instances of Types

        • Interacting with COM Objects

        • Extending Types

          • The Add-Member cmdlet

          • Custom type extension files

      • Writing Scripts, Reusing Functionality

        • Writing Commands

          • Writing scripts

          • Writing functions

          • Writing script blocks

        • Running Commands

          • Invoking

          • Dot-sourcing

          • Parameter splatting

        • Providing Input to Commands

          • Argument array

          • Formal parameters

          • Command behavior customizations

          • Parameter attribute customizations

          • Parameter validation attributes

          • Pipeline input

          • Cmdlet keywords in commands

          • $MyInvocation automatic variable

        • Retrieving Output from Commands

          • Pipeline output

          • Return statement

          • Exit statement

        • Help Documentation

      • Managing Errors

        • Nonterminating Errors

        • Terminating Errors

      • Formatting Output

        • Custom Formatting Files

      • Capturing Output

      • Common Customization Points

        • Console Settings

          • Adjust your window size

          • Make text selection easier

          • Use hotkeys to operate the shell more efficiently

        • Profiles

        • Prompts

        • Tab Completion

    • Appendix B. Regular Expression Reference

    • Appendix C. XPath Quick Reference

    • Appendix D. .NET String Formatting

      • String Formatting Syntax

      • Standard Numeric Format Strings

      • Custom Numeric Format Strings

    • Appendix E. .NET DateTime Formatting

      • Custom DateTime Format Strings

    • Appendix F. Selected .NET Classes and Their Uses

    • Appendix G. WMI Reference

    • Appendix H. Selected COM Objects and Their Uses

    • Appendix I. Selected Events and Their Uses

    • Appendix J. Standard PowerShell Verbs

  • Index

Nội dung

www.it-ebooks.info www.it-ebooks.info Windows PowerShell Cookbook www.it-ebooks.info www.it-ebooks.info SECOND EDITION Windows PowerShell Cookbook Lee Holmes Beijing • Cambridge • Farnham • Köln • Sebastopol • Taipei • Tokyo www.it-ebooks.info Windows PowerShell Cookbook, Second Edition by Lee Holmes Copyright © 2010 Lee Holmes. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://my.safaribooksonline.com). For more information, contact our corporate/institutional sales department: (800) 998-9938 or corporate@oreilly.com. Editor: Mike Hendrickson Production Editor: Teresa Elsey Copyeditor: Genevieve d’Entremont Proofreader: Teresa Elsey Indexer: Newgen North America, Inc. Cover Designer: Karen Montgomery Interior Designer: David Futato Illustrator: Robert Romano Printing History: October 2007: First Edition. August 2010: Second Edition. Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly Media, Inc. Windows PowerShell Cookbook, the image of a box turtle, and related trade dress are trademarks of O’Reilly Media, Inc. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and O’Reilly Media, Inc., was aware of a trademark claim, the designations have been printed in caps or initial caps. While every precaution has been taken in the preparation of this book, the publisher and authors assume no responsibility for errors or omissions, or for damages resulting from the use of the information con- tained herein. ISBN: 978-0-596-80150-2 [M] 1281554603 www.it-ebooks.info Table of Contents Foreword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii Foreword to the First Edition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv Part I. Tour A Guided Tour of Windows PowerShell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Part II. Fundamentals 1. The Windows PowerShell Interactive Shell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 1.1 Run Programs, Scripts, and Existing Tools 19 1.2 Resolve Errors Calling Native Executables 21 1.3 Run a PowerShell Command 23 1.4 Invoke a Long-Running or Background Command 24 1.5 Notify Yourself of Job Completion 27 1.6 Customize Your Shell, Profile, and Prompt 28 1.7 Find a Command to Accomplish a Task 31 1.8 Get Help on a Command 32 1.9 Program: Search Help for Text 34 1.10 Program: View PowerShell’s HTML Help 36 1.11 Launch PowerShell at a Specific Location 37 1.12 Invoke a PowerShell Command or Script from Outside PowerShell 38 1.13 Customize the Shell to Improve Your Productivity 40 1.14 Program: Learn Aliases for Common Commands 42 1.15 Program: Learn Aliases for Common Parameters 44 1.16 Access and Manage Your Console History 46 1.17 Program: Create Scripts from Session History 48 v www.it-ebooks.info 1.18 Invoke a Command from Your Session History 49 1.19 Program: Search Formatted Output for a Pattern 51 1.20 Interactively View and Process Command Output 52 1.21 Store the Output of a Command into a File 54 1.22 Add Information to the End of a File 55 1.23 Record a Transcript of Your Shell Session 55 1.24 Extend Your Shell with Additional Commands 56 1.25 Use Commands from Customized Shells 57 1.26 Save State Between Sessions 59 2. Pipelines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 2.1 Filter Items in a List or Command Output 64 2.2 Group and Pivot Data by Name 65 2.3 Program: Simplify Most Where-Object Filters 68 2.4 Program: Interactively Filter Lists of Objects 70 2.5 Work with Each Item in a List or Command Output 72 2.6 Automate Data-Intensive Tasks 74 2.7 Program: Simplify Most Foreach-Object Pipelines 78 2.8 Intercept Stages of the Pipeline 80 2.9 Automatically Capture Pipeline Output 81 2.10 Capture and Redirect Binary Process Output 83 3. Variables and Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 3.1 Display the Properties of an Item as a List 90 3.2 Display the Properties of an Item as a Table 92 3.3 Store Information in Variables 94 3.4 Access Environment Variables 95 3.5 Program: Retain Changes to Environment Variables Set by a Batch File 98 3.6 Control Access and Scope of Variables and Other Items 100 3.7 Program: Create a Dynamic Variable 102 3.8 Work with .NET Objects 104 3.9 Create an Instance of a .NET Object 108 3.10 Program: Create Instances of Generic Objects 110 3.11 Reduce Typing for Long Class Names 113 3.12 Use a COM Object 115 3.13 Learn About Types and Objects 115 3.14 Get Detailed Documentation About Types and Objects 117 3.15 Add Custom Methods and Properties to Objects 119 3.16 Create and Initialize Custom Objects 121 3.17 Add Custom Methods and Properties to Types 125 vi | Table of Contents www.it-ebooks.info 4. Looping and Flow Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131 4.1 Make Decisions with Comparison and Logical Operators 131 4.2 Adjust Script Flow Using Conditional Statements 133 4.3 Manage Large Conditional Statements with Switches 135 4.4 Repeat Operations with Loops 136 4.5 Add a Pause or Delay 139 5. Strings and Unstructured Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141 5.1 Create a String 141 5.2 Create a Multiline or Formatted String 143 5.3 Place Special Characters in a String 144 5.4 Insert Dynamic Information in a String 144 5.5 Prevent a String from Including Dynamic Information 146 5.6 Place Formatted Information in a String 147 5.7 Search a String for Text or a Pattern 148 5.8 Replace Text in a String 151 5.9 Split a String on Text or a Pattern 152 5.10 Combine Strings into a Larger String 154 5.11 Convert a String to Upper/Lowercase 156 5.12 Trim a String 157 5.13 Format a Date for Output 158 5.14 Program: Convert Text Streams to Objects 160 5.15 Generate Large Reports and Text Streams 164 5.16 Generate Source Code and Other Repetitive Text 166 6. Calculations and Math . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171 6.1 Perform Simple Arithmetic 171 6.2 Perform Complex Arithmetic 173 6.3 Measure Statistical Properties of a List 175 6.4 Work with Numbers as Binary 177 6.5 Simplify Math with Administrative Constants 180 6.6 Convert Numbers Between Bases 181 7. Lists, Arrays, and Hashtables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183 7.1 Create an Array or List of Items 183 7.2 Create a Jagged or Multidimensional Array 185 7.3 Access Elements of an Array 186 7.4 Visit Each Element of an Array 188 7.5 Sort an Array or List of Items 189 7.6 Determine Whether an Array Contains an Item 190 7.7 Combine Two Arrays 191 7.8 Find Items in an Array That Match a Value 192 7.9 Compare Two Lists 193 Table of Contents | vii www.it-ebooks.info 7.10 Remove Elements from an Array 193 7.11 Find Items in an Array Greater or Less Than a Value 194 7.12 Use the ArrayList Class for Advanced Array Tasks 195 7.13 Create a Hashtable or Associative Array 197 7.14 Sort a Hashtable by Key or Value 198 8. Utility Tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201 8.1 Get the System Date and Time 201 8.2 Measure the Duration of a Command 202 8.3 Read and Write from the Windows Clipboard 203 8.4 Generate a Random Number or Object 206 8.5 Program: Search the Windows Start Menu 208 8.6 Program: Show Colorized Script Content 209 Part III. Common Tasks 9. Simple Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217 9.1 Get the Content of a File 217 9.2 Search a File for Text or a Pattern 219 9.3 Parse and Manage Text-Based Logfiles 222 9.4 Parse and Manage Binary Files 224 9.5 Create a Temporary File 227 9.6 Search and Replace Text in a File 228 9.7 Program: Get the Encoding of a File 231 9.8 Program: View the Hexadecimal Representation of Content 233 10. Structured Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237 10.1 Access Information in an XML File 237 10.2 Perform an XPath Query Against XML 240 10.3 Convert Objects to XML 242 10.4 Modify Data in an XML File 243 10.5 Easily Import and Export Your Structured Data 245 10.6 Store the Output of a Command in a CSV or Delimited File 247 10.7 Import CSV and Delimited Data from a File 248 10.8 Use Excel to Manage Command Output 249 10.9 Parse and Interpret PowerShell Scripts 251 11. Code Reuse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255 11.1 Write a Script 255 11.2 Write a Function 258 11.3 Find a Verb Appropriate for a Command Name 260 11.4 Write a Script Block 261 viii | Table of Contents www.it-ebooks.info [...]... about Windows PowerShell He sees the revolutionary changes introduced with the 2.0 release of the product If Windows PowerShell 1.0 was the concept, Windows PowerShell 2.0 is the answer If Windows PowerShell 1.0 was the vision, Windows PowerShell 2.0 is the reality If Windows PowerShell 1.0 was for early adopters, Windows PowerShell 2.0 is moving into the mainstream With the inclusion of Windows PowerShell. .. there are more than 200 Windows PowerShell projects on CodePlex One person presenting at SQL Saturday declared that the active Windows PowerShell community was one of the great strengths of Windows PowerShell You are not alone when it comes to learning and implementing Windows PowerShell I do not own every Windows PowerShell book ever written I have probably looked at most Windows PowerShell books, but... uses PowerShell s standard verbs What You Need to Use This Book The majority of this book requires only a working installation of Windows PowerShell Windows 7 and Windows Server 2008 R2 include Windows PowerShell by default If you do not yet have PowerShell installed, you may obtain it by following the download link at http://www.microsoft.com /PowerShell This link provides download instructions for PowerShell. .. advanced functionality, but you can be productive in PowerShell immediately To launch Windows PowerShell, do one of the following: • Click Start→All Programs→Accessories Windows PowerShell • Click Start→Run, and then type PowerShell A PowerShell prompt window opens that’s nearly identical to the traditional command prompt window of Windows XP, Windows Server 2003, and their many ancestors The PS... Certification Examinations, and as a network administrator or a consultant, you will need to learn Windows PowerShell Learning Windows PowerShell need not be tedious, boring, or exhausting In fact, you will be joining a community that is at least as passionate about Windows PowerShell as Lee (or the rest of the Windows PowerShell team) or me (I write the Hey, Scripting Guy! blog seven days a week—the only Microsoft... read about is the Windows PowerShell Integrated Scripting Environment (ISE), in Chapter 19 A common request for years was for Microsoft to write a script editor For years, I have been telling people we did write a script editor— Notepad The Windows PowerShell ISE is much better than Notepad Not only is the Windows PowerShell ISE a great script editor in its own right, but the Windows PowerShell team... its provider model, PowerShell lets you manage data stores using the same techniques you already use to manage files and folders We’ll explore each of these pillars in this introductory tour of PowerShell If you are running Windows 7 or Windows 2008 R2, version two of PowerShell is already installed If not, visit the download link at http://www.microsoft.com /PowerShell to install it PowerShell and its... that is a good thing, because his book is not a typical scripting book; it is a cookbook Just like a “real cookbook that contains recipes for meals, this fascinating volume is what I find myself thumbing through when I am hungry to try something new with Windows PowerShell In reviewing Lee’s upgraded Windows PowerShell Cookbook, I see that I will not be placed on a diet of “foo” and “bar”; instead,... really need is a scripting environment that includes the FCL as an enabler That’s exactly what Windows PowerShell does In fact, Windows PowerShell runs in the same environment as that library and, as a result, works seamlessly with it I read a lot of press about the object-pipelining capabilities of Windows PowerShell Those capabilities are very cool and represent an excellent advance in the glue department—an... from writing a script and consulting documentation Not so when working with objects in Windows PowerShell Type this at a Windows PowerShell prompt: $objShell = New-Object -com Shell.Application $objShell | Get-Member It does a scripter good, does it not? That Lee Guy Hopefully my rambling has convinced you that Windows PowerShell is a good thing and that it’s worth your time to learn it Now, why do I think . www.it-ebooks.info www.it-ebooks.info Windows PowerShell Cookbook www.it-ebooks.info www.it-ebooks.info SECOND EDITION Windows PowerShell Cookbook Lee Holmes Beijing • Cambridge • Farnham • Köln • Sebastopol • Taipei • Tokyo www.it-ebooks.info Windows. about Windows PowerShell. He sees the revolutionary changes introduced with the 2.0 release of the product. If Windows PowerShell 1.0 was the concept, Windows PowerShell 2.0 is the answer. If Windows. PowerShell 2.0 is the answer. If Windows PowerShell 1.0 was the vision, Windows PowerShell 2.0 is the reality. If Windows PowerShell 1.0 was for early adopters, Windows PowerShell 2.0 is moving into the

Ngày đăng: 03/05/2014, 20:18

TỪ KHÓA LIÊN QUAN