O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

PowerShell Troubleshooting Guide

Book Description

Minimize debugging time and maximize troubleshooting efficiency by leveraging the unique features of the PowerShell language

In Detail

Windows PowerShell provides an amazing platform for administrative scripting and automation. Understanding the PowerShell language will enable you to spend less time troubleshooting and be more effective when debugging is required. PowerShell also includes several avenues to provide feedback from your code that will make your troubleshooting and debugging time more profitable.

Improved coding practices combined with useful diagnostic information can lead to results quickly. This book starts with background information on the purposes of automation and PowerShell's entrance into Microsoft's automation strategy, and also answers the question of whether scripting (and specifically PowerShell scripting) is essentially different than traditional programming. A brief overview of the main features of the PowerShell language along with examples is provided. Focus is placed on proper script design and use of PowerShell features in an attempt to reduce the need for troubleshooting.

What You Will Learn

  • Utilize PowerShell output cmdlets to provide the troubleshooting information you need
  • Use pipeline input in your functions to reduce parameter passing issues
  • Use Write-* cmdlets to expose the right kind of information in the right places
  • Control the environment to eliminate surprises when executing scripts
  • Create powerful scripts and functions that communicate expectations
  • Write unit tests for your PowerShell functions and scripts to ensure that they can be executed correctly

Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.

Table of Contents

  1. PowerShell Troubleshooting Guide
    1. Table of Contents
    2. PowerShell Troubleshooting Guide
    3. Credits
    4. About the Author
    5. About the Reviewers
    6. www.PacktPub.com
      1. Support files, eBooks, discount offers, and more
        1. Why subscribe?
        2. Free access for Packt account holders
        3. Instant updates on new Packt books
    7. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Conventions
      5. Reader feedback
      6. Customer support
        1. Downloading the example code
        2. Errata
        3. Piracy
        4. Questions
    8. 1. PowerShell Primer
      1. Introduction
      2. Cmdlets
        1. The big three cmdlets
      3. Functions
      4. Scripts
      5. Pipelines
        1. How pipelines change the game
        2. What's the fuss about sorting?
      6. Variables
      7. Modules
      8. Further reading
      9. Summary
    9. 2. PowerShell Peculiarities
      1. PowerShell strings
        1. String substitution
        2. How string substitution goes wrong
        3. Escaping in PowerShell strings
      2. Function output
      3. Pipeline processing
      4. PowerShell error handling
        1. The trap statement
        2. try, catch, and finally statements
        3. Non-terminating errors
      5. Further reading
      6. Summary
    10. 3. PowerShell Practices
      1. Filter left
      2. Format right
      3. Comment-based help
      4. Using Write-* cmdlets
        1. Write-Host
        2. Output – the correct way
        3. What about the other Write-* cmdlets?
        4. Which Write should I use?
        5. Write-Verbose
        6. Write-Debug
        7. Write-Warning
        8. Write-Error
        9. Write-Progress
      5. Further reading
      6. Summary
    11. 4. PowerShell Professionalism
      1. Naming conventions
        1. Cmdlet and function naming
        2. Parameter naming
        3. Module naming
        4. Variable naming
      2. Modularization
        1. Breaking a process into subtasks
        2. Helper functions
        3. Single responsibility principle
        4. Don't repeat code
        5. Understanding the process
      3. Version control
        1. Using version control with PowerShell
      4. Unit testing
        1. Rolling your own unit tests
        2. Why is PowerShell testing difficult?
        3. An introduction to Pester
        4. Mocking with Pester
      5. Further reading
      6. Summary
    12. 5. Proactive PowerShell
      1. Error handling
        1. Error-handling guidelines
        2. Error-handling techniques
        3. Investigating cmdlet error behavior
        4. Catch and release
      2. CmdletBinding()
        1. Common parameter support
      3. SupportsShouldProcess
        1. Parameter name validation
      4. Parameter value validation
      5. Pipeline input
      6. Pipelines and function execution
      7. Parameter type transformation
      8. #REQUIRES statements
      9. Set-StrictMode and Set-PSDebug -strict
      10. Further reading
      11. Summary
    13. 6. Preparing the Scripting Environment
      1. Validating operating system properties
        1. Workstation/server version
        2. Operating system version
        3. Putting it all together
      2. Validating service status
      3. Validating disk and memory availability
      4. Validating network connectivity
        1. Using telnet
        2. Using Test-NetConnection
        3. Writing Test-NetConnection in downstream versions
        4. Testing UDP and ICMP connectivity
        5. Validating connectivity prior to implementation
        6. Putting it all together
      5. Further reading
      6. Summary
    14. 7. Reactive Practices – Traditional Debugging
      1. Reading error messages
        1. The color problem
        2. Changing console colors
        3. Changing ISE colors
        4. PowerShell profiles
        5. Error message content
      2. Using Set-PSDebug
      3. Debugging in the console
      4. Debugging in the ISE
      5. Event logs
        1. Listing event logs
        2. Reading event logs
        3. Writing to event logs
      6. The PSDiagnostics module
      7. Using –confirm and –whatif
      8. Reducing input set
      9. Using Tee-Object to see intermediate results
      10. Replacing the foreach loop with the foreach-object cmdlet
      11. Further reading
      12. Summary
    15. 8. PowerShell Code Smells
      1. Code smells
        1. Code smells, best practices, antipatterns, and technical debt
      2. Language-agnostic code smells
      3. PowerShell-specific code smells
      4. Missing Param() statements
      5. Homegrown common parameters
      6. Unapproved verbs
      7. Accumulating output objects
      8. Sequences of assignment statements
      9. Using untyped or [object] parameters
      10. Static analysis tools – ScriptCop and Script Analyzer
        1. ScriptCop
        2. Script Analyzer
      11. Further reading
      12. Summary
    16. Index