Docsity

Windows PowerShell - Apress, Study Guides, Projects, Research of Windows Programming

Toggle sidebar
bg1
this print for content only—size & color not accurate spine = 0.983" 520 page count
Books for professionals By professionals®
Pro Windows PowerShell
Dear Reader,
I wrote this book as a traveling companion on your journey through the world
of Windows PowerShell. It’s true that PowerShell is easy to learn, and grasping
the basic syntax doesn’t take much effort, so you can immediately be more
productive than you would have been with the plain old cmd.exe. However,
this book takes you much further than the basics, teaching all the tricks and
techniques that make PowerShell unrivaled by other scr ipting languages. The
journey starts with learning your way around the basic shell, and the adventure
continues as you’re introduced the .NET, COM, and WMI infrastructure to help
interoperate with programs as well as achieve tasks not directly supported by
the shell. We finally arrive at our destination as we delve into essential add-ons
that will take you to the next productivity level. I focus on the use of free, open
source tools that make the shell a better product and one that is even more
enjoyable to work with.
PowerShell is an extremely useful product that is still largely undiscovered.
When administrators hear about PowerShell for the first time, many simply
shrug, assuming it’s just another version of the command prompt we inherited
from DOS. I have been able to open the eyes of many of my colleagues and
cohorts to the magic of PowerShell by simply sitting with them and working
together to automate various tasks. The usual reaction is, “Wow, can it really do
that?” This book will hopefully do the same for you—it is my goal to offer you
the eureka moment that will change your professional life forever.
The detailed explanations and the numerous working examples in this
book will help you quickly master each of the topics. The text tries goes beyond
showing tips and techniques to unveil the big picture—how PowerShell doesn’t
just fit into but becomes the basis of your Windows administration toolkit. By
the end of this book, you will be reaching new heights in capability and pro-
ductivity. Meet you at the top!
Hristo Deshev
US $49.99
Shelve in
Windows Administration
User level:
Intermediate–Advanced
Deshev
Windows PowerShell
The eXperT’s Voice® in WindoWs adminisTraTion
Pro
Windows
PowerShell
cyan
maGenTa
yelloW
Black
panTone 123 c
Hristo Deshev
Companion
eBook Available
www.apress.com
SOURCE CODE ONLINE
Companion eBook
See last page for details
on $10 eBook version
ISBN-13: 978-1-59059-940-2
ISBN-10: 1-59059-940-3
9 781590 599402
54999
Pro
RelateD title
Download at Boykma.Com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d
pf5e
pf5f
pf60
pf61
pf62
pf63
pf64

Partial preview of the text

Download Windows PowerShell - Apress and more Study Guides, Projects, Research Windows Programming in PDF only on Docsity!

The eXperT’s Voice®^ in WindoWs adminisTraTion

Pro

Windows

PowerShell

Hristo Deshev

Pro Windows PowerShell Copyright © 2008 by Hristo Deshev All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN-13 (pbk): 978-1-59059-940- ISBN-10 (pbk): 1-59059-940- ISBN-13 (electronic): 978-1-4302-0546- ISBN-10 (electronic): 1-4302-0546- Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. Lead Editor: Tony Campbell Technical Reviewer: Jon Rolfe Editorial Board: Clay Andres, Steve Anglin, Ewan Buckingham, Tony Campbell, Gary Cornell, Jonathan Gennick, Kevin Goff, Matthew Moodie, Joseph Ottinger, Jeffrey Pepper, Frank Pohlmann, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Project Manager: Sofia Marchant Copy Editor: Heather Lang Associate Production Director: Kari Brooks-Copony Production Editor: Laura Cheu Compositor: Susan Glinert Stevens Proofreader: Linda Seifert Indexer: Julie Grady Cover Designer: Kurt Krames Manufacturing Director: Tom Debolski Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail [email protected], or visit http://www.springeronline.com. For information on translations, please contact Apress directly at 2855 Telegraph Avenue, Suite 600, Berkeley, CA 94705. Phone 510-549-5930, fax 510-549-5939, e-mail [email protected], or visit http://www.apress.com. Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at http://www.apress.com/info/bulksales. The information in this book is distributed on an “as is” basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. The source code for this book is available to readers at http://www.apress.com. You will need to answer questions pertaining to this book in order to successfully download the code.

To my wife, Yana, for all her love and support

v

■ CHAPTER 22 PSEventing: .NET Events in PowerShell...................... 449

■ CHAPTER 23 Enhancing Tab Completion with PowerTab................... 461

■INDEX..................................................................... 473

  • ■ CHAPTER 1 Objects and Object Types Introduction xxiii
  • ■ CHAPTER 2 Controlling Execution Flow
  • ■ CHAPTER 3 The Object Pipeline
  • ■ CHAPTER 4 Working with Script Blocks
  • ■ CHAPTER 5 Working with Functions
  • ■ CHAPTER 6 Command Aliases
  • ■ CHAPTER 7 Providers
  • ■ CHAPTER 8 Script Files
  • ■ CHAPTER 9 Error Handling and Debugging
  • ■ CHAPTER 10 Signing Scripts
  • ■ CHAPTER 11 The Shell Environment and Its Configuration
  • ■ CHAPTER 12 Extending the Type System
  • ■ CHAPTER 13 Getting Help
  • ■ CHAPTER 14 Taming Processes and Services
  • ■ CHAPTER 15 Input and Output
  • ■ CHAPTER 16 Monitoring Your System
  • ■ CHAPTER 17 PowerShell and the World Wide Web
  • ■ CHAPTER 18 Sending E-mail
  • ■ CHAPTER 19 Talking to COM Objects
  • ■ CHAPTER 20 Managing Windows with WMI
  • ■ CHAPTER 21 PowerShell Community Extensions
  • ■ CHAPTER 1 Objects and Object Types Introduction xxiii
  • The Extended Type System
  • Accessing Object Properties
  • Using Object Methods
  • Object Adapters.
  • Type Extensions
  • Built-in Types
  • Strings and String Operations
  • Numeric Types
  • Arrays and Collections
  • Dictionaries and Hash Tables
  • Type Literals
  • Type Conversion
  • Accessing Static Members
  • Summary
  • ■ CHAPTER 2 Controlling Execution Flow
  • Conditional Expressions
  • Comparing Values.
  • Implicit Type Conversions
  • Logic and Bitwise Operations
  • Boolean Conversions
  • The Power of String Conversions
  • Collections and Conditional Expressions
  • Branching Instructions viii ■C O N T E N T S
  • Simple Branching: if/else.
  • Multiple Branches and the switch Statement
  • Loops: Repeatedly Executing Blocks of Code
  • The Simplest Loop: while
  • Loops and Counters: for Loop
  • Executing Actions on All Objects in a Collection: foreach Loop.
  • Controlling Loop Execution: break and continue Statements
  • Summary
  • ■ CHAPTER 3 The Object Pipeline
  • Text-Based Pipelines
  • Object-Based Pipelines
  • Processing Collections Using ForEach-Object
  • Filtering Collections Using Where-Object
  • Adding or Deleting Properties Using Select-Object
  • Sorting Collections
  • Pipeline Tees
  • Grouping Objects
  • Gathering Collection Statistics
  • Detecting Changes and Differences Among Objects
  • The Object Pipeline and Functional Programming
  • Summary
  • ■ CHAPTER 4 Working with Script Blocks
  • Defining Script Blocks
  • Passing Parameters and Returning Values
  • Processing Pipeline Input
  • Variable Scope
  • Invoking Strings as Expressions
  • Script Blocks as Delegates
  • Summary
  • ■ CHAPTER 5 Working with Functions
  • Defining Functions
  • Function Internals
  • Function Parameters
  • Passing Parameters by Reference
  • Returning Values. ■C O N T E N T S ix
  • Scoping Rules
  • Filters
  • Functions and Script Blocks
  • Implementing New Control Structures
  • Script Blocks as Strategies
  • Summary
  • ■ CHAPTER 6 Command Aliases
  • Working with Aliases
  • Creating Aliases
  • Modifying Aliases
  • Exporting and Importing Aliases
  • Aliasing Tips, Techniques, and Pitfalls
  • Name Clashes
  • Complex Aliases
  • Removing Broken Aliases
  • Built-in Aliases
  • cmd.exe Look-alikes
  • UNIX Look-alikes.
  • Convenience Aliases.
  • Summary
  • ■ CHAPTER 7 Providers
  • Enumerating Providers
  • Drives
  • Drive Scope
  • Navigating to Drives
  • Provider Capabilities
  • Basic Provider Capabilities
  • Drive Providers
  • Item Providers
  • Item Container Providers
  • Navigation Providers.
  • Item Content Providers.
  • Item Property Providers
  • Dynamic Item Property Providers
  • Item Security Descriptor Providers
  • Summary
  • ■ CHAPTER 8 Script Files x ■C O N T E N T S
  • Creating Scripts
  • Invoking Scripts
  • Passing Parameters
  • Returning Values.
  • Executing PowerShell Scripts from Other Environments
  • Developing and Maintaining Script Libraries
  • Dot-sourcing as a Means to Include Script Libraries
  • The Library Path Problem
  • Summary
  • ■ CHAPTER 9 Error Handling and Debugging
  • Handling Errors
  • Common Error-Handling Parameters
  • Trapping Errors
  • Capturing Nonterminating Errors
  • Raising Errors
  • Debugging Your Code
  • Print Debugging
  • Stepping Through Scripts and Breaking Execution.
  • Tracing Script Execution Details
  • Summary
  • ■ CHAPTER 10 Signing Scripts
  • How Code Signing Works
  • Guaranteeing Script Integrity.
  • Certifying the Origin of a Script
  • Managing Certificates
  • Creating a Self-Signed Certificate
  • Creating a Certification Authority Certificate
  • Issuing a Code-Signing Certificate
  • Signing Scripts
  • Running Scripts on Other Machines
  • Summary
  • ■ CHAPTER 11 The Shell Environment and Its Configuration ■C O N T E N T S xi
  • Shell Hosts
  • User Profile Scripts
  • Settings for All Users and All Shells
  • Settings for All Users Using a Specific Shell
  • Settings for a Specific User and All Shells
  • Settings for a Specific User and a Specific Shell
  • Working with Saved Console Settings
  • Changing the Prompt Settings
  • Tab Expansion: How Command Completion Works
  • Summary
  • ■ CHAPTER 12 Extending the Type System
  • Modifying Objects and Types
  • Adding Members to Single Objects.
  • Adding Members to All Instances of a Class
  • Extending Object Formatting
  • Creating Our Own Views
  • Summary
  • ■ CHAPTER 13 Getting Help
  • The Help System
  • The Basics
  • Parameter Details
  • Advanced Techniques
  • Getting Command Information
  • Getting Information About Objects
  • Using the Internet to Get Help
  • Summary
  • ■ CHAPTER 14 Taming Processes and Services
  • Working with Processes
  • Listing Processes: Getting the Big Picture
  • Starting and Stopping Processes
  • Processes and Their Windows
  • Baby Depend? Process Modules: On What Libraries Does This
  • Getting Information About a Program’s Publisher. xii ■C O N T E N T S
  • Setting Process Priority
  • Administering Services
  • Starting and Stopping Services
  • Configuring Services
  • Analyzing Service Dependencies
  • Summary
  • ■ CHAPTER 15 Input and Output
  • Reading Content
  • Writing Content
  • Content Encoding
  • Getting Byte Content.
  • Different Unicode Encodings
  • Extracting Data from Text
  • Finding Matches for a Regular Expression.
  • Finding String Occurrences Inside Files
  • Summary
  • ■ CHAPTER 16 Monitoring Your System
  • Working with the Windows Event Log
  • Reading from the System Event Logs
  • Writing to the Event Log.
  • Performance Counters
  • Consuming Counter Data
  • Monitoring Programs
  • Summary
  • ■ CHAPTER 17 PowerShell and the World Wide Web
  • Laying the Foundation
  • Fetching Files from the Web
  • Connection Problems Setting Connection Options and Debugging
  • Testing and Validating Web Sites
  • Test-Url: Verifying a Page’s Existence
  • Checking If a Page Contains Broken Links
  • Working with News Feeds ■C O N T E N T S xiii
  • Getting Feeds
  • Calling Web Services
  • Calling Web Services Using HTTP GET Requests
  • Calling Web Services Using HTTP POST Requests
  • Calling Web Services Using the SOAP Protocol
  • Handling FTP Transactions
  • Downloading Files from an FTP Server
  • Uploading Files to an FTP Server
  • Summary
  • ■ CHAPTER 18 Sending E-mail
  • Using System.Net.Mail
  • Building a Reusable Script
  • Configuring Recipients
  • Working with Message Headers
  • Multipart Messages
  • Message Views
  • Attaching Files to Messages
  • Embedding Media in Messages
  • Putting It All Together
  • Summary
  • ■ CHAPTER 19 Talking to COM Objects
  • How COM Works
  • How PowerShell Supports COM
  • Scripting Programs Through Their COM Interfaces
  • Automating Microsoft Word
  • Scripting Microsoft Excel
  • Driving Internet Explorer
  • Windows Script Host Code Interoperability
  • Evaluating Code
  • Exposing Objects from MSScriptControl
  • Summary
  • ■ CHAPTER 20 Managing Windows with WMI xiv ■C O N T E N T S
  • A Brief History of WMI
  • WBEM and WMI Components
  • The Common Information Model.
  • Network Transport
  • Object Discovery
  • PowerShell’s WMI Support
  • Get-WmiObject: The WMI Query Tool
  • Language Support for WMI Objects
  • Exploring CIM Classes and Objects
  • Getting a List of Classes and Namespaces
  • WMI System Properties
  • Querying Hardware Devices
  • Getting Information About Software
  • Operating System Updates
  • Installed Programs
  • The Network Configuration
  • Generating WMI Code
  • Scriptomatic
  • Summary
  • ■ CHAPTER 21 PowerShell Community Extensions
  • Installation and Configuration
  • Tab Expansion.
  • Editing Configuration Files
  • Getting Help.
  • File System Utilities
  • Navigation Helpers
  • NTFS Helpers.
  • Compressing and Archiving Files
  • Network Utilities
  • Executing Processes and Commands
  • XML Tools
  • Working with Image Files
  • Clipboard Helpers
  • New Providers ■C O N T E N T S xv
  • Reading from the .NET Global Assembly Cache
  • Exploiting the Internet Explorer Feed Store
  • Manipulating Windows Active Directory.
  • Utility Applications
  • Summary
  • ■ CHAPTER 22 PSEventing: .NET Events in PowerShell
  • Introducing PSEventing
  • Event-Handling Mechanics
  • FileSystemWatcher: A Real-World Example
  • Monitoring Entries Written to the System Event Logs
  • Handling WMI Events
  • Detecting If Our Script Has Been Terminated by the User
  • Using Script Blocks As Event Handlers
  • Summary
  • ■ CHAPTER 23 Enhancing Tab Completion with PowerTab
  • Installation
  • How PowerTab Works
  • Data Grid Pop-up Windows
  • Configuring PowerTab
  • The IntelliSense Completion Handler
  • The Tab Expansion Database
  • Summary
  • ■ INDEX

xvii

About the Author

■ HRISTO DESHEV has been a software developer, a team leader, and a product manager for Telerik, the leading vendor of user interface components (http://www.telerik.com). For the past several years, his focus has been on creating rich and, at the same time, robust and reliable web and desktop components running on the Microsoft .NET platform. A major goal for him and his team is to predictably deliver working software—applying agile software engineering practices to create rock-solid solutions targeting all modern web browsers and supporting multiple Visual Studio .NET versions. Hristo is a reformed UNIX fanatic who now runs Windows and tries to apply his scripting skills and experiences to the Microsoft platform. He is also a tool freak: anything that can help automate a task—be it a scriptable utility or a full-blown scripting language—is a welcome addition to his toolbox. PowerShell has been a passion of his since its early unofficial releases due to its ease of use and almost incredible ability to automate all Windows-related tasks without needing other tools. You can reach Hristo through his blog at http://weblogs.asp.net/hristodeshev/.