Windows PowerShell™ 2.0 Best Practices

Book description

Apply best practices for automating system administration with Windows PowerShell 2.0 and optimize your operational efficiency and results. This guide captures the field-tested tips, real-world lessons, and candid advice of practitioners across the range of business and technical scenarios and across the scripting life cycle. Discover how to:

  • Take advantage of new features and cmdlets in Windows PowerShell 2.0

  • Plan scripting usage scenarios and define standards Deploy Windows PowerShell 2.0 to desktops and servers

  • Configure scripting environments

  • Optimize remote scripting capabilities

  • Work with Active Directory and WMI

  • Design functions and modules

  • Optimize input and output

  • Handle errors

  • Document scripts

  • Test and troubleshoot scripts

  • Avoid scripting pitfalls

  • The companion CD includes a fully searchable eBook and sample scripts.

    For customers who purchase an ebook version of this title, instructions for downloading the CD files can be found in the ebook.

    Table of contents

    1. Dedication
    2. Acknowledgments
    3. Introduction
      1. Why I Wrote This Book
      2. On Form and Function
      3. For Whom Is This Book?
      4. How Is This Book Organized?
      5. What This Book Is Not
      6. System Requirements
      7. Support for This Book
      8. We Want to Hear from You
    4. About the Companion Media
    5. I. Introduction
      1. 1. Assessing the Scripting Environment
        1. Why Use Windows PowerShell 2.0?
        2. Comparison with Windows PowerShell 1.0
          1. Backward Compatibility
          2. Using the Version Tag
        3. New Features of Windows PowerShell 2.0
          1. New cmdlets
          2. Modified cmdlets
          3. Architectural Changes
            1. Remote Interactive Session
            2. Remote Command Execution
            3. Persistent Connection
            4. Remote Script Execution
        4. Comparing Windows PowerShell 2.0 to VBScript
          1. The Learning Curve
          2. Component Object Model (COM) Support
          3. Comparing Windows PowerShell 2.0 to the Command Shell
        5. Deployment Requirements for Windows PowerShell 2.0
          1. .NET Framework
          2. Service Dependencies
        6. Deploying Windows PowerShell 2.0
          1. Where to Deploy Windows PowerShell 2.0
          2. How Do You Anticipate Using Windows PowerShell 2.0?
        7. Additional Resources
      2. 2. Survey of Windows PowerShell Capabilities
        1. Using the Interactive Command Line
          1. The Easiest cmdlets
          2. The Most Important cmdlets
          3. Grouping and Sorting Output
            1. Group-Object
            2. Sort-Object
          4. Saving Output to Files
        2. Working with WMI
          1. Obtaining Information from Classes
          2. Finding WMI Classes
          3. Setting Properties
          4. Calling Methods
          5. Working with Instances
          6. Working with Events
        3. Working Remotely
          1. Using –computer
            1. Get-Process
            2. Get-Service
            3. Get-EventLog
            4. Get-WinEvent
            5. Test-Connection
            6. Restart-Computer
            7. Stop-Computer
          2. Creating a Remote Interactive Session
          3. Running a Remote Command
        4. Additional Resources
      3. 3. Survey of Active Directory Capabilities
        1. Creating Users, Groups, and Organizational Units
        2. Creating Objects
          1. Creating a User Account
          2. Creating a Group
          3. Creating a Computer Account
        3. Deriving the Create Object Pattern
          1. Modifying the Variables
          2. Constant vs. Read-Only Variables
          3. Creating a Utility Script
          4. Using CSV Files to Create Multiple Objects
          5. Testing a Script
        4. Configuring the Connection to the Database
          1. Using ADO.NET
          2. Using ADO COM Objects
        5. Modifying Properties
          1. Using Excel to Update Attributes
          2. Making the Connection
          3. Reading the Data
          4. Making the Changes
          5. Closing the Connection
        6. Additional Resources
      4. 4. User Management
        1. Examining the Active Directory Schema
        2. Querying Active Directory
          1. Using ADO
          2. Using Directory Searcher
          3. Using [ADSISearcher]
        3. Performing Account Management
          1. Locating Disabled User Accounts
          2. Moving Objects
          3. Searching for Missing Values in Active Directory
        4. Additional Resources
    6. II. Planning
      1. 5. Identifying Scripting Opportunities
        1. Automating Routine Tasks
          1. Automation Interface
          2. Using RegRead to Read the Registry
        2. Structured Requirements
          1. Security Requirements
            1. Detecting the Current User
            2. Detecting the User Role
          2. .NET Framework Version Requirements
          3. Operating System Requirements
          4. Application Requirements
          5. Snap-in Requirements
        3. Additional Resources
      2. 6. Configuring the Script Environment
        1. Configuring a Profile
          1. Creating Aliases
          2. Creating Functions
            1. Overriding Existing Commands
            2. Alias the Function
            3. Loop the Array
          3. Passing Multiple Parameters
            1. Multiple Parameters with $args
            2. Multiple Named Parameters
          4. Creating Variables
          5. Creating PSDrives
          6. Enabling Scripting
        2. Creating a Profile
          1. Choosing the Correct Profile
          2. Creating Other Profiles
        3. Accessing Functions in Other Scripts
          1. Creating a Function Library
          2. Using an Include File
        4. Additional Resources
      3. 7. Avoiding Scripting Pitfalls
        1. Lack of cmdlet Support
        2. Complicated Constructors
        3. Version Compatibility Issues
          1. Trapping the Operating System Version
        4. Lack of WMI Support
        5. Working with Objects and Namespaces
        6. Listing WMI Providers
        7. Working with WMI Classes
          1. Changing Settings
          2. Modifying Values Through the Registry
        8. Lack of .NET Framework Support
          1. Use of Static Methods and Properties
          2. Version Dependencies
          3. Lack of COM Support
          4. Lack of External Application Support
        9. Additional Resources
      4. 8. Tracking Scripting Opportunities
        1. Evaluating the Need for the Script
          1. Reading a Text File
          2. Export Command History
          3. Fan-out Commands
          4. Query Active Directory
            1. Using [ADSISearcher]
            2. Using Active Directory cmdlets
          5. Just Use the Command Line
        2. Calculating the Benefit from the Script
          1. Repeatability
          2. Documentability
          3. Adaptability
        3. Script Collaboration
          1. Windows SharePoint Services
          2. Microsoft Office Groove
          3. Live Mesh
        4. Additional Resources
    7. III. Designing
      1. 9. Designing Functions
        1. Understanding Functions
        2. Using Functions to Provide Ease of Code Reuse
          1. Using Two Input Parameters
          2. Using a Type Constraint
        3. Using More than Two Input Parameters
        4. Using Functions to Encapsulate Business Logic
        5. Using Functions to Provide Ease of Modification
        6. Understanding Filters
        7. Additional Resources
      2. 10. Designing Help for Scripts
        1. Adding Help Documentation to a Script with Single-Line Comments
        2. Using the Here-String for Multiple-Line Comments
          1. Constructing a Here-String
          2. An Example: Adding Comments to a Registry Script
          3. Retrieving Comments by Using Here-Strings
            1. Using Throw to Raise an Error
            2. Working with Temporary Folders
            3. Reading the Comments in the Output File
        3. Using Multiple-Line Comment Tags in Windows PowerShell 2.0
          1. Creating Multiple-Line Comments with Comment Tags
          2. Creating Single-Line Comments with Comment Tags
        4. The 13 Rules for Writing Effective Comments
          1. Update Documentation When a Script Is Updated
          2. Add Comments During the Development Process
          3. Write for an International Audience
          4. Consistent Header Information
          5. Document Prerequisites
          6. Document Deficiencies
          7. Avoid Useless Information
          8. Document the Reason for the Code
          9. Use of One-Line Comments
          10. Avoid End-of-Line Comments
          11. Document Nested Structures
          12. Use a Standard Set of Keywords
          13. Document the Strange and Bizarre
        5. Additional Resources
      3. 11. Planning for Modules
        1. Locating and Loading Modules
          1. Listing Available Modules
          2. Loading Modules
        2. Installing Modules
          1. Creating a User’s Modules Folder
            1. Locating the Directory Based on the Operating System
            2. Checking for an Existing Folder
            3. Creating a New Modules Folder
          2. Working with the $ModulePath Variable
          3. Creating a Module Drive
        3. Including Functions by Dot-Sourcing
        4. Adding Help for Functions
          1. Using a Here-String for Help
          2. Using Help Function Tags to Produce Help
        5. Additional Resources
      4. 12. Handling Input and Output
        1. Choosing the Best Input Method
          1. Reading from the Command Line
            1. Using the $args Automatic Variable
            2. Supplying Multiple Values to $args
          2. Using the Param Statement
            1. Creating a Mandatory Parameter
            2. Using Parameter Attributes
            3. Creating a Parameter Alias
            4. Validating Parameter Input
            5. Using Multiple Parameter Arguments
          3. Working with Passwords as Input
            1. Store the Password in the Script
            2. Store the Password in a Text File
            3. Store the Password in the Registry
            4. Store the Password in Active Directory Domain Services
            5. Prompt for the Password
          4. Working with Connection Strings as Input
        2. Prompting for Input
        3. Choosing the Best Output Method
          1. Output to the Screen
          2. Output to File
          3. Splitting the Output to Both the Screen and the File
          4. Output to E-Mail
          5. Output from Functions
            1. Avoid Populating the Global Variable
            2. Using a Namespace in the Global Variable
        4. Additional Resources
      5. 13. Handling Errors
        1. Handling Missing Parameters
          1. Creating a Default Value for the Parameter
            1. Detecting the Missing Value and Assigning It in the Script
            2. Assigning the Value in the Parameter Statement
          2. Making the Parameter Mandatory
        2. Limiting Choices
          1. Using PromptForChoice to Limit Selections
          2. Using Ping to Identify Accessible Computers
          3. Using the –contains Operator to Examine the Contents of an Array
          4. Using the –contains Operator to Test for Properties
        3. Handling Missing Rights
          1. Attempting and Failing
          2. Checking for Rights and Exiting Gracefully
        4. Handling Missing WMI Providers
        5. Incorrect Data Types
        6. Out of Bounds Errors
          1. Using a Boundary Checking Function
          2. Placing Limits on the Parameter
        7. Additional Resources
    8. IV. Testing and Deploying
      1. 14. Testing Scripts
        1. Using Basic Syntax Checking Techniques
          1. Looking for Errors
          2. Running the Script
          3. Documenting What You Did
        2. Conducting Performance Testing of Scripts
          1. Using the Store and Forward Approach
          2. Using the Windows PowerShell Pipeline
            1. Comparing the Speed of Two Scripts
            2. Reducing Code Complexity
          3. Evaluating the Performance of Different Versions of a Script
            1. Command-Line Parameters
            2. Functions
            3. Displaying the Results and Creating the Log File
        3. Using Standard Parameters
          1. Using the debug Parameter
          2. Using the –whatif Parameter
        4. Using Start-Transcript to Produce a Log
        5. Advanced Script Testing
        6. Additional Resources
      2. 15. Running Scripts
        1. Selecting the Appropriate Script Execution Policy
          1. The Purpose of Script Execution Policies
          2. Understanding the Different Script Execution Policies
          3. Understanding the Internet Zone
        2. Deploying the Script Execution Policy
          1. Modifying the Registry
          2. Using the Set-ExecutionPolicy cmdlet
            1. Using the Set-ExecutionPolicy cmdlet on a Local Computer
            2. Using the Set-ExecutionPolicy cmdlet via a Logon Script
          3. Using Group Policy to Deploy the Script Execution Policy
        3. Understanding Code Signing
        4. Logon Scripts
          1. What to Include in Logon Scripts
          2. Methods of Calling the Logon Scripts
        5. Script Folder
          1. Deployed Locally
          2. MSI Package Deployed Locally
        6. Stand-Alone Scripts
          1. Diagnostics
          2. Reporting and Auditing
        7. Help Desk Scripts
          1. Avoid Editing
          2. Provide a Good Level of Help Interaction
        8. Why Version Control?
          1. Avoid Introducing Errors
          2. Enable Accurate Troubleshooting
          3. Track Changes
          4. Maintain a Master Listing
          5. Maintain Compatibility with Other Scripts
          6. Internal Version Number in the Comments
            1. Incrementing Version Numbers
            2. Tracking Changes
          7. Version Control Software
        9. Additional Resources
    9. V. Optimizing
      1. 16. Logging Results
        1. Logging to a Text File
          1. Designing a Logging Approach
            1. Overwriting the Log
            2. Appending to the Log
            3. Using the Out-File cmdlet
          2. Text Location
          3. Networked Log Files
            1. Writing Directly to the File
            2. Writing to the Local File and Copying to the Network
        2. Logging to the Event Log
          1. Using the Application Log
          2. Creating a Custom Event Log
        3. Logging to the Registry
        4. Additional Resources
      2. 17. Troubleshooting Scripts
        1. Using the Set-PSDebug cmdlet
          1. Tracing the Script
            1. Working with Trace Level 1
            2. Working with Trace Level 2
          2. Stepping Through the Script
          3. Enabling StrictMode
            1. Using Set-PSDebug –Strict
            2. Using the Set-StrictMode cmdlet
        2. Debugging Scripts
          1. Setting Breakpoints
            1. Setting a Breakpoint on a Line Number
            2. Setting a Breakpoint on a Variable
            3. Setting a Breakpoint on a Command
          2. Responding to Breakpoints
          3. Listing Breakpoints
          4. Enabling and Disabling Breakpoints
          5. Deleting Breakpoints
        3. Additional Resources
    10. A. Windows PowerShell 2.0 cmdlets
    11. B. Common Windows PowerShell Verbs
    12. C. Useful COM Objects
    13. D. Useful WMI Classes
    14. E. Useful Microsoft .NET Framework Classes
    15. F. WMI Error Messages
    16. Index
    17. About the Author
    18. Copyright

    Product information

    • Title: Windows PowerShell™ 2.0 Best Practices
    • Author(s): Ed Wilson
    • Release date: December 2009
    • Publisher(s): Microsoft Press
    • ISBN: 9780735642218