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
₹799.99 ₹1965.99
Paperback
₹2457.99
Subscription
Free Trial
Renews at $19.99p/m

What do you get with Print?

Product feature icon Instant access to your digital copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
Product feature icon Redeem a companion digital copy on all Print orders
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

Shipping Address

Billing Address

Shipping Methods
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
Estimated delivery fee Deliver to United States

Economy delivery 10 - 13 business days

Free $6.95

Premium delivery 6 - 9 business days

$21.95
(Includes tracking information)

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 Print?

Product feature icon Instant access to your digital copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
Product feature icon Redeem a companion digital copy on all Print orders
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

Shipping Address

Billing Address

Shipping Methods
Estimated delivery fee Deliver to United States

Economy delivery 10 - 13 business days

Free $6.95

Premium delivery 6 - 9 business days

$21.95
(Includes tracking information)

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 $ 10,204.97
Active Directory with PowerShell
₹3649.99
Mastering Windows PowerShell Scripting
₹4096.99
PowerShell Troubleshooting Guide
₹2457.99
Total $ 10,204.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 the digital copy I get with my Print order? Chevron down icon Chevron up icon

When you buy any Print edition of our Books, you can redeem (for free) the eBook edition of the Print Book you’ve purchased. This gives you instant access to your book when you make an order via PDF, EPUB or our online Reader experience.

What is the delivery time and cost of print book? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela
What is custom duty/charge? Chevron down icon Chevron up icon

Customs duty are charges levied on goods when they cross international borders. It is a tax that is imposed on imported goods. These duties are charged by special authorities and bodies created by local governments and are meant to protect local industries, economies, and businesses.

Do I have to pay customs charges for the print book order? Chevron down icon Chevron up icon

The orders shipped to the countries that are listed under EU27 will not bear custom charges. They are paid by Packt as part of the order.

List of EU27 countries: www.gov.uk/eu-eea:

A custom duty or localized taxes may be applicable on the shipment and would be charged by the recipient country outside of the EU27 which should be paid by the customer and these duties are not included in the shipping charges been charged on the order.

How do I know my custom duty charges? Chevron down icon Chevron up icon

The amount of duty payable varies greatly depending on the imported goods, the country of origin and several other factors like the total invoice amount or dimensions like weight, and other such criteria applicable in your country.

For example:

  • If you live in Mexico, and the declared value of your ordered items is over $ 50, for you to receive a package, you will have to pay additional import tax of 19% which will be $ 9.50 to the courier service.
  • Whereas if you live in Turkey, and the declared value of your ordered items is over € 22, for you to receive a package, you will have to pay additional import tax of 18% which will be € 3.96 to the courier service.
How can I cancel my order? Chevron down icon Chevron up icon

Cancellation Policy for Published Printed Books:

You can cancel any order within 1 hour of placing the order. Simply contact customercare@packt.com with your order details or payment transaction id. If your order has already started the shipment process, we will do our best to stop it. However, if it is already on the way to you then when you receive it, you can contact us at customercare@packt.com using the returns and refund process.

Please understand that Packt Publishing cannot provide refunds or cancel any order except for the cases described in our Return Policy (i.e. Packt Publishing agrees to replace your printed book because it arrives damaged or material defect in book), Packt Publishing will not accept returns.

What is your returns and refunds policy? Chevron down icon Chevron up icon

Return Policy:

We want you to be happy with your purchase from Packtpub.com. We will not hassle you with returning print books to us. If the print book you receive from us is incorrect, damaged, doesn't work or is unacceptably late, please contact Customer Relations Team on customercare@packt.com with the order number and issue details as explained below:

  1. If you ordered (eBook, Video or Print Book) incorrectly or accidentally, please contact Customer Relations Team on customercare@packt.com within one hour of placing the order and we will replace/refund you the item cost.
  2. Sadly, if your eBook or Video file is faulty or a fault occurs during the eBook or Video being made available to you, i.e. during download then you should contact Customer Relations Team within 14 days of purchase on customercare@packt.com who will be able to resolve this issue for you.
  3. You will have a choice of replacement or refund of the problem items.(damaged, defective or incorrect)
  4. Once Customer Care Team confirms that you will be refunded, you should receive the refund within 10 to 12 working days.
  5. If you are only requesting a refund of one book from a multiple order, then we will refund you the appropriate single item.
  6. Where the items were shipped under a free shipping offer, there will be no shipping costs to refund.

On the off chance your printed book arrives damaged, with book material defect, contact our Customer Relation Team on customercare@packt.com within 14 days of receipt of the book with appropriate evidence of damage and we will work with you to secure a replacement copy, if necessary. Please note that each printed book you order from us is individually made by Packt's professional book-printing partner which is on a print-on-demand basis.

What tax is charged? Chevron down icon Chevron up icon

Currently, no tax is charged on the purchase of any print book (subject to change based on the laws and regulations). A localized VAT fee is charged only to our European and UK customers on eBooks, Video and subscriptions that they buy. GST is charged to Indian customers for eBooks and video purchases.

What payment methods can I use? Chevron down icon Chevron up icon

You can pay with the following card types:

  1. Visa Debit
  2. Visa Credit
  3. MasterCard
  4. PayPal
What is the delivery time and cost of print books? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela
Modal Close icon
Modal Close icon