3

Use a recursive function next to a workflow

 2 years ago
source link: https://www.codesd.com/item/use-a-recursive-function-next-to-a-workflow.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Use a recursive function next to a workflow

advertisements

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?


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


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK