A workflow cannot use recursion

I have the code below which has been fine until I installed V5.1 of PS. Now everytime I load the module with this code I get the recursion error. Cant figure out why it has changed…

Workflow Test-WFConnection {

    # Run multiple parallel "pings" so it runs quicker


  foreach -parallel ($computer in $computers) {

        Computer = $computer
        Reachable = (Test-Connection -ComputerName $computer -Count 1 -Quiet)

I ran your code in a Windows 2016 domain - PowerShell 5.1 - and it worked.

Test-Connection uses the Win32_PingStatus WMI class and requires a DCOM connection to the remote machine. I’d suggest testing Test-Connection against one of the failing remote machines directly rather than in the workflow. I’d also suggest that you test get-WMIObject against the failing machine to help determine if there is a DCOM issue

The code works. The problem is that when I load the module that has the above code in it I get the ‘A workflow cannot use recursion’ error. Don’t know its giving me that error when the code actually works.

Do you call the workflow within the module code?

No - the module is imported at the start of an external script and the workflow is launched from there.
This code has not changed - it just started showing this ‘error’ since V5 or 5.1 of WMF.

It appears to be a bug. I just removed the workflow from my module.

It appears you can use workflows with modules with v5.1 and later but it requires a bit of a trick.


I posted this same solution SO: https://stackoverflow.com/questions/45183731/using-a-recursive-function-beside-a-workflow


For example, this workflow can be used to start/stop service in parallel:

Workflow Invoke-ServiceInParallelWF
    Workflow to stop/start services in parallel on a server.
    Utilizes a workflow to start/stop services running on a server in parallel to shorten the start/stop duration.
    if (!($Name.Count -gt 0))
        throw "No services provided!"
# run parrallel on services argument
foreach -parallel ($svc in $Name){

        #build object first for consistency
        # Wrap in exception handler
            #Perform the desired service action
            if ($using:action -eq 'stop') {
                Stop-Service -name $using:svc -ErrorAction stop
            } elseif ($using:action -eq 'start') {
                Start-Service -name $using:svc -ErrorAction stop
            } else {
        # generate object back to workflow
        New-Object -TypeName PSCustomObject -Property $props


If you put this in your psm1 file and try to import it, it will fail with this error:
At C:\Source\Powershell\Common\Modules\MyModule\MyModule.psm1:1 char:1
+ #
+ ~
A workflow cannot use recursion.
+ CategoryInfo : ParserError: (:) [], ParseException
+ FullyQualifiedErrorId : RecursiveWorkflowNotSupported
To embed this in the module, do NOT put it in the .psm1 file, create a separate ps1 file and place it in the module folder. e.g. Invoke-ServiceInParallelWF.ps1

Then in your manifest (psd1) file, modify the ScriptsToProcess to include the ps1 file.


# Script module or binary module file associated with this manifest.
RootModule = 'MyModule.psm1'

# Version number of this module.
ModuleVersion = '1.47.1'

# ID used to uniquely identify this module
GUID = 'bd4390dc-a8ad-4bce-8d69-f53ccf8e4163'

# Author of this module
Author = 'Justin Marshall'

# Script files (.ps1) that are run in the caller's environment prior to importing this module.
ScriptsToProcess = @('Invoke-ServiceInParallelWF.ps1')

Finally, import your module and test the function:

PS C:\source\powershell> Import-Module MyModule -force

PS C:\source\powershell> Invoke-ServiceInParallelWF -Action Start -Name w3svc

Service : w3svc
Action : Start
Result : Success
Error :
PSComputerName : localhost
PSSourceJobInstanceId : 1a564d5d-f363-44b7-a27e-88670764de2d