Powershell basics part 12 - How to debug in powershell

1 comment
Powershell logo

Hello again,
In this post we will talk how can you debug a script in powershell. No matter how good you are when programming, you will bump into errors. You will have to know how to deal with errors, how to detect them ,understand and how to fix them.
Powershell has two mechanisms that were created to report errors, one is for the so called terminating errors and one is for non terminating errors.
The following lines best describe terminating and non-terminating errors. The link is from Microsoft's website:

"Use the following guidelines to determine whether an error is a terminating or non-terminating error.
An error is a terminating error if it prevents your cmdlet from continuing to process the current object or from successfully processing any further input objects, regardless of their content.

An error is a terminating error if you do not want your cmdlet to continue processing the current object or any further input objects, regardless of their content.

An error is a terminating error if it occurs in a cmdlet that does not accept or return an object or if it occurs in a cmdlet that accepts or returns only one object.

An error is a non-terminating error if you want your cmdlet to continue processing the current object and any further input objects.

An error is a non-terminating error if it is related to a specific input object or subset of input objects."
Syntax errors - this type of error occur when you use a cmdlet or a command and you are using the wrong syntax. If you are not sure how to use a command simply type Get-Help [command] to use it in the proper way. I will give you an example of a syntax error and the message that powershell returns:
Powershell script

As you can see the message 
"The string starting:
At line:1 char:19
+ $mes = Write-Host  <<<< "HELLO!
is missing the terminator: ".
At line:2 char:1" 
is returned and powershell is telling you that you didn't use the command with the correct syntax and you have to add the " character.
When an error occurs, the information about that particular error is stored in the ErrorRecord object. This object stores information of terminating and non terminating errors and it has the following data:
The exception that describes the error, this is returned by the cmdlet 
An error identifier that is used to uniquely identify each error
An error category used to display error information when the $ErrorView variable is set to "CategoryView".
A replacement error message that is used for debugging 
A recommended action used to provide a user some aid in solving the problem
An invocation information used to show the user what command was executed when the error occurred.
Read more about this topic on Microsoft's website http://msdn.microsoft.com/en-us/library/windows/desktop/ms714465(v=vs.85).aspx
When a non terminating error is found in a script, powershell will continue to execute the script. We can change this by telling powershell what to when a particular error is discovered. We can tell powershell how to react to errors by changing the variable $ErrorActionPreference. Windows powershell allows you to set this variable to the following values:
Stop - stops the script from executing
Continue - this tells powershell to continue executing the script
SilentlyContinue - doesn't display the error and the script si not stopped
Inquire - waits for the user to tell what action it should take
The –ErrorAction variable is used to instruct powershell of the action that it should undertake when an error is discovered. To demonstrate how to use powershell first type the following Get-WmiObject win32_systemdevices this command will display information about system devices. You can do the same for a remote computer Get-WmiObject win32_systemdevices -computername PC1. Normally powershell will display an error if the computer can be contacted or not. We can alter this by specifying the -ErrorAction variable: Get-WmiObject win32_systemdevices -computername PC1 -ErrorAction Continue. This will make powershell continue running the script even if the computer PC1 cannot be reached.
This is all for this post, there are more things that we can talk about when we talk about errors in powershell, we will talk about them in a future post.
If You Enjoyed This, Take 5 Seconds To Share It

1 comment:

  1. Hi, Are you well?

    I just included you to my buddies and decided upon your material and I have just submitted new eye-catching web page on ittrainingday.com.It is a amazing web page for all the area of person.This web page maintaining play a role many many way.Truly, I like this web page I am going to demonstrate you how to make a web page in WordPress in less than 10 min. I will show How to create a website. Wordpress platforms is a free foundation (free) web page designer which you can handle online. You will learn how Easy it is to set up WordPress, along with simple guidelines on "How to make some primary options in WordPress." But first, you will need to make web host with HostGator. In it clip I have defined this detailed.

    Thank you many many for your Nice able Article.