Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
PowerShell Troubleshooting Guide
PowerShell Troubleshooting Guide

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

eBook
€17.98 €19.99
Paperback
€24.99
Subscription
Free Trial
Renews at $19.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Table of content icon View table of contents Preview book icon Preview Book

PowerShell Troubleshooting Guide

Chapter 2. PowerShell Peculiarities

In many ways, PowerShell is different (as a language) than other traditional programming languages. Some of PowerShell's peculiarities will be presented in this chapter, as well as some guidance on how to avoid common pitfalls. Here are the topics we'll cover in this chapter:

  • Strings (quoting, substitution, and escaping)
  • Function return values
  • Pipeline processing
  • Error handling and non-terminating errors

PowerShell strings

In PowerShell, either double quotes or single quotes can be used to express string literals. For instance, the following values are the same:

"HELLO WORLD"
'HELLO WORLD'

Using both kinds of quotes can be useful when quoting strings which themselves contain quotes, such as the following:

"I can't stop using PowerShell"
'He said, "I like using PowerShell" all day long'

If a single quote is needed in a single-quoted string, you can double the quote (for example, 'can't is a contraction'). The same technique allows the use of double-quotes in a double-quoted string. Strings written this way can be somewhat confusing to look at and it is easy to lose track of the number of quotes. A simpler method of including a double quote character in a string is to escape it with the backtick (`), also called a grave accent. The following string is an example: "the `" character is a double quote".

A peculiar...

Function output

The example function included in Chapter 1, PowerShell Primer, used string substitution and returned the single result of that operation. As a reminder, here it is again:

function Get-PowerShellVersionMessage{
param($name)
    $PowerShellVersion=$PSVersionTable.PSVersion
    return "We're using $PowerShellVersion, $name!"
}

This pattern (that is, performing a calculation and returning the result) is common to procedural programming languages such as C#, Java, and Visual Basic. In PowerShell, however, functions are more complicated than the usage in this scenario in several ways.

In statically-typed languages, the type of a function (that is, the type of a value returned by the function) is either declared as part of the function definition or inferred by the compiler. In PowerShell, the only consideration of a type associated with the output of a function is in the help provided for the function. This output type can be used by a PowerShell host to guide IntelliSense...

Pipeline processing

Another way that functions in PowerShell are different from other languages is how they interact with the pipeline. Functions in PowerShell output objects that can be picked up as input to subsequent functions or cmdlets. This in and of itself is not unique, but the timing of the output is different from other languages.

Consider the process of obtaining a listing of all of the files on a drive. For some drives this operation will be very lengthy, possibly taking several minutes to complete. In PowerShell though, the list of files begin to appear almost instantly. The process can take a while to be complete, but the function (Get-ChildItem) will output file and folder objects as each directory is scanned rather than waiting to have all of them collected in a list and returning the list all at once. This feature of built-in cmdlets is something that might easily be taken for granted, but when writing functions, the concept of a return value needs to be carefully considered...

PowerShell error handling

Discussions about error handling in PowerShell revolve around two things: the statements used in error handling, and what constitutes an error that needs to be handled.

The trap statement

PowerShell error handling had a rocky start. The Version 1.0 error handling statement was the trap statement. This statement is similar to the ON ERROR GOTO statement in Visual Basic 6. This was a functional way to do error handling, but it was not what most programming languages had been using for the last decade. If a trap statement is included in a scope, when an exception (called a terminating error in PowerShell terminology) occurs in that scope, the execution is stopped and the trap statement is executed. By default, trap statements handle any terminating error and can be written to only handle certain types of errors. In the scope of the trap statement, the $_ special variable contains the error or exception that was caught.

The default execution for a trap statement writes...

Further reading

You can go through the following references for more information on this topic:

  • get-help about_quoting_rules
  • get-help about_PowerShell_Ise.exe
  • get-help about_preference_variables
  • get-help about_operators
  • get-help about_escape_characters
  • get-help about_return
  • get-help about_trap
  • get-help about_try_catch_finally
  • get-help about_throw
  • Learn about formatting codes at http://msdn.microsoft.com/en-us/library/26etazsy.aspx

PowerShell strings


In PowerShell, either double quotes or single quotes can be used to express string literals. For instance, the following values are the same:

"HELLO WORLD"
'HELLO WORLD'

Using both kinds of quotes can be useful when quoting strings which themselves contain quotes, such as the following:

"I can't stop using PowerShell"
'He said, "I like using PowerShell" all day long'

If a single quote is needed in a single-quoted string, you can double the quote (for example, 'can't is a contraction'). The same technique allows the use of double-quotes in a double-quoted string. Strings written this way can be somewhat confusing to look at and it is easy to lose track of the number of quotes. A simpler method of including a double quote character in a string is to escape it with the backtick (`), also called a grave accent. The following string is an example: "the `" character is a double quote".

A peculiar kind of string in PowerShell is called a here-string. Here-strings allow strings to...

Left arrow icon Right arrow icon

Description

The techniques in this book apply to beginners who have just started to learn PowerShell, as well as advanced scripters who have a good grasp of the language.

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

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Nov 28, 2014
Length: 206 pages
Edition : 1st
Language : English
ISBN-13 : 9781782173588
Vendor :
Microsoft
Tools :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

Publication date : Nov 28, 2014
Length: 206 pages
Edition : 1st
Language : English
ISBN-13 : 9781782173588
Vendor :
Microsoft
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total $ 103.97
Active Directory with PowerShell
€36.99
Mastering Windows PowerShell Scripting
€41.99
PowerShell Troubleshooting Guide
€24.99
Total $ 103.97 Stars icon

Table of Contents

9 Chapters
1. PowerShell Primer Chevron down icon Chevron up icon
2. PowerShell Peculiarities Chevron down icon Chevron up icon
3. PowerShell Practices Chevron down icon Chevron up icon
4. PowerShell Professionalism Chevron down icon Chevron up icon
5. Proactive PowerShell Chevron down icon Chevron up icon
6. Preparing the Scripting Environment Chevron down icon Chevron up icon
7. Reactive Practices – Traditional Debugging Chevron down icon Chevron up icon
8. PowerShell Code Smells Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Full star icon Full star icon Full star icon Half star icon 4.7
(3 Ratings)
5 star 66.7%
4 star 33.3%
3 star 0%
2 star 0%
1 star 0%
miltongoh Feb 09, 2015
Full star icon Full star icon Full star icon Full star icon Full star icon 5
After reading this book for awhile, I decided to post my review according to the word “NICE”. Why this? Well, let’s see.N – NeatThe structure of this book is being planned and categorized in a way that I personally feel is neat. Although this book is primarily for readers who are not a beginner in PowerShell Technologies, however the book just walked you through bits and pieces the basic in PowerShell so that it caters to beginners who are also fast-learners. (Of course if you required more in-depth learning of PowerShell or if you are fresh to PowerShell, there are tons of books and learning resources out there. Do not worry! Earmark this book for reading purposes when you are ready!)The book is being structured in a way that it walks you through different aspect of PowerShell such as there is dependency. For example, you need to know Cmdlets before you goes into Functions. You will need to know Functions before going to Scripts. Then go to the next level of meddling with Pipelines and Modules. So all in all, this is a progressive learning over and over again from chapter to chapter.I – Intuitive and/or InstructionalAdding on to the “Neat” that I have elaborate above, I feel that the content and knowledge that I have received after reading this book is as if I am doing Self-Learning which reminds of me of all the Microsoft Official Curriculum that I have read through while preparing for my Active Directory exam for Window Server 2008. Lots of screenshots have been placed in the book to provide better illustration of the coding that the author was trying to demonstrate. On top of that, do not forget that as a reader, you have access to the ZIP Archive which contains the PowerShell Script and Module used for the various chapter. So it simply save you time to type the command out instead. Although I really encourage PowerShell lovers to type out the command rather than copying because, while you are typing, you can to interface with the various Cmdlets and you may hit into errors when you are selecting the wrong Cmdlets to perform the right tasks.Reviewing the error messages will definitely help in getting yourself prepared in troubleshooting more complex PowerShell scripts in future.C – CorrectWell, what do I mean by Correct? Do I meant that the content is Correct or the way the content is structured is Correct?No, what I actually meant is. The Author have used the Correct way to target at PowerShell lovers. In everything that we do, there may be more than one way to get to the endpoint but usually there is only one way to nail things down and make sure things are done in the Correct manner.Why is being Correct important? Take for example, if one adopts a different way of writing PowerShell script, then when the scripts are being shared with other Scripters. Then it will take others a lot of time in order to put themselves in the shoes of the original scripter. Sometime it will be worst where one will take some time to refined the scripts to their own style. What could be worst is that, if the timeline is short and there is already a shortage of resources that are available to perform a set of tasks then you wouldn’t want to expense out a resource time to reinvent the whole wheel.So let’s all adopt to a single lingo and automate the world.E – EducationI know all books are for learning, but there are definitely books that I have personally read and felt that I have learned nothing much. I would highly recommend this book to all levels of PowerShell lovers so that for beginners will have a taste of PowerShell and for advance players out there, you will get to perfect your skills.
Amazon Verified review Amazon
muhammed Shafeeque Feb 11, 2015
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This Book has to offer so much more than just troubleshooting. Author starts with a PowerShell primer and focuses on the 3 cmdlets (Get-Help, Get-Command & Get-Member ) which often many people overlook.I liked the approach from the Author on Troubleshooting process first know the best practices and follow those once you do it the troubleshooting process comes in.That's why he explains the peculiarities in PowerShell in second chapter and then the adopted PowerShell practices in the third chapter ( Filter left, Format right ).The best chapter of the book is the chapter 4 on PowerShell professionalism as the Author covers 4 most important aspects of when you are writing Production grade Scripts:1. Naming Convention2. Modularization3. Version Control4. Unit testingUnit testing examples using Pester are really good as they give you a good peek into what Pester can do for you.Chapter 5 is all about how to use PowerShell proactively to improve our scripts.Chapter 6 talks about when you deploy PowerShell scripts targeting a version, what to check so that there are no surprises.Right now in process of reading chapter 7 , till now the book has been a good read and it brushes all the good practices I have picked by following the forums and going through the Scripts of others.This Book is for you if you have already grasped basics of PowerShell and write scripts for your environment and you want your scripts to follow all the adopted best practices as that will help you later on troubleshoot them.
Amazon Verified review Amazon
Deepak Jan 29, 2015
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
This Book has to offer so much more than just troubleshooting. Author starts with a PowerShell primer and focuses on the 3 cmdlets (Get-Help, Get-Command & Get-Member ) which often many people overlook.I liked the approach from the Author on Troubleshooting process first know the best practices and follow those once you do it the troubleshooting process comes in.That's why he explains the peculiarities in PowerShell in second chapter and then the adopted PowerShell practices in the third chapter ( Filter left, Format right ).The best chapter of the book is the chapter 4 on PowerShell professionalism as the Author covers 4 most important aspects of when you are writing Production grade Scripts:1. Naming Convention2. Modularization3. Version Control4. Unit testingUnit testing examples using Pester are really good as they give you a good peek into what Pester can do for you.Chapter 5 is all about how to use PowerShell proactively to improve our scripts.Chapter 6 talks about when you deploy PowerShell scripts targeting a version, what to check so that there are no surprises.Right now in process of reading chapter 7 , till now the book has been a good read and it brushes all the good practices I have picked by following the forums and going through the Scripts of others.This Book is for you if you have already grasped basics of PowerShell and write scripts for your environment and you want your scripts to follow all the adopted best practices as that will help you later on troubleshoot them.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.

Modal Close icon
Modal Close icon