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

Windows Powershell in Action, 2nd Edition potx

1K 2.6K 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

  • WindowsPowerShell

  • brief contents

  • contents

  • preface

  • acknowledgments

  • about this book

    • Who should read this book?

    • Roadmap

    • Code conventions

    • Source code downloads

    • Author Online

    • About the author

    • About the title

  • about the cover illustration

  • PART 1 Learning PowerShell

    • 1 Welcome to PowerShell

      • 1.1 What is PowerShell?

        • 1.1.1 Shells, command lines, and scripting languages

        • 1.1.2 Why a new shell? Why now?

        • 1.1.3 The last mile problem

      • 1.2 Soul of a new language

        • 1.2.1 Learning from history

        • 1.2.2 Leveraging .NET

      • 1.3 Brushing up on objects

        • 1.3.1 Reviewing object-oriented programming

        • 1.3.2 Objects in PowerShell

      • 1.4 Up and running with PowerShell

        • 1.4.1 PowerShell

        • 1.4.2 Starting PowerShell

        • 1.4.3 The PowerShell console host

        • 1.4.4 The PowerShell Integrated Scripting Environment

        • 1.4.5 Command completion

      • 1.5 Dude! Where’s my code?

        • 1.5.1 Navigation and basic operations

        • 1.5.2 Basic expressions and variables

        • 1.5.3 Processing data

        • 1.5.4 Flow-control statements

        • 1.5.5 Scripts and functions

        • 1.5.6 Remoting and the Universal Execution Model

      • 1.6 Summary

    • 2 Foundations of PowerShell

      • 2.1 Getting a sense of the PowerShell language

      • 2.2 The core concepts

        • 2.2.1 Command concepts and terminology

        • 2.2.2 Commands and cmdlets

        • 2.2.3 Command categories

      • 2.3 Aliases and elastic syntax

      • 2.4 Parsing and PowerShell

        • 2.4.1 How PowerShell parses

        • 2.4.2 Quoting

        • 2.4.3 Expression-mode and command-mode parsing

        • 2.4.4 Statement termination

        • 2.4.5 Comment syntax in PowerShell

      • 2.5 How the pipeline works

        • 2.5.1 Pipelines and streaming behavior

        • 2.5.2 Parameters and parameter binding

      • 2.6 Formatting and output

        • 2.6.1 The formatting cmdlets

        • 2.6.2 The outputter cmdlets

      • 2.7 Summary

    • 3 Working with types

      • 3.1 Type management in the wild, wild West

        • 3.1.1 PowerShell: a type-promiscuous language

        • 3.1.2 The type system and type adaptation

      • 3.2 Basic types and literals

        • 3.2.1 String literals

        • 3.2.2 Numbers and numeric literals

      • 3.3 Collections: dictionaries and hashtables

        • 3.3.1 Creating and inspecting hashtables

        • 3.3.2 Modifying and manipulating hashtables

        • 3.3.3 Hashtables as reference types

      • 3.4 Collections: arrays and sequences

        • 3.4.1 Collecting pipeline output as an array

        • 3.4.2 Array indexing

        • 3.4.3 Polymorphism in arrays

        • 3.4.4 Arrays as reference types

        • 3.4.5 Singleton arrays and empty arrays

      • 3.5 Type literals

        • 3.5.1 Type name aliases

        • 3.5.2 Generic type literals

        • 3.5.3 Accessing static members with type literals

      • 3.6 Type conversions

        • 3.6.1 How type conversion works

        • 3.6.2 PowerShell’s type-conversion algorithm

        • 3.6.3 Special type conversions in parameter binding

      • 3.7 Summary

    • 4 Operators and expressions

      • 4.1 Arithmetic operators

        • 4.1.1 The addition operator

        • 4.1.2 The multiplication operator

        • 4.1.3 Subtraction, division, and the modulus operator

      • 4.2 The assignment operators

        • 4.2.1 Multiple assignments

        • 4.2.2 Multiple assignments with type qualifiers

        • 4.2.3 Assignment operations as value expressions

      • 4.3 Comparison operators

        • 4.3.1 Scalar comparisons

        • 4.3.2 Comparisons and case sensitivity

        • 4.3.3 Using comparison operators with collections

      • 4.4 Pattern matching and text manipulation

        • 4.4.1 Wildcard patterns and the -like operator

        • 4.4.2 Regular expressions

        • 4.4.3 The -match operator

        • 4.4.4 The -replace operator

        • 4.4.5 The -join operator

        • 4.4.6 The -split operator

      • 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 and subexpressions

        • 5.3.1 Subexpressions $( ... )

        • 5.3.2 Array subexpressions @( ... )

      • 5.4 Array operators

        • 5.4.1 The comma operator

        • 5.4.2 The range operator

        • 5.4.3 Array indexing and slicing

        • 5.4.4 Using the range operator with arrays

        • 5.4.5 Working with multidimensional arrays

      • 5.5 Property and method operators

        • 5.5.1 The dot operator

        • 5.5.2 Static methods and the double-colon operator

        • 5.5.3 Indirect method invocation

      • 5.6 The format operator

      • 5.7 Redirection and the redirection operators

      • 5.8 Working with variables

        • 5.8.1 Creating variables

        • 5.8.2 Variable name syntax

        • 5.8.3 Working with the variable cmdlets

        • 5.8.4 Splatting a variable

      • 5.9 Summary

    • 6 Flow control in scripts

      • 6.1 The conditional statement

      • 6.2 Looping statements

        • 6.2.1 The while loop

        • 6.2.2 The do-while loop

        • 6.2.3 The for loop

        • 6.2.4 The foreach loop

      • 6.3 Labels, break, and continue

      • 6.4 The switch statement

        • 6.4.1 Basic use of the switch statement

        • 6.4.2 Using wildcard patterns with the switch statement

        • 6.4.3 Using regular expressions with the switch statement

        • 6.4.4 Processing files with the switch statement

        • 6.4.5 Using the $switch loop enumerator in the switch statement

      • 6.5 Flow control using cmdlets

        • 6.5.1 The ForEach-Object cmdlet

        • 6.5.2 The Where-Object cmdlet

      • 6.6 Statements as values

      • 6.7 A word about performance

      • 6.8 Summary

    • 7 PowerShell functions

      • 7.1 Fundamentals of PowerShell functions

        • 7.1.1 Passing arguments using $args

        • 7.1.2 Example functions: ql and qs

        • 7.1.3 Simplifying $args processing with multiple assignment

      • 7.2 Declaring formal parameters for a function

        • 7.2.1 Mixing named and positional parameters

        • 7.2.2 Adding type constraints to parameters

        • 7.2.3 Handling variable numbers of arguments

        • 7.2.4 Initializing function parameters with default values

        • 7.2.5 Handling mandatory parameters, v1-style

        • 7.2.6 Using switch parameters to define command switches

        • 7.2.7 Switch parameters vs. Boolean parameters

      • 7.3 Returning values from functions

        • 7.3.1 Debugging problems in function output

        • 7.3.2 The return statement

      • 7.4 Using simple functions in a pipeline

        • 7.4.1 Filters and functions

        • 7.4.2 Functions with begin, process, and end blocks

      • 7.5 Managing function definitions in a session

      • 7.6 Variable scoping in functions

        • 7.6.1 Declaring variables

        • 7.6.2 Using variable scope modifiers

      • 7.7 Summary

    • 8 Advanced functions and scripts

      • 8.1 PowerShell scripts

        • 8.1.1 Script execution policy

        • 8.1.2 Passing arguments to scripts

        • 8.1.3 Exiting scripts and the exit statement

        • 8.1.4 Scopes and scripts

        • 8.1.5 Managing your scripts

        • 8.1.6 Running PowerShell scripts from other applications

      • 8.2 Writing advanced functions and scripts

        • 8.2.1 Specifying script and function attributes

        • 8.2.2 The CmdletBinding attribute

        • 8.2.3 The OutputType attribute

        • 8.2.4 Specifying parameter attributes

        • 8.2.5 Creating parameter aliases with the Alias attribute

        • 8.2.6 Parameter validation attributes

      • 8.3 Dynamic parameters and dynamicParam

        • 8.3.1 Steps for adding a dynamic parameter

        • 8.3.2 When should dynamic parameters be used?

      • 8.4 Documenting functions and scripts

        • 8.4.1 Automatically generated help fields

        • 8.4.2 Creating manual help content

        • 8.4.3 Comment-based help

        • 8.4.4 Tags used in documentation comments

      • 8.5 Summary

    • 9 Using and authoring modules

      • 9.1 The role of a module system

        • 9.1.1 Module roles in PowerShell

        • 9.1.2 Module mashups: composing an application

      • 9.2 Module basics

        • 9.2.1 Module terminology

        • 9.2.2 Modules are single-instance objects

      • 9.3 Working with modules

        • 9.3.1 Finding modules on the system

        • 9.3.2 Loading a module

        • 9.3.3 Removing a loaded module

      • 9.4 Writing script modules

        • 9.4.1 A quick review of scripts

        • 9.4.2 Turning a script into a module

        • 9.4.3 Controlling member visibility with Export-ModuleMember

        • 9.4.4 Installing a module

        • 9.4.5 How scopes work in script modules

        • 9.4.6 Nested modules

      • 9.5 Binary modules

        • 9.5.1 Binary modules vs. snap-ins

        • 9.5.2 Creating a binary module

        • 9.5.3 Nesting binary modules in script modules

      • 9.6 Summary

    • 10 Module manifests and metadata

      • 10.1 Module folder structure

      • 10.2 Module manifest structure

      • 10.3 Production manifest elements

        • 10.3.1 Module identity

        • 10.3.2 Runtime dependencies

      • 10.4 Construction manifest elements

        • 10.4.1 The loader manifest elements

        • 10.4.2 Module component load order

      • 10.5 Content manifest elements

      • 10.6 Language restrictions in a manifest

      • 10.7 Advanced module operations

        • 10.7.1 The PSModuleInfo object

        • 10.7.2 Using the PSModuleInfo methods

        • 10.7.3 The defining module versus the calling module

        • 10.7.4 Setting module properties from inside a script module

        • 10.7.5 Controlling when modules can be unloaded

        • 10.7.6 Running an action when a module is removed

      • 10.8 Summary

    • 11 Metaprogramming with scriptblocks and dynamic code

      • 11.1 Scriptblock basics

        • 11.1.1 Invoking commands

        • 11.1.2 The scriptblock literal

        • 11.1.3 Defining functions at runtime

      • 11.2 Building and manipulating objects

        • 11.2.1 Looking at members

        • 11.2.2 Using Add-Member to extend objects

        • 11.2.3 Adding note properties with New-Object

      • 11.3 Using the Select-Object cmdlet

      • 11.4 Dynamic modules

        • 11.4.1 Dynamic script modules

        • 11.4.2 Closures in PowerShell

        • 11.4.3 Creating custom objects from modules

      • 11.5 Steppable pipelines

        • 11.5.1 How steppable pipelines work

        • 11.5.2 Creating a proxy command with steppable pipelines

      • 11.6 A closer look at the type-system plumbing

        • 11.6.1 Adding a property

        • 11.6.2 Shadowing an existing property

      • 11.7 Extending the PowerShell language

        • 11.7.1 Little languages

        • 11.7.2 Adding a CustomClass keyword to PowerShell

        • 11.7.3 Type extension

      • 11.8 Building script code at runtime

        • 11.8.1 The Invoke-Expression cmdlet

        • 11.8.2 The ExecutionContext variable

        • 11.8.3 The ExpandString() method

        • 11.8.4 The InvokeScript() method

        • 11.8.5 Mechanisms for creating scriptblocks

        • 11.8.6 Creating functions using the function: drive

      • 11.9 Compiling code with Add-Type

        • 11.9.1 Defining a new .NET class: C#

        • 11.9.2 Defining a new enum at runtime

        • 11.9.3 Dynamic binary modules

      • 11.10 Summary

    • 12 Remoting and background jobs

      • 12.1 Getting started with remoting

        • 12.1.1 Commands with built-in remoting

        • 12.1.2 The PowerShell remoting subsystem

        • 12.1.3 Enabling remoting

        • 12.1.4 Additional setup steps for workgroup environments

        • 12.1.5 Enabling remoting in the enterprise

      • 12.2 Applying PowerShell remoting

        • 12.2.1 Basic remoting examples

        • 12.2.2 Adding concurrency to the examples

        • 12.2.3 Solving a real problem: multimachine monitoring

      • 12.3 Sessions and persistent connections

        • 12.3.1 Additional session attributes

        • 12.3.2 Using the New-PSSession cmdlet

        • 12.3.3 Interactive sessions

        • 12.3.4 Managing PowerShell sessions

      • 12.4 Implicit remoting

        • 12.4.1 Using implicit remoting

        • 12.4.2 How implicit remoting works

      • 12.5 Background jobs in PowerShell

        • 12.5.1 The job commands

        • 12.5.2 Working with the job cmdlets

        • 12.5.3 Working with multiple jobs

        • 12.5.4 Starting jobs on remote computers

        • 12.5.5 Running jobs in existing sessions

      • 12.6 Considerations when running commands remotely

        • 12.6.1 Remote session startup directory

        • 12.6.2 Profiles and remoting

        • 12.6.3 Issues running executables remotely

        • 12.6.4 Reading and writing to the console

        • 12.6.5 Remote output vs. local output

        • 12.6.6 Processor architecture issues

      • 12.7 Summary

    • 13 Remoting: configuring applications and services

      • 13.1 Remoting infrastructure in depth

        • 13.1.1 The PowerShell remoting protocol stack

        • 13.1.2 Using the WSMan cmdlets and providers

        • 13.1.3 Authenticating the target computer

        • 13.1.4 Authenticating the connecting user

        • 13.1.5 Addressing the remoting target

        • 13.1.6 Windows version-specific connection issues

        • 13.1.7 Managing resource consumption

      • 13.2 Building custom remoting services

        • 13.2.1 Remote service connection patterns

        • 13.2.2 Working with custom configurations

        • 13.2.3 Creating a custom configuration

        • 13.2.4 Access controls and endpoints

        • 13.2.5 Constraining a PowerShell session

        • 13.2.6 Creating a constrained execution environment

      • 13.3 Summary

    • 14 Errors and exceptions

      • 14.1 Error handling

        • 14.1.1 ErrorRecords and the error stream

        • 14.1.2 The $error variable and –ErrorVariable parameter

        • 14.1.3 Determining if a command had an error

        • 14.1.4 Controlling the actions taken on an error

      • 14.2 Dealing with errors that terminate execution

        • 14.2.1 The trap statement

        • 14.2.2 The try/catch/finally statement

        • 14.2.3 The throw statement

      • 14.3 Debugging with the host APIs

        • 14.3.1 Catching errors with strict mode

        • 14.3.2 The Set-StrictMode cmdlet in PowerShell v2

        • 14.3.3 Static analysis of scripts

      • 14.4 Capturing session output

        • 14.4.1 Starting the transcript

        • 14.4.2 What gets captured in the transcript

      • 14.5 PowerShell and the event log

        • 14.5.1 The EventLog cmdlets

        • 14.5.2 Examining the PowerShell event log

      • 14.6 Summary

    • 15 The PowerShell ISE and debugger

      • 15.1 The PowerShell ISE

        • 15.1.1 Controlling the ISE pane layout

        • 15.1.2 Using the ISE editor

        • 15.1.3 Executing commands in the ISE

        • 15.1.4 Considerations when running scripts in the ISE

      • 15.2 Using multiple PowerShell tabs

        • 15.2.1 Local in-memory session tabs

        • 15.2.2 Remote session tabs in PowerShell ISE

      • 15.3 Extending the ISE

        • 15.3.1 The $psISE variable

        • 15.3.2 Using the Options property

        • 15.3.3 Managing tabs and files

        • 15.3.4 Working with text panes

        • 15.3.5 Adding a custom menu

      • 15.4 PowerShell script debugging features

        • 15.4.1 The Set-PSDebug cmdlet

        • 15.4.2 Nested prompts and the Suspend operation

      • 15.5 The PowerShell v2 debugger

        • 15.5.1 The graphical debugger

      • 15.6 Command-line debugging

        • 15.6.1 Working with breakpoint objects

        • 15.6.2 Setting breakpoints on commands

        • 15.6.3 Setting breakpoints on variable assignment

        • 15.6.4 Debugger limitations and issues

      • 15.7 Summary

  • PART 2 Using PowerShell

    • 16 Working with files, text, and XML

      • 16.1 PowerShell and paths

        • 16.1.1 Providers and the core cmdlets

        • 16.1.2 Working with PSDrives

        • 16.1.3 Working with paths that contain wildcards

        • 16.1.4 Suppressing wildcard processing in paths

        • 16.1.5 The -LiteralPath parameter

        • 16.1.6 The Registry provider

      • 16.2 File processing

        • 16.2.1 Reading and writing files

        • 16.2.2 Writing files

        • 16.2.3 All together now—reading and writing

        • 16.2.4 Performance caveats with Get-Content

      • 16.3 Processing unstructured text

        • 16.3.1 Using System.String to work with text

        • 16.3.2 Using hashtables to count unique words

        • 16.3.3 Using regular expressions to manipulate text

        • 16.3.4 Searching files with the Select-String cmdlet

      • 16.4 XML structured text processing

        • 16.4.1 Using XML as objects

        • 16.4.2 Adding elements to an XML object

        • 16.4.3 Loading and saving XML files

        • 16.4.4 Processing XML documents in a pipeline

        • 16.4.5 Processing XML with XPath

        • 16.4.6 A hint of XLinq

        • 16.4.7 Rendering objects as XML

      • 16.5 Summary

    • 17 Extending your reach with .NET

      • 17.1 Using .NET from PowerShell

        • 17.1.1 .NET basics

        • 17.1.2 Working with assemblies

        • 17.1.3 Finding types

        • 17.1.4 Creating instances of types

        • 17.1.5 Defining new types with Add-Type

        • 17.1.6 Working with generic types

      • 17.2 PowerShell and the internet

        • 17.2.1 Retrieving a web page

        • 17.2.2 Processing an RSS feed

      • 17.3 PowerShell and graphical user interfaces

        • 17.3.1 PowerShell and WinForms

        • 17.3.2 Creating a winforms module

        • 17.3.3 PowerShell and Windows Presentation Foundation

      • 17.4 Summary

    • 18 Working with COM

      • 18.1 Working with COM in PowerShell

        • 18.1.1 Creating COM objects

        • 18.1.2 Identifying and locating COM classes

      • 18.2 Automating Windows with COM

        • 18.2.1 Exploring with the Shell.Application class

        • 18.2.2 Managing browser windows using COM

        • 18.2.3 A browser window management module

      • 18.3 Working with the WScript.Shell class

      • 18.4 Using COM to manage applications

        • 18.4.1 Looking up a word using Internet Explorer

        • 18.4.2 Using Microsoft Word to do spell checking

      • 18.5 The WSH ScriptControl class

        • 18.5.1 Embedding VBScript code in a PowerShell script

        • 18.5.2 Embedding JScript code in a PowerShell script

      • 18.6 Working with the Windows Task Scheduler

        • 18.6.1 Getting started with the Schedule.Service class

        • 18.6.2 Listing running tasks

        • 18.6.3 Creating a new scheduled task

        • 18.6.4 Credentials and scheduled tasks

        • 18.6.5 Viewing the life cycle of a task

      • 18.7 Issues with COM

        • 18.7.1 64-bit vs. 32-bit issues

        • 18.7.2 Threading model problems

        • 18.7.3 Interop assemblies, wrappers, and typelibs

      • 18.8 Summary

    • 19 Management objects: WMI and WS-MAN

      • 19.1 Working with WMI in PowerShell

        • 19.1.1 Exploring WMI

        • 19.1.2 The WMI infrastructure

      • 19.2 The WMI cmdlets

        • 19.2.1 The WMI cmdlet common parameters

        • 19.2.2 The Get-WmiObject cmdlet

        • 19.2.3 The Set-WmiInstance cmdlet

        • 19.2.4 The Invoke-WmiMethod cmdlet

        • 19.2.5 The Remove-WmiObject cmdlet

      • 19.3 The WMI object adapter

        • 19.3.1 The WMI type accelerators

        • 19.3.2 Putting modified WMI objects back

      • 19.4 Exploring WS-Man

        • 19.4.1 The WS-Man cmdlets

        • 19.4.2 Using Get-WSManInstance to retrieve management data

        • 19.4.3 Updating resources using Set-WSManInstance

        • 19.4.4 Invoking methods with Invoke-WSManAction

      • 19.5 Summary

    • 20 Responding in real time with eventing

      • 20.1 Foundations of event handling

      • 20.2 Synchronous events

        • 20.2.1 Synchronous eventing in GUIs

        • 20.2.2 Delegates and delegation

      • 20.3 Asynchronous events

        • 20.3.1 Subscriptions, registrations, and actions

        • 20.3.2 The eventing cmdlets

      • 20.4 Working with asynchronous .NET events

        • 20.4.1 Writing a timer event handler

        • 20.4.2 Managing event subscriptions

      • 20.5 Asynchronous event handling with scriptblocks

        • 20.5.1 Automatic variables in the event handler

        • 20.5.2 Dynamic modules and event handler state

      • 20.6 Queued events and the Wait-Event cmdlet

      • 20.7 Working with WMI events

        • 20.7.1 WMI event basics

        • 20.7.2 Class-based WMI event registration

        • 20.7.3 Query-based WMI event registrations

      • 20.8 Engine events

        • 20.8.1 Predefined engine events

        • 20.8.2 Generating events in functions and scripts

      • 20.9 Remoting and event forwarding

        • 20.9.1 Handling remote EventLog events

        • 20.9.2 Serialization issues with remote events

      • 20.10 How eventing works

      • 20.11 Summary

    • 21 Security, security, security

      • 21.1 Introduction to security

        • 21.1.1 What security is and what it isn’t

        • 21.1.2 Security: perception and reality

      • 21.2 Security modeling

        • 21.2.1 Introduction to threat modeling

        • 21.2.2 Classifying threats using the STRIDE model

        • 21.2.3 Security basics: threats, assets, and mitigations

      • 21.3 Securing the PowerShell environment

        • 21.3.1 Secure by default

        • 21.3.2 Enabling scripting with execution policy

      • 21.4 Signing scripts

        • 21.4.1 How public key encryption and one-way hashing work

        • 21.4.2 Signing authorities and certificates

        • 21.4.3 Self-signed certificates

        • 21.4.4 Using a certificate to sign a script

        • 21.4.5 Enabling strong private key protection

        • 21.4.6 Using the PFX file to sign a file

      • 21.5 Writing secure scripts

      • 21.6 Using the SecureString class

        • 21.6.1 Creating a SecureString object

        • 21.6.2 The SecureString cmdlets

        • 21.6.3 Working with credentials

        • 21.6.4 Avoiding Invoke-Expression

      • 21.7 Summary

  • index

    • Symbols

    • Numerics

    • A

    • B

    • C

    • D

    • E

    • F

    • G

    • H

    • I

    • J

    • K

    • L

    • M

    • N

    • O

    • P

    • Q

    • R

    • S

    • T

    • U

    • V

    • W

    • X

    • Z

  • WindowsPS-back

Nội dung

MANNING Bruce Payette SECOND EDITION Covers PowerShell v2 www.it-ebooks.info PRAISE FOR THE FIRST EDITION The book on PowerShell, it has all the secrets. —James Truher, PowerShell Program Manager, Microsoft If all it had going for it was the authoritative pedigree of the writer, it might be worth it, but it’s also well-written, well-organized, and thorough, which I think makes it invaluable as both a learning tool and a reference. —Slashdot.org an encyclopedic tome of PowerShell scripting bringing the reader through the basics with simple shell scripts through powerful and flexible scripts any Windows systems administrator will find immediately useful. —ArsGeek.com The nuances of PowerShell from the lead language designer himself! Excellent content and easy read- ability! —Keith Hill, Software Architect [It gives you] inside information, excellent examples, and a colorful writing style. —Marc van Orsouw (MOW), PowerShell MVP There’s no better way to learn PowerShell than from someone on the core PowerShell team—and that’s exactly what you get with this book. —Joe Topjian, adminspotting.net Where’s the 6 stars option? I haven’t enjoyed a software engineering book to the same extent for a long time. —T. Kirby Green, Technical Architect, SunGard Consider this book the definitive reference for PowerShell. As one of the designers of the PowerShell environment, the author knows all the ins and outs, back alleys, hidden rooms, and secret handshakes the language offers, and isn’t afraid to grab you by the hand and drag you along (like it or not!) for the tour of your life. —Jase T. Wolfe, Amazon reader I love this book! —Scott Hanselman ComputerZen.com www.it-ebooks.info www.it-ebooks.info Windows PowerShell in Action, Second Edition BRUCE PAYETTE MANNING Shelter Island www.it-ebooks.info For online information and ordering of this and other Manning books, please visit www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact: Special Sales Department Manning Publications Co. 20 Baldwin Road PO Box 261 Shelter Island, NY 11964 Email: orders@manning.com ©2011 by Manning Publications Co. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps. Recognizing also our responsibility to conserve the resources of our planet, Manning books are printed on paper that is at least 15 percent recycled and processed without the use of elemental chlorine Manning Publications Co. Development Editor Cynthia Kane 20 Baldwin Road Copyeditor: Liz Welch PO Box 261 Typ eset ter: Marija Tudor Shelter Island, NY 11964 Cover designer: Marija Tudor ISBN 9781935182139 Printed in the United States of America 1 2 3 4 5 6 7 8 9 10 – MAL – 16 15 14 13 12 11 www.it-ebooks.info v brief contents Part 1 Learning PowerShell 1 1 Welcome to PowerShell 3 2 Foundations of PowerShell 36 3 Working with types 72 4 Operators and expressions 110 5 Advanced operators and variables 151 6 Flow control in scripts 198 7 PowerShell functions 236 8 Advanced functions and scripts 275 9 Using and authoring modules 322 10 Module manifests and metadata 361 11 Metaprogramming with scriptblocks and dynamic code 392 12 Remoting and background jobs 447 13 Remoting: configuring applications and services 502 14 Errors and exceptions 553 15 The PowerShell ISE and debugger 606 www.it-ebooks.info vi BRIEF CONTENTS Part 2 Using PowerShell 661 16 Working with files, text, and XML 663 17 Extending your reach with .NET 719 18 Working with COM 760 19 Management objects: WMI and WS-MAN 797 20 Responding in real time with eventing 847 21 Security, security, security 888 www.it-ebooks.info vii contents preface xix acknowledgments xxi about this book xxiii about the cover illustration xxix Part 1 Learning PowerShell 1 1 Welcome to PowerShell 3 1.1 What is PowerShell? 5 Shells, command lines, and scripting languages 6 ✦ Why a new shell? Why now? 7 ✦ The last mile problem 8 1.2 Soul of a new language 9 Learning from history 9 ✦ Leveraging .NET 10 1.3 Brushing up on objects 11 Reviewing object-oriented programming 11 ✦ Objects in PowerShell 13 1.4 Up and running with PowerShell 13 PowerShell 14 ✦ Starting PowerShell 14 ✦ The PowerShell console host 14 ✦ The PowerShell Integrated Scripting Environment 17 ✦ Command completion 20 1.5 Dude! Where’s my code? 22 Navigation and basic operations 22 ✦ Basic expressions and variables 23 ✦ Processing data 25 ✦ Flow control statements 30 ✦ Scripts and functions 31 ✦ Remoting and the Universal Execution Model 32 1.6 Summary 35 www.it-ebooks.info viii CONTENTS 2 Foundations of PowerShell 36 2.1 Getting a sense of the PowerShell language 37 2.2 The core concepts 38 Command concepts and terminology 38 ✦ Commands and cmdlets 38 ✦ Command categories 42 2.3 Aliases and elastic syntax 46 2.4 Parsing and PowerShell 50 How PowerShell parses 51 ✦ Quoting 51 ✦ Expression- mode and command-mode parsing 54 ✦ Statement termination 56 ✦ Comment syntax in PowerShell 58 2.5 How the pipeline works 60 Pipelines and streaming behavior 61 ✦ Parameters and parameter binding 62 2.6 Formatting and output 64 The formatting cmdlets 64 ✦ The outputter cmdlets 67 2.7 Summary 70 3 Working with types 72 3.1 Type management in the wild, wild West 72 PowerShell: a type-promiscuous language 73 ✦ The type system and type adaptation 75 3.2 Basic types and literals 77 String literals 77 ✦ Numbers and numeric literals 82 3.3 Collections: dictionaries and hash tables 85 Creating and inspecting hash tables 85 ✦ Modifying and manipulating hash tables 88 ✦ Hash tables as reference types 90 3.4 Collections: arrays and sequences 91 Collecting pipeline output as an array 91 ✦ Array indexing 92 ✦ Polymorphism in arrays 92 ✦ Arrays as reference types 93 ✦ Singleton arrays and empty arrays 94 3.5 Type literals 96 Type name aliases 96 ✦ Generic type literals 98 ✦ Accessing static members with type literals 99 3.6 Type conversions 101 How type conversion works 101 ✦ PowerShell’s type- conversion algorithm 104 ✦ Special type conversions in parameter binding 107 3.7 Summary 109 www.it-ebooks.info CONTENTS ix 4 Operators and expressions 110 4.1 Arithmetic operators 112 The addition operator 113 ✦ The multiplication operator 116 Subtraction, division, and the modulus operator 117 4.2 The assignment operators 119 Multiple assignments 120 ✦ Multiple assignments with type qualifiers 121 ✦ Assignment operations as value expressions 123 4.3 Comparison operators 124 Scalar comparisons 125 ✦ Comparisons and case sensitivity 127 ✦ Using comparison operators with collections 129 4.4 Pattern matching and text manipulation 131 Wildcard patterns and the -like operator 132 ✦ Regular expressions 133 ✦ The -match operator 134 ✦ The -replace operator 137 ✦ The -join operator 139 ✦ The -split operator 143 4.5 Logical and bitwise operators 148 4.6 Summary 150 5 Advanced operators and variables 151 5.1 Operators for working with types 152 5.2 The unary operators 154 5.3 Grouping and subexpressions 157 Subexpressions $( ) 159 ✦ Array subexpressions @( ) 160 5.4 Array operators 162 The comma operator 162 ✦ The range operator 165 Array indexing and slicing 167 ✦ Using the range operator with arrays 170 ✦ Working with multidimensional arrays 171 5.5 Property and method operators 173 The dot operator 174 ✦ Static methods and the double-colon operator 177 ✦ Indirect method invocation 178 5.6 The format operator 179 5.7 Redirection and the redirection operators 181 5.8 Working with variables 184 Creating variables 185 ✦ Variable name syntax 186 Working with the variable cmdlets 188 Splatting a variable 193 5.9 Summary 196 www.it-ebooks.info [...]... Working with assemblies 721 ✦ Finding types 725 ✦ Creating instances of types 727 ✦ Defining new types with Add-Type 729 ✦ Working with generic types 739 17.2 PowerShell and the internet 740 Retrieving a web page 740 ✦ Processing an RSS feed 742 17.3 PowerShell and graphical user interfaces 743 PowerShell and WinForms 744 ✦ Creating a winforms module 750 PowerShell and Windows Presentation Foundation... Working with COM 760 18.1 Working with COM in PowerShell 761 Creating COM objects 761 ✦ Identifying and locating COM classes 762 18.2 Automating Windows with COM 764 Exploring with the Shell.Application class 765 ✦ Managing browser windows using COM 767 ✦ A browser window management module 770 18.3 Working with the WScript.Shell class 777 18.4 Using COM to manage applications 779 Looking up a word using... 447 12.1 Getting started with remoting 448 Commands with built -in remoting 448 ✦ The PowerShell remoting subsystem 449 ✦ Enabling remoting 450 Additional setup steps for workgroup environments 451 Enabling remoting in the enterprise 452 12.2 Applying PowerShell remoting 454 Basic remoting examples 454 ✦ Adding concurrency to the examples 455 ✦ Solving a real problem: multimachine monitoring 457 12.3... the PowerShell language itself using these features Chapter 12 introduces PowerShell remoting, starting with basic configuration and setup It then covers the various forms of remoting (interactive and non-interactive) and how to apply these techniques Chapter 13 explores remoting and the underlying protocols in more detail Creation of custom remoting endpoints, including constrained endpoints, is included... 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 17, we look at how we can explore and apply the vast capabilities of the NET framework We cover locating, exploring, and instantiating types in the NET framework, including generic types Then we look at a number of applications using these... “Using PowerShell, ” we shift our focus from individual features towards 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 16 looking at how PowerShell can be used to attack the kind of text processing tasks that have traditionally been the domain of languages like Perl This chapter begins... the principal author of the language implementation He joined Microsoft in 2001 working on Interix, the POSIX subsystem for Windows, then moved to help found the PowerShell project shortly after that 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,... text processing 693 Using XML as objects 693 ✦ Adding elements to an XML object 695 ✦ Loading and saving XML files 697 Processing XML documents in a pipeline 701 ✦ Processing XML with XPath 702 ✦ A hint of XLinq 709 ✦ Rendering objects as XML 711 16.5 Summary 717 xv CONTENTS www.it-ebooks.info 17 Extending your reach with NET 719 17.1 Using NET from PowerShell 720 NET basics 720 ✦ Working with assemblies... www.manning.com/WindowsPowerShellinActionSecondEdition Author Online Purchase of Windows PowerShell in Action, Second Edition includes free access to a private web forum run by Manning Publications where you can make comments about the book, ask technical questions, and receive help from the author and from other users To access the forum and subscribe to it, point your web browser to www manning.com/WindowsPowerShellinActionSecondEdition... Additional PowerShell topics Appendixes are available for download from www.manning.com/WindowsPowerShellinActionSecondEdition xviii CONTENTS www.it-ebooks.info preface Well, it’s been a wild ride since the first edition of this book was released At that time, PowerShell had just shipped and had a fairly limited scope of influence Things have changed a lot PowerShell now ships in the box with Windows (at . ComputerZen.com www.it-ebooks.info www.it-ebooks.info Windows PowerShell in Action, Second Edition BRUCE PAYETTE MANNING Shelter Island www.it-ebooks.info For online information and ordering of this and other Manning. Enabling remoting in the enterprise 452 12.2 Applying PowerShell remoting 454 Basic remoting examples 454 ✦ Adding concurrency to the examples 455 ✦ Solving a real problem: multimachine monitoring. Learning from history 9 ✦ Leveraging .NET 10 1.3 Brushing up on objects 11 Reviewing object-oriented programming 11 ✦ Objects in PowerShell 13 1.4 Up and running with PowerShell 13 PowerShell

Ngày đăng: 29/03/2014, 14:20

TỪ KHÓA LIÊN QUAN