Close the File and Try Again Powershell

Unlike other programming languages, the PowerShell scripting language has two types of mistake scenarios: terminating and not-terminating. Both of these types of errors are "bad," but by classifying them differently, the scripter can handle them differently. This distinction is important if you want to stop or exit a PowerShell script when it errors.

  • Author
  • Recent Posts

Non-terminating errors ^

For example, allow's say I've got a script that checks to see whether a file exists or not. I'll telephone call it ErrorExample.ps1. This script uses Test-Path to confirm whether a file exists. If it doesn't, it will return an mistake via the Write-Error cmdlet. This error will be non-terminating because Write-Fault always returns non-terminating errors.

if (-not (Test-Path -Path C:\DoesNotExist.txt)) {     Write-Mistake 'The file does not be' } else {     Write-Host 'The file does exist' } Write-Host 'Standing script regardless if file exists or not...'

The script will continue executing code whether or not that file exists as shown below.

Non terminating error

Not terminating error

This is a non-terminating fault example considering Write-Error did non terminate the script at line ii. Instead, information technology returned the mistake to the console and kept going. This scenario sometimes isn't desirable. Perhaps that file you're checking for is disquisitional to the success of the lines beneath it. In that case, you'd want the script to stop execution completely. You'd know that if that file doesn't exist, the rest of the script isn't going to work.

Terminating errors ^

Terminating errors are the second type of mistake in PowerShell. We can think of terminating errors as exceptions. Exceptions are either errors that terminate a script completely or ones PowerShell "throws" into a catch block to handle the mistake and perform whatever necessary actions after throwing the exception.

One fashion a scripter tin can invoke a terminating error is by using the throw keyword. This PowerShell construct creates a terminating fault while also throwing an exception. Using the instance above, let's say that file is disquisitional and we'd like to stop script execution if it doesn't exist. We could replace Write-Error with throw instead.

if (-not (Test-Path -Path C:\DoesNotExist.txt)) {     throw 'The file does not be' } else {     Write-Host 'The file does be' }  Write-Host 'Continuing script regardless if file exists or not...'

Notice at present that line seven doesn't run; throw has stopped the script at line two.

Terminating error

Terminating error

Turning non-terminating into terminating errors ^

It'southward sometimes difficult to supersede all Write-Error references and other actions that produce not-terminating errors with throw statements. Instead, y'all can keep all of those non-terminating error-producing commands in your scripts and "catechumen" them to terminating errors.

If you'd like to turn all non-terminating errors into terminating ones, you lot tin perform a global action change by setting the $ErrorActionPreference variable to Stop. This automatic variable controls non-terminating error beliefs. Past setting $ErrorActionPreference to Stop, you're telling PowerShell to treat all instances where a non-terminating error occurs as terminating. This action applies across the board.

However, mayhap you'd rather non change all non-terminating errors to terminating ones and pick certain ones instead. Using the common parameter ErrorAction applied to all cmdlets and advanced functions, you can make this happen.

In our example once again, I can actually "override" the Write-Error cmdlet's behavior past forcing it to throw a terminating error. I'll use the ErrorAction parameter and set it to Stop.

Subscribe to 4sysops newsletter!

Write-Error 'The file does not exist' -ErrorAction Stop

When the script runs now, you lot'll run across that information technology stops execution without using the throw keyword. You lot tin can use the ErrorAction parameter on every cmdlet and avant-garde role in PowerShell. It is a great style to choose which commands should terminate script execution and which ones should not. Depending on the severity and dependencies further downward in the script, this may or not may be necessary.

avatar avatar

pleasantsbagall.blogspot.com

Source: https://4sysops.com/archives/stop-or-exit-a-powershell-script-when-it-errors/

0 Response to "Close the File and Try Again Powershell"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel