0

I have a powershell script that at one point will call 2 other powershell scripts to run. It runs one script to completion, then the other, but this causes it to take longer. Can I force the script to execute the other scripts and continue cycling through? When I used to run these scripts manually I would have 20-30 sessions running and walk away while it worked. What I wrote took the monotony of clicking through them manually

Here's the parent script:

$List = Get-Content C:\archive\${env:id}.txt 
$Batch = New-Object System.Collections.ArrayList

foreach ($Data in $List){
    if ($Data -eq "" -or $data -eq $List[-1]){
        $ProjectName = $Batch[0]
        out-file C:\archive\"$ProjectName".txt

        foreach($Data in $Batch -ne $Batch[0]){
            Add-Content -Path C:\archive\"$ProjectName".txt -Exclude 
            $Batch[0] -Value $Data
        }
   -->  C:\archive\GetPrograms.ps1 $ProjectName 
   -->  C:\archive\GetNetwork.ps1 $ProjectName
        $Batch = New-Object System.Collections.ArrayList
    }
    else{
        [void]$Batch.Add($Data)      
    }
}

The parent script is not contingent on the data produced by the other 2 scripts. It simply executes them by passing in data

  • 1
    I commend to your attention the output of [`Get-Help Start-Process -Full`](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/start-process?view=powershell-6). – Jeff Zeitlin Feb 07 '18 at 18:41
  • Thank you for that, I was able to ask a more exact question and found my solution here: https://stackoverflow.com/questions/17776426/powershell-start-process-to-start-powershell-session-and-pass-local-variables I used Start-Process Powershell -ArgumentList "C:/archive/script $ProjectName" – user2021129 Feb 07 '18 at 19:09
  • You should delete this question then – EBGreen Feb 07 '18 at 19:19
  • What I meant was I was able to google more specifically what I was looking for and that thread popped up – user2021129 Feb 07 '18 at 19:20
  • So did you find a solution or not? – EBGreen Feb 07 '18 at 19:48
  • I did, I used 'Start-Process -ArgumentList "C:/archive/script $ProjectName"' – user2021129 Feb 08 '18 at 19:40

1 Answers1

0

Honestly, based on your use case description, you really want to be looking at Parallel job/task/script processing.

Here is a post along the lines of what should satisfy your goals.

How do I run my PowerShell scripts in parallel without using Jobs?

Update - While this answer explains the process and mechanics of PowerShell runspaces and how they can help you multi-thread non-sequential workloads, fellow PowerShell aficionado Warren 'Cookie Monster' F has gone the extra mile and incorporated these same concepts into a single tool called Invoke-Parallel - it does what I describe below, and he has since expanded it with optional switches for logging and prepared session state including imported modules, really cool stuff - I strongly recommend you check it out before building you own shiny solution!

https://serverfault.com/questions/626711/how-do-i-run-my-powershell-scripts-in-parallel-without-using-jobs

postanote
  • 15,138
  • 2
  • 14
  • 25