0

I am trying to do something like the following:

Workflow spawnInParallel() {
  $task1 = { "Dir C:\; pause" }
  $task2 = { "Dir C:\Windows; pause" }
  $task3 = { "ng build --prod; pause" }

  $tasks = $task1, $task2, $task3    
  ForEach -Parallel ($task in $tasks) {
    start powershell "& $task"
  }
}

spawnInParallel

I am encapsulating DOS commands as a string simply because I don't know a cleaner way.

I don't know if this is a task for Workflow, Jobs or something else.. Looking to hopefully keep it clean and simple.. Using PS 5.1.

The desired result would be the original, plus 3 new Powershell Windows that run the command and not immediately close.

Implementation Using Updated Accepted Answer

Workflow showParallel() {    
    param([String[]] $procs)

    $tasks = @()
    foreach ($proc in $procs) {
        $tasks += " cmd.exe /c 'Echo Running $proc&$proc&pause' "
    }

    foreach -parallel ($task in $tasks) {
        Start-Process powershell "-c $task"
    }
}

$app1BuildCmd = "ng build --app app1 --prod --output-path $app1Root"
$app2BuildCmd = "ng build --app app2 --prod --output-path $app2Root"
$app3BuildCmd = "ng build --app app3 --prod --output-path $app3Root"

showParallel -procs @($app1BuildCmd , $app2BuildCmd , $app3BuildCmd )
ttugates
  • 5,818
  • 3
  • 44
  • 54
  • PowerShell workflows suck. Or, at least, what workflows are designed to do is not what anybody who writes scripts ever wants to do because they have a ton of restrictions. PowerShell multithreading is best handled with `System.Management.Automation.Runspaces`. Take a look at the [Hey Scripting Guy primer on runspaces](https://blogs.technet.microsoft.com/heyscriptingguy/2015/11/26/beginning-use-of-powershell-runspaces-part-1/) or the [PoshRSJob module](https://github.com/proxb/PoshRSJob). – Bacon Bits Oct 13 '17 at 22:14

1 Answers1

1

Stringing together a bunch of DOS commands may be tricky, but can be done or further refactored...

This appears to do what you want, or at least can be used to achieve your final result...

Workflow showParallel()
{
    $t1 = {
        cmd /k echo t1 in cmd here...`&pause
    }
    $t2 = {
        cmd /k echo t2 in cmd here...`&pause
    }
    $t3 = {
        cmd /k echo t3 in cmd here...`&pause
    }

    $tasks = $t1, $t2, $t3
    foreach -parallel ($task in $tasks)
    {
        Start-Process powershell "-noexit `"$task`""
    }
}

showParallel

UPDATED per request from OP

Workflow showParallel()
{
    $name1 = 't1!'
    $run1 = "echo $name1 in cmd here..."

    $t1 = "
        cmd.exe /c '$run1&pause'
    "

    $t2 = {
        cmd /c echo t2 in cmd C here...`&pause
    }
    $t3 = {
        cmd /k echo t3 in cmd K here...`&pause
    }

    $tasks = $t1, $t2, $t3
    foreach -parallel ($task in $tasks)
    {
        $task
        #Start-Process powershell "-noexit -c $task"
        Start-Process powershell "-c $task"
    }
}

showParallel
Kory Gill
  • 6,993
  • 1
  • 25
  • 33