0

How do I call methods within workflow?

I am trying to call "Task" from within a workflow and it seems to be getting ignored? I have provided a watered down nonsense code to illustrate what I'm trying to do. basically call a method within the class in parallel and return the results.

Sample Code

    Class Something {
    [string]Task($item) {
        Start-sleep -Seconds 10
        Return "Result"
    }
    [System.Array]GetSomething($list) {
        workflow GetWF {
            param($listarr)
            ForEach -parallel ($item in $listarr) {
                $res = InlineScript {
                    Write-Host("Starting.." + $using:item)
                    $this.Task($using:item)
                }
            }
            $res 
        }
        Return GetWF -listarr $list 
    } 
}

$list = @('host1','host2','host3','host4')
$Something = [Something]::New()
$Something.GetSomething($list)

Output :

Starting..host1
Starting..host4
Starting..host2
Starting..host3

Desired Result from Example

The issue is how do I get my results back as an array ? In this example above I would like to see the final result to be this:

$Result = @("Result","Result","Result","Result")

Mike Q
  • 6,716
  • 5
  • 55
  • 62
  • what's the point of using a `workflow` ? any particular reason ? – Santiago Squarzon Sep 03 '22 at 05:21
  • to put it simple, if you were looking into `workflow` because of `ForEach -parallel` note that it will not bring any performance gain, `workflow` is meant for long running tasks and it will be always slower than a linear loop, i.e.: test `workflow something { foreach -parallel ($i in 0..100) { $i } }; something` to see how slow it can be. For multithreading you should look into runspaces or [`ThreadJob`](https://learn.microsoft.com/en-us/powershell/module/threadjob/?view=powershell-7.2) or `ForEach-Object -Parallel` if running PS Core. – Santiago Squarzon Sep 03 '22 at 15:18
  • @SantiagoSquarzon From my testing and reading it would and does work well for support cmd-lets and custom functions. I have also used jobs to do this but I wanted to use workflow because it's more straight forward. As an example I tested this with a function that sleeps for 30 seconds, and called that 4x. Workflow completed in about 30 sounds, the for loop took 30x4 seconds. Hence, much faster. I've also used it recently to ping a bunch of hosts in parallel. Much faster. – Mike Q Sep 03 '22 at 17:42
  • Wouldn't call that a valid test but if it serves your need – Santiago Squarzon Sep 03 '22 at 17:44
  • It seems to be a very common quick and dirty way to do this when it deems to be the right tool. – Mike Q Sep 03 '22 at 17:56
  • I encourage you to read [this answer](https://stackoverflow.com/a/41797153/15339544) reg. `workflow`. If you want true multi-threading go with the options I named before – Santiago Squarzon Sep 03 '22 at 19:40
  • That wasn't particularly easy to implement either. I ended up just turning my Class back into Functions and used Start-Job etc. to run all of my tasks in parallel. It reduced 10 machines of API calls from 1 minute to 10 seconds . – Mike Q Sep 04 '22 at 22:44

0 Answers0