Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a recursive function beside a workflow

I have a PowerShell script whose purpose is to get a list of files then do some work on each file.

The list of files is generated by a recursive function like this:

function Recurse($path)
{
    .. create $folder

    foreach ($i in $folder.files) { 
        $i
    }
    foreach ($i in $folder.subfolders) {
        Recurse($i.path)
    }
}

Separate from this function i do a workflow that takes a list of files and do the work (in parallel) on each file. The code looks something like this:

workflow Do-Work {
    param(
        [parameter(mandatory)][object[]]$list
    )
    foreach -parallel ($f in $list) {
        inlinescript {
            .. do work on $Using:f
        }
    }
}

These two parts are then combined with the following logic:

$myList = Recurse($myPath)
Do-Work -list $myList

The problem is that this generates an error:

A workflow cannot use recursion.
    + CategoryInfo          : ParserError: (:) [], ParseException
    + FullyQualifiedErrorId : RecursiveWorkflowNotSupported

Why is this happening when the recursive function and the workflow is separate? Is there any way to work around this issue?

like image 754
Artog Avatar asked May 17 '26 21:05

Artog


1 Answers

Recursive calling is not permitted in workflows.

Give your path directly:

Instead of this:

Recurse($myPath)

do this:

Recurse $myPath

You can refer to this article:

Adding Nested functions and Nested Workflows