Question

In PowerShell advanced functions, the [CmdletBinding()] attribute is used to add useful metadata, and ensure that the function acts like a compiled PowerShell cmdlet. Some of this useful information can be accessed using the $PSCmdlet automatic variable.

In PowerShell Workflow, the [CmdletBinding()] attribute can be declared in a workflow definition (without receiving an error), but using the $PSCmdlet inside the workflow yields the following error message:

At C:\Demo\Workflow - Demo 04.ps1:11 char:5
+     $PSCmdlet;
+     ~~~~~~~~~
The variable 'PSCmdlet' cannot be used in a script workflow.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : VariableNotSupportedInWorkflow

My question boils down to this: Is there any benefit to using the [CmdletBinding()] attribute on a PowerShell workflow?

Was it helpful?

Solution

For the most part, the CmdletBinding attribute won't do much for a workflow. Workflows emulate cmdlets in that they won't bind unknown parameters, so [CmdletBinding()] doesn't change that aspect of parameter binding any.

You can specify the default parameter set with [CmdletBinding(DefaultParameterSetName='DefaultSet')], that may be useful.

Note that if you use the Parameter attribute, a function/workflow will implicitly use cmdlet binding. Workflows can't take advantage of everything you could use with the Parameter attribute, e.g. accepting values from the pipeline won't be useful because the workflow can't specify a process block. You can still specify parameter sets, positional arguments, and help messages which might be useful.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top