Windows PowerShell Best Practices

Book description

Expert recommendations, pragmatically applied.

Automate system administration using Windows PowerShell best practices—and optimize your operational efficiency. With this practical guide, Windows PowerShell expert and instructor Ed Wilson delivers field-tested tips, real-world examples, and candid advice culled from administrators across a range of business and technical scenarios. If you’re an IT professional with Windows PowerShell experience, this book is ideal.

Discover how to:

  • Use Windows PowerShell to automate Active Directory tasks

  • Explore available WMI classes and methods with CIM cmdlets

  • Identify and track scripting opportunities to avoid duplication

  • Use functions to encapsulate business logic and reuse code

  • Design your script’s best input method and output destination

  • Test scripts by checking their syntax and performance

  • Choose the most suitable method for running remote commands

  • Manage software services with Desired State Configuration

  • Table of contents

    1. Windows PowerShell Best Practices
    2. Dedication
    3. Foreword
    4. Introduction
      1. Who is this book for?
      2. How is this book organized?
      3. System requirements
      4. The companion website
      5. Acknowledgements
      6. Support & feedback
        1. Errata
        2. We want to hear from you
        3. Stay in touch
    5. I. Understanding the basics of Windows PowerShell
      1. 1. Survey of Windows PowerShell capabilities
        1. Understanding Windows PowerShell
        2. Installing Windows PowerShell
        3. Deploying Windows PowerShell
          1. Using cmdlets
        4. Using command-line utilities
        5. Security issues with Windows PowerShell
          1. Controlling execution of Windows PowerShell cmdlets
          2. Confirming commands
          3. Suspending confirmation of cmdlets
        6. Working with Windows PowerShell
          1. Accessing Windows PowerShell
          2. Configuring Windows PowerShell
        7. Supplying options for cmdlets
        8. Working with the help options
          1. Updating help information
          2. Discovering information in help
        9. Additional resources
      2. 2. Using the CIM cmdlets
        1. Using the CIM cmdlets to explore WMI classes
          1. Using the classname parameter
          2. Finding WMI class methods
          3. Filtering classes by qualifier
        2. Retrieving WMI instances
          1. Reduce returned properties and instances
          2. Clean up output from the command
        3. Working with Association classes
        4. Additional resources
    6. II. Planning for scripting
      1. 3. Using the Active Directory module
        1. Understanding the Active Directory module
          1. Installing the Active Directory module
          2. Getting started with the Active Directory module
        2. Using the Active Directory module
          1. Finding the FSMO role holders
          2. Documenting Active Directory
          3. Renaming Active Directory sites
          4. Managing users
          5. Creating a user
          6. Finding and unlocking AD user accounts
          7. Finding disabled users
          8. Finding unused user accounts
        3. Additional resources
      2. 4. Identifying scripting opportunities
        1. Automating routine tasks
        2. Automation interface
          1. Using RegRead to read the registry
          2. Using WMI to read the registry
          3. Using .NET to read the registry
          4. Using intrinsic Windows PowerShell techniques
        3. Structured requirements
          1. Security requirements
          2. Detecting the current user
          3. Detecting the user role
          4. .NET Framework version requirements
          5. Operating system requirements
          6. Application requirements
          7. Module requirements
        4. Additional resources
      3. 5. Configuring the script environment
        1. Configuring a profile
          1. Creating aliases
          2. Creating functions
            1. Overriding existing commands
            2. Aliasing the function
            3. Looping 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
      4. 6. 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
      5. 7. 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
        4. Additional resources
    7. III. Designing the script
      1. 8. Designing the script
        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. 9. Designing help for scripts
        1. Adding help documentation to a script with single-line comments
          1. Working with temporary folders
        2. Using multiple-line comment tags in Windows PowerShell 4.0
          1. Creating multiple-line comments with comment tags
          2. Creating single-line comments with comment tags
        3. Using comment-based help
        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. 10. Designing modules
        1. Understanding modules
        2. Locate and load modules
          1. Listing available modules
          2. Loading modules
        3. Install modules
          1. Creating a modules folder
          2. Working with the $modulePath variable
          3. Creating a module drive
          4. Checking for module dependencies
          5. Using a module from a share
        4. Creating a module
        5. Additional resources
      4. 11. 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 email
          5. Output from functions
            1. Avoid populating the global variable
            2. Using a namespace in the global variable
        4. Additional resources
      5. 12. 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 Param 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
          3. Using #Requires
            1. Require Administrator rights
            2. Requiring specific modules
        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
      6. 13. 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 Verbose parameter
          3. Using the whatif parameter
        4. Using Start-Transcript to produce a log
        5. Advanced script testing
        6. Additional resources
      7. 14. Documenting scripts
        1. Getting documentation from help
        2. Getting documentation from comments
        3. Using the AST parser
        4. Additional resources
    8. IV. Deploying the script
      1. 15. Managing the execution policy
        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. Additional resources
      2. 16. Running scripts
        1. Logon scripts
          1. What to include in logon scripts
          2. Methods of calling the logon scripts
        2. Script folder
          1. Deploy locally
          2. Deploy an MSI package locally
        3. Stand-alone scripts
          1. Diagnostics
          2. Reporting and auditing
        4. Help desk scripts
          1. Avoid editing
          2. Provide a good level of help interaction
        5. Additional resources
      3. 17. Versioning scripts
        1. 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
        2. Version control software
        3. Additional resources
      4. 18. 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 Aapplication log
          2. Creating a custom event log
        3. Logging to the registry
        4. Additional resources
      5. 19. Troubleshooting scripts
        1. Understanding debugging in Windows PowerShell
          1. Working with syntax errors
          2. Working with runtime errors
          3. Working with logic errors
        2. 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 strict mode
            1. Using Set-PSDebug -strict
            2. Using the Set-StrictMode cmdlet
        3. 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
        4. Additional resources
      6. 20. Using the Windows PowerShell ISE
        1. Running the Windows PowerShell ISE
          1. Navigating the Windows PowerShell ISE
          2. Working with the script pane
          3. Tab expansion and IntelliSense
        2. Working with Windows PowerShell ISE snippets
          1. Using Windows PowerShell ISE snippets to create code
          2. Creating new Windows PowerShell ISE snippets
          3. Removing user-defined Windows PowerShell ISE snippets
        3. Additional resources
      7. 21. Using Windows PowerShell remoting and jobs
        1. Understanding Windows PowerShell remoting
          1. Classic remoting
          2. WinRM—Windows Remote Management
            1. Create a remote Windows PowerShell session
            2. Run a single Windows PowerShell command
        2. Using Windows PowerShell jobs
        3. Additional resources
      8. 22. Using Windows PowerShell Workflow
        1. Why use Windows PowerShell Workflow
          1. Workflow requirements
          2. A simple workflow
        2. Parallel PowerShell
        3. Workflow activities
          1. Windows PowerShell cmdlets as activities
          2. Disallowed core cmdlets
          3. Non-automatic cmdlet activities
          4. Parallel activities
        4. Checkpointing a Windows PowerShell workflow
          1. Understanding checkpoints
          2. Placing checkpoints
          3. Adding checkpoints
            1. Workflow checkpoints are free
            2. Checkpoint activity
            3. Using the CheckPoint-Workflow activity
        5. Adding a sequence activity to a workflow
        6. Additional resources
      9. 23. Using the Windows PowerShell DSC
        1. Understanding Desired State Configuration
          1. The DSC process
          2. Configuration parameters
          3. Setting dependencies
          4. Configuration data
            1. Creating users with the user provider
            2. Creating groups with the group provider
        2. Controlling configuration drift
        3. Additional resources
    9. A. About the author
    10. Index
    11. About the Author
    12. Copyright

    Product information

    • Title: Windows PowerShell Best Practices
    • Author(s): Ed Wilson
    • Release date: January 2014
    • Publisher(s): Microsoft Press
    • ISBN: 9780735676589