PowerShell Troubleshooting Guide uses easy-to-understand examples to explain the PowerShell language, enabling you to spend more of your time writing code to solve the problems you face and less time agonizing over syntax and cryptic error messages. Beginning with the foundations of PowerShell, including functions, modules, and the pipeline, you will learn how to leverage the power built into the language to solve problems and avoid reinventing the wheel. Writing code in PowerShell can be fun, and once you've learned the techniques in this book, you will enjoy PowerShell more and more.
Chapter 1, PowerShell Primer, provides a brief introduction to some of the most important entities in the PowerShell language including cmdlets, functions, scripts, and modules. A special emphasis is placed on the importance of the pipeline in PowerShell operations.
Chapter 2, PowerShell Peculiarities, includes a number of features of the PowerShell language, which are unusual when compared with other mainstream programming languages. Examples of these topics are output from functions and non-terminating errors.
Chapter 3, PowerShell Practices, shows a few ways that the scripting experience in PowerShell can be improved, either in performance or in maintainability. A lengthy discussion of the various output cmdlets is included.
Chapter 4, PowerShell Professionalism, gives examples of practices that might not be as familiar to traditional system administrators but are common among professional developers. These practices will help scripters create more reliable products and be more confident when making changes to existing codebases.
Chapter 5, Proactive PowerShell, presents a number of practices that, when applied to code, will result in more flexible code with fewer bugs. In a sense, this is pre-emptive troubleshooting, where we create our code thoughtfully in order to reduce the need for troubleshooting later.
Chapter 6, Preparing the Scripting Environment, covers the idea of knowing the characteristics of the environment in which your scripts are running. We also spend some effort trying to weed out network connectivity issues.
Chapter 7, Reactive Practices – Traditional Debugging, shows how to perform traditional troubleshooting in PowerShell using the debugging features of the console and the ISE, along with other techniques. It wraps up with an example of how using the wrong PowerShell feature to perform an operation can lead to poor performance.
Chapter 8, PowerShell Code Smells, explains the concept of code smells (signs of poorly implemented code) and compares it with antipatterns, best practices, and technical debt. It then shows some ways that PowerShell code might begin to smell.
Most of the examples in the book will work with PowerShell Version 2.0 and above. In places where a higher version of the engine is required, it will be indicated in the text. You should have no problems running the provided code on either a client installation (Windows 7 or greater) or a server installation (Windows Server 2008 R2 or greater).
This book is intended for anyone who has some experience with PowerShell and would like to expand their understanding of the language design and features in order to spend less time troubleshooting their code. The examples are designed to be understood without needing any specific application knowledge (for example, Exchange, Active Directory, and IIS) in order to keep the intent clear. A few sections are aimed at system administrators specifically. This is due to the different skill set that most administrators bring to the table compared with developers. However, the points made are applicable to anyone using PowerShell.
In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.
Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "The Get-Help
cmdlet has a number of switches that control precisely what help information is displayed."
A block of code is set as follows:
param($name) $PowerShellVersion=$PSVersionTable.PSVersion return "We're using $PowerShellVersion, $name!"
When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:
#find largest 5 items in the directory tree dir -recurse | tee-object –Variable Files | sort-object Length | tee-object –Variable SortedFiles | select-object -last 5
Any command-line input or output is written as follows:
Get-ChildItem "c:\program files" –include *.dll –recurse
New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "It features a button labeled Scan Script, a gear button for options, and a grid for results."
Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.
To send us general feedback, simply send an e-mail to <[email protected]>
, and mention the book title via the subject of your message. If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.
Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.
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.
Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.
Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.
Please contact us at <[email protected]>
with a link to the suspected pirated material.
We appreciate your help in protecting our authors, and our ability to bring you valuable content.
You can contact us at <[email protected]>
if you are having a problem with any aspect of the book, and we will do our best to address it.