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.99 €19.99
Paperback
€24.99
Subscription
Free Trial
Renews at $19.99p/m

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
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 : 9781782173571
Vendor :
Microsoft
Tools :

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details

Publication date : Nov 28, 2014
Length: 206 pages
Edition : 1st
Language : English
ISBN-13 : 9781782173571
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

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.

Modal Close icon
Modal Close icon