How to use the ErrorAction parameter in PowerShell?

PowerShellMicrosoft TechnologiesSoftware & Coding

Like ErrorActionPreference variable, ErrorAction parameter works similarly. ErrorAction parameter supported in Advance functions and most of the built-in cmdlets in PowerShell. It is useful to convert the non-terminating error to the terminating error and then you can handle them with try/catch blocks.

Supported Values and Examples,

  • Continue − This is the default value of the ErrorAction parameter and Error will be displayed and commands listed in Pipeline will be executed further.

Get-WmiObject -Class Win32_Logicaldisk -ComputerName Nonexist
-ErrorAction Continue
Write-Host "`nHello World" -BackgroundColor DarkGreen

Output 

Get-WmiObject : The RPC server is unavailable.
At line:1 char:1
+ Get-WmiObject -Class Win32_Logicaldisk -ComputerName Nonexist -ErrorA ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Get-WmiObject], COMExcept ion
+ FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands .GetWmiObjectCommand

Hello World
  • Stop − Error message will be stopped displaying and the commands in the pipeline for execution won't run. There will be no output of the below example.

Example 

PS C:\WINDOWS\system32>>> Get-WmiObject -Class Win32_Logicaldisk -ComputerName
Nonexist -ErrorAction Stop
Write-Host "`nHello World" -BackgroundColor DarkGreen
  • SilentlyContinue − An error message will not be displayed and the script executes the pipeline commands.

Example 

Get-WmiObject -Class Win32_Logicaldisk -ComputerName Nonexist -
ErrorAction SilentlyContinue
Write-Host "`nHello World" -BackgroundColor DarkGreen

Output 

PS C:\WINDOWS\system32>>> Get-WmiObject -Class Win32_Logicaldisk -ComputerName Nonexist -ErrorAction SilentlyContinue
Write-Host "`nHello World" -BackgroundColor DarkGreen

Hello World
  • Ignore − Ignore value is the same as the Silentlycontinue except the error output is not stored into $Error variable.

Get-WmiObject -Class Win32_Logicaldisk -ComputerName
Nonexist -ErrorAction Ignore
Write-Host "`nHello World" -BackgroundColor DarkGreen

Hello World

Check the error variable now. You see in the below example, it won’t contain any error data while in the SilentlyContinue value, it stores the error output.

PS C:\WINDOWS\system32>>> $Error
  • Inquire − When an error occurred due to cmdlet, this option gives user below choices and prompt for appropriate action.

Get-WmiObject -Class Win32_Logicaldisk -ComputerName Nonexist -
ErrorAction Inquire
Write-Host "`nHello World" -BackgroundColor DarkGreen

Output 

Confirm
The RPC server is unavailable.
[Y] Yes [A] Yes to All [H] Halt Command [S] Suspend [?] Help (default is "
Y"):

An error message will be displayed if you select Yes/YestoAll and for Halt and suspend, the error won't be displayed.

  • Suspend − This value is used for the PowerShell workflow. Workflow is suspended for investigating error and then workflow can be resumed.

raja
Published on 27-May-2020 09:30:27
Advertisements