CS Tutorials have content on different topics from computer science/information technology field.
Error Handling in PowerShell
Get link
Facebook
X
Pinterest
Email
Other Apps
A good practice while working on any script is to have a mechanism for error handling. Though it may take a little bit of additional time, its rewards are priceless. Before moving to Error handling in PowerShell, first go through what type of errors we may encounter.
Error Types
There are two types of errors that can be occurred during script execution.
Terminating
Non-terminating
As the name implies, terminating error will stop the program from further execution, whereas a non-terminating error will not stop the execution. An example of terminating error would be a syntax error whereas an example of non-terminating error would be file/path not found.
Terminating errors can be caught and handled while Non-terminating errors cannot be caught by try-catch block.
Examples:
Terminating error: Script execution halted when error occurred.
Non-terminating error: Write-Host "Still running" executed even after error occurred.
Convert Non-terminating errors into Terminating
If you want to trap the error using try-catch block, hide it from user and provide some user friendly message. You have to convert non-terminating error into terminating.
To do that we have ErrorAction parameter. Every PowerShell cmdlet supports ErrorAction. By specifying -ErrorAction Stop on the end of a cmdlet you ensure that any errors it throws are treated as terminating and can be caught.
Change in execution flow is visible in snapshot. Once we used ErrorAction with stop, Non-terminating error converted to terminating one.
Possible values for ErrorAction parameter:
Continue - Logs error to $Error, displays error to console, continues execution.
Stop - Logs error to $Error, displays error to console, terminates.
SilentlyContinue - Logs error to $Error, does not display error, continues processing.
Ignore - Does not log error to $Error, does not display error, continues processing.
It is also possible to make all error as terminating for session by using $ErrorActionPreference = Stop as first line.
Error Handling via try-catch block
In PowerShell, the error handling is done through try and catch blocks. The try block will have the code, that may likely throw an error. The catch block contains the code or action to be executed in case of an error that is thrown by the try block. Additionally, a finally block can be used to free up the resources. Mostly, non-terminating errors can’t be handled in PowerShell. To handle such errors, they needed to be converted to a terminating error (Which we already discussed).
When you have cmdlets in a try/catch and you want PowerShell to go to the catch, you still need to specify ErrorAction. Just because a cmdlet is in a try/catch does not mean PowerShell will go to the catch if the cmdlet encounters an error.
You can also create multiple catch blocks to handle specific type error
The exception type is checked for each catch block until one is found that matches your exception. Only one catch block is invoked even if there are multiple matches.
Adding proper exception handling to your scripts not only make them more stable, but also makes it easier for you to troubleshoot those exceptions.
In this tutorial, we will see how we can use PowerShell to import and export CSV file's data. Managing CSV Files using Import/Export-CSV in PowerShell We have two important cmdlets import-csv/export-csv which are widely used while working with CSV. Before moving to how to manage these type of files, First look at what is CSV file: A Comma Separated Values ( CSV ) file is a plain text file that contains a list of data. These files are often used for exchanging data between different applications. For example, databases and contact managers often support CSV files. A CSV file has a fairly simple structure. It’s a list of data separated by commas as you can see in below screenshot. We have file "email.csv" having email address and name separated by comma. You can use notepad/excel to open thses file. Now lets move back to our main topic, how to manage csv files with PowerShell. Reading CSV Files with Import-Csv Importing of csv file is pretty straight forward. First move to...
If you ever need a unique id in your script, you can create GUID (globally unique identifier) In PowerShell 4.0 and earlier you can use [guid] class with its static method. In PowerShell 5.0 and later, you can use New-Guid cmdlet: You can use Get-Member to know its object type. You can also store GUID into a variable like below If you want to get GUID as string, use ToString method or Guid property. Both return output of string type, you can verify this using Get-Member .
In this tutorial we will see about PowerShell Arithmetic Operators and how PowerShell is dealing with them. Arithmetic operators are used to perform numeric calculations. As well as numeric calculations, the addition operator may also be used with strings, arrays, and hashtables. Multiplication operator may also be used with strings and arrays. Below is the list of PowerShell arithmetic operators. Operator Description Example + Adds numeric values 4 + 6 concatenates a string, arrays, and hash tables “CS” + “Tutorials” – Subtracts numeric values 4 – 2 Makes a number negative -98 * Multiple numeric values 4 * 2 copy string, arrays to the specified number of times “!” * 4 / Divides numeric values 4 / 2 % Gives remainder after division 7 % 3 For live demo, watch below video. I have demonstrated the use of PowerShell arithmetic operators by creating calculator. Along with operators, Switch statement and While loop also used.
Comments
Post a Comment