Subscribe

Continue execution of workflow even when it errors

[ Edited ]

So I have a bunch of steps that I would like to continue executing even if one of fails.

I have provided the option -ErrorAction Continue but it still fails.

Looks like this is only for a non-terminating error.

 

I have a sequence of steps like this

 

Invoke-Expression -ErrorAction Continue $expression

Invoke-Expression -ErrorAction Continue $expression

Invoke-Expression -ErrorAction Continue $expression

 

 

The expression varies in every step and I want the rest to continue even when it fails.

 

-Prasad

Re: Continue execution of workflow even when it errors

[ Edited ]

Sometimes ErrorAction doesnot work for non terminating errors, e.g: unable to connect to ONTAP server.

You can use try -- catch block and can continue execution based on the type of exception message.

Re: Continue execution of workflow even when it errors

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")
}
}

 

 

Re: Continue execution of workflow even when it errors

Thanks Gaurab. That was detailed info.

In my case, I have a workflow to deploy a storage system.

But sometimes, it can fail half way after creating the aggregates.

 

So the next time I run, I want the skip the part of creating aggregates since its already created.

One choice is remove the steps and create new references to the aggregates (cumbersome).

 

So I was wondering if there is a any recommended way to skip steps in a workflow when required and I tried ErrorAction which did not work in this case (because trying to create a aggregate that already exists is a terminating error).

I will check out try/catch exceptions.

 

Thanks a bunch,

-Prasad

Re: Continue execution of workflow even when it errors

Hello Prasad,

 

Let me understand the scenario clearly.

Let us take it we have a workflow which constitutes of 9 different steps.

1->2->3->4->5->…->9

Now, let say, the aggregate creation happens in the 3 step (and always passes).

Now the step 4 will execute and for simplicity, lets it will pass.

Then it’s time for 5th step and it will fail/is failing catastrophically.

 

In this situation, what do you want?

Do you want an abort or continue with the other step(s) till end and even if it is in a loop, other iteration should continue??

 

Thanks a lot!

                --Gaurab

Re: Continue execution of workflow even when it errors

Hi Gaurab,

 

Using your scenario, let the 5th one fail and stop execution.

After fixing the issues related to step 5, I will need to rerun the workflow. This time, it will fail at step 3 (aggr creation) since the aggrs already exist.

What I need is to selectively ignore a few steps in reruns (in this case step 3), but access to those variable still need to be available for future steps (like vol creation).

 

Is that possible or is there a workaround ?

 

-Prasad

Re: Continue execution of workflow even when it errors

[ Edited ]

Hello Prasad,

 

Is your workflow always going to create aggregate?

I guess, it is not based on the availability of aggregate it will decide.

 

If this is the case, it can be handling easily using a user option on aggregate creation.

 

Already Existing Aggregate: [true/false]

New Aggregate: [some_new_aggr]

Existing Aggregate: [aggr_list|\/]

 

New Aggregate will take new aggregate name (string type) when Already Existing Aggregate is false. This time Existing Aggregate drop down list will be disabled.

Second time when user is going to run the flow, he will search his aggregate in the Existing Aggregate drop down list.

 

The step, where we are going to create the aggregate (the aggregate creation command), can be handle (enable/disable) using MVEL expression like:

In the advance tab of the command instance,

Use: If the following expression is true:

<your_emvl_expr>

 

Like you can use: $AlreadyExistingAggregate == 'false'

 

Thanks,

                --Gaurab