Hello,
There are two types of Error. Terminating and non-terminating.
A terminating error is an error that will halt a function or operation. If you make a syntax error or run out of memory, that is a terminating error. Terminating errors can be caught and handled.
Non-terminating errors allow Powershell to continue and usually come from cmdlets or other managed situations. Under normal circumstances they cannot be caught by Try-Catch-Finally.
So how to catch a Non-Terminating error?
Basically, we need to tell PowerShell to treat it as terminating. To do this you use the ErrorAction parameter. 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.
Add-NcLunMap -Path $LunPath -InitiatorGroup $IgroupName -VserverContext $VserverName -Id $LunId -ErrorAction Stop
Once you have ensured that the error you are trying to catch is going to be treated as terminating, you can build a Try Catch block around the command (or commands) that might cause the error. The first stage is to surround the section of your script that may throw the error with a Try block. Immediately after the Try block you must place a Catch block to deal with the error. The Catch block is only accessed if a terminating error occurs, otherwise it is ignored.
try {
if ($LunId) {
Add-NcLunMap -Path $LunPath -InitiatorGroup $IgroupName -VserverContext $VserverName -Id $LunId -ErrorAction Stop
Get-WFALogger -Info -message $("Mapped LUN successfully")
}
else {
Add-NcLunMap -Path $LunPath -InitiatorGroup $IgroupName -VserverContext $VserverName -ErrorAction Stop
Get-WFALogger -Info -message $("Mapped LUN successfully")
}
}
catch {
$ErrorMessage = $_.Exception.Message
$FailedItem = $_.Exception.ItemName
throw "Failed to map LUN $FailedItem. Error: $ErrorMessage"
}
The values you can supply for the –ErrorAction parameter are SilentlyContinue, Stop, Continue, and Inquire.
Enumeration | Value | Description |
SilentlyContinue | 0 | The Windows PowerShell runtime will continue processing without notifying the user that an action has occurred. |
Stop | 1 | The Windows PowerShell runtime will stop processing when an action occurs. |
Continue | 2 | The Windows PowerShell runtime will continue processing and notify the user that an action has occurred. |
Inquire | 3 | The Windows PowerShell runtime will stop processing and ask the user how it should proceed. |
You can handle the situation as bellow:
if ($LunId) {
Add-NcLunMap -ErrorVariable Err -Path $LunPath -InitiatorGroup $IgroupName -VserverContext $VserverName -Id $LunId -ErrorAction SilentlyContinue
if ($Err) {
Get-WFALogger -Error -message $("Failed to map LUN: "+ $Err)
}
else {
Get-WFALogger -Info -message $("Mapped LUN successfully")
}
}
else {
Add-NcLunMap -ErrorVariable Err -Path $LunPath -InitiatorGroup $IgroupName -VserverContext $VserverName -ErrorAction SilentlyContinue
if ($Err) {
Get-WFALogger -Error -message $("Failed to map LUN:"+ $Err)
}
else {
Get-WFALogger -Info -message $("Mapped LUN successfully")
}
}