0

I am trying to create a Powershell workflow with a number of functions inside it.

My code, and the error...

workflow New-AutomationVM
{
    function Pre-DeploymentChecks
    { Write-Output $true }

    function Deploy 
    { $checkResult = Pre-DeploymentChecks }

    Deploy
}
New-AutomationVM 

The term 'Pre-DeploymentChecks' is not recognized as the name of a cmdlet, function, script file, or operable program.

My intention was to create a deploy function that encapsulated all of the other functions. But it seems not possible to call a function from within a function within a workflow. i.e. I can call Pre-DeploymentChecks from the workflow, but not the function.

Obviously I could just put that logic outside of the functions, but this approach seemed a little tidier.

Am I doing something stupid! or is this just not possible within Powershell workflows?

1 Answer 1

1

Use nested workflows instead:

workflow New-AutomationVM
{
    workflow Pre-DeploymentChecks
    { Write-Output $true }

    workflow Deploy 
    { $checkResult = Pre-DeploymentChecks }

    Deploy
}
New-AutomationVM 

This also allows you to use workflow constructs inside each nested workflow, such as sequence or parallel:

workflow New-AutomationVM
{
    workflow Pre-DeploymentChecks
    {
        function Check-IP
        { return $true }

        function Check-Name
        { return $true }

        function Check-Role
        { return $false } 

        $CheckResults = parallel {
            sequence {
                Check-IP
                Check-Name
            }
            Check-Role
        }

        ($CheckResults |Where-Object {-not $_}) -le 1 
    }

    workflow Deploy 
    {
        Pre-DeploymentChecks
    }

    Deploy
}
New-AutomationVM 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.