Nested function not recognized as the name of a cmdlet
Below is the file (highly simplified versions) of interest:

main-func.psm1:
function main-func
{
    [CmdletBinding()]
    param
    ()

    Write-Verbose "In main-func"
    main-workflow
}

workflow main-workflow
{
    [CmdletBinding()]
    param 
    ()

    Write-Verbose "In main-workflow"
    func-outer
}

function func-outer
{
    [CmdletBinding()]
    param 
    ()
 
    Write-Verbose "In func-outer"
    func-inner
}

function func-inner
{
    [CmdletBinding()]
    param
    ()

    Write-Verbose "In func-inner"
}

Export-ModuleMember -function main-func
Now I open Windows Powershell and execute the following commands:
> Import-Module .\main-func.psm1
> main-func -Verbose

The output that I get is as below:
VERBOSE: In main-func
VERBOSE: [localhost]:In main-workflow
VERBOSE: [localhost]:In func-outer
The term 'func-inner' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
    + CategoryInfo          : ObjectNotFound: (func-inner:String) [func-outer], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException,func-outer
    + PSComputerName        : [localhost]
But if I replace main-workflow in function main-func with func-outer, then it works.

I'm pretty new to Powershell and Workflows and using workflows is a requirement. Can somebody please explain what's wrong here?


May 25th, 2015 2:09am

Hi Bittu,

The main issue is the script below:

workflow main-workflow
{
    [CmdletBinding()]
    param 
    ()

    Write-Verbose "In main-workflow"
    func-outer
}

function func-outer
{
    [CmdletBinding()]
    param 
    ()
 
    Write-Verbose "In func-outer"
    func-inner
}

function func-inner
{
    [CmdletBinding()]
    param
    ()

    Write-Verbose "In func-inner"
}

A Workflow cannot see beyond what's immediately available in the scope. So nested functions are not working with a single workflow, because it cannot see them.

Nested workflows can call workflows and functions in the current scope and any parent scope.

You can refer to the script below:

function fun2 
            {
                "in fun2"
            }
workflow workflow1
{
    function fun1 
    {
        "in fun1"            
                     
    }
	fun1
	fun2
    "in workflow1"
    
}

workflow1

For more detailed about nested function and workflow, please refer to the articles below:

Adding Nested Functions and Nested Workflows

Understanding scope of functions in powershell workflow

If there is anything else regarding this issue, please feel free to post back.

Best Regards,

Anna Wang

Free Windows Admin Tool Kit Click here and download it now
May 25th, 2015 10:39pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics