Running multiple blocks of script at the same time in PowerShell

I am trying to run say 5 to 10 actions at the same time in PowerShell.
As I test I wrote 5 simple scripts to preform simple action on all objects in AD and they all ran fine with little system overhead.  I know this is an absurd way to do it as with Jobs it is SAID to be easy to run multiple script blocks at one time.
I cannot make it work.

For a simple test there is about 200,000 folders.  I want to do a simple get child item and have it dump in in a text file.  Once again this is only an exercise so I may figure out how to use jobs correctly.

I have half the paths in one text file and the other half in another text file.  As you will see below I have 2 jobs but nothing happens when I run them.

Please help


$workfile1 = Get-Content $env:TEMP\worklog1.txt
$workfile2 = Get-Content $env:TEMP\worklog2.txt




start-job  -Name action1   {
foreach ($line1 in $workfile1) {
$mypath1 = "\\es01a5901\STU_HOME\$line1"
Get-ChildItem $mypath1 | Out-File $env:TEMP\scrap1.txt -Append
}
}



start-job -name action2 {
foreach ($line2 in $workfile2) {
$mypath2 = "\\es02a5902\STU_HOME\$line2"
Get-ChildItem $mypath2 | Out-File $env:TEMP\scrap2.txt -Append
}
}

Open in new window

SLPowersEngineerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

QlemoBatchelor, Developer and EE Topic AdvisorCommented:
The jobs do not have access to your vars. So essentially $workfile1 and $workfile2 are not defined inside the jobs, and treated as being empty - nothing runs then in the loop.
For this simple exercise you best read the text files in the jobs itself.
Another approach is to use a construct like
start-job -Name action1 -ArgumentList $workfile1 -ScriptBlock {
  param([String] $file)
  foreach ($line1 in $file)
  {
    Get-ChildItem "\\es01a5901\STU_HOME\$line1" | Out-File $env:TEMP\scrap1.txt -Append
  }
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SLPowersEngineerAuthor Commented:
Thank so much nailed it!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.