There is an error handling issue

If I call

$Cc = (Invoke-KFTemplate -item $a -template $item["Cc"] -LocalConfig $Wildcards -ThrowErrors:$false -ErrorAction SilentlyContinue)

and the “Cc” is empty the workflow breaks but I told the command to SilentlyContinue and throw no errors. Mistake or little but?

Markus,

did you get an exception in Debug mode or during automatic execution?

We have tested this. You get an error like this:

26832   exe3  0016  2019-02-04-21-40-31  Error       Message:

PowerShell error:
    Cannot bind argument to parameter 'Template' because it is null.

No Error Details.

Invocation Info:
      Text: System.Management.Automation.InvocationInfo
      Line: $Cc = (Invoke-KFTemplate -item $a -template $item["Cc"] -LocalConfig $Wildcards -ThrowErrors:$false -ErrorAction SilentlyContinue)

       Col: 45
    Script: C:\Source\wfengine\_workflows\empty-template-test\Workflow Script.ps1


Script Stack Trace:
    at <ScriptBlock>, C:\Source\wfengine\_workflows\empty-template-test\Workflow Script.ps1: line 5
    at <ScriptBlock>, <No file>: line 8

Error Id: ParameterArgumentValidationErrorNullNotAllowed,kenaflow.Cmdlets.InvokeKFTemplate

Stack Trace: **empty**

Inner Exception
    Message:
    Cannot bind argument to parameter 'Template' because it is null.

    Stack Trace: **empty**

This is not an error that kenaflow can handle. It’s a pure PowerShell error. -ErrorAction (PowerShell) and -ThrowErrors are not able to catch this error.

The reason is that the -Template parameter of cmdlet Invoke-KFTemplate is $null. PowerShell (not kenaflow) cannot bind the $null value the the mandatory parameter -Template.

In our understanding it’s an error to pass an empty template into the template engine.

In this default cmdlet you get the same result when you pass in an $null value as parameter:

get-content -Path $null -ErrorAction SilentlyContinue

It makes no sense to read content from a file that is not specified. -ErrorAction does not work here eighter.

We think about using “AllowEmptyString” and “AllowNull” as attributes for the mandatory parameter -Template of Invoke-KFTemplate.

You will get it in version 2.0.29.

Many thanks for the detailed answer … and for the new feature, which makes things easier.

Alternatively, I had experimented with an IF condition, which would have worked, but I use it quite often.

if($item['Cc'] -ne $null) {
  $Cc = (Invoke-KFTemplate -item $a -template $item["Cc"] -LocalConfig $Wildcards -ThrowErrors:$false -ErrorAction SilentlyContinue)
} else {
  $Cc = ""
}