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 )
1
  • 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 or the PoshRSJob module. Commented Oct 13, 2017 at 22:14

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

3 Comments

This does what I need. Thanks :)
Any chance you can show how to refactor such that I can do something like $cmd1 = "dir" and then have $t1 = { cmd /k $cmd1 `& pause } ?
@ttugates: To dynamically create a scriptblock see e.g.stackoverflow.com/a/23948626/1701026

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.