Powershell foreach loop spawning jobs, but they're not running concurrently

Viewed 67

I'm writing a Powershell script that monitors a directory for changes, and when detected, spawns a job to deal with the file:

get-childitem | foreach-object {
   $name = $_.Name
   $newname = "work-"+$name

   Rename-Item -Path $name -NewName $newname
   Start-Job -ScriptBlock {
      $newvar = $args[0]
      ffmpeg -i $newvar -acodec flac -f ogg $newvar".flac"
      } -ArgumentList $newname
   }
}

When running the script, jobs are spawned correctly, but processing doesn't commence after the third job. For example:

Job 1 --> Complete
Job 2 --> Complete
Job 3 --> Complete
Job 4 --> Running
Job 5 --> Running
Job 6 --> Running

And the script does not progress. Jobs 4-6 are left perpetually in the running state. There is no output file created from ffmpeg, so I know the script block hasn't run.

I have tried setting Set-PSDebug -Trace 2 to monitor the script - variables are correctly being set, and jobs are spawning correctly. However, as the script is running, I can't do a Receive-Job or Get-Job to see what's going on inside of them.

Any advice on how to proceed?

0 Answers
Related