0

I have been using just a standard foreach loop, which executes as it should. However, I have an 8 core processor and 16GB of RAM and thought I would utilize such with a Parallel.foreach loop. Well, this syntax works as it should

$filelist = Get-ChildItem G:\GoodFilesForServer\ -filter *.mkv
$num = $filelist | measure
$filecount = $num.count 
$i = 0;
ForEach ($file in $filelist)
{
    $Workflow:i++;
    $oldfile = $file.DirectoryName + "\" + $file.BaseName + $file.Extension;
    $newfile = $file.DirectoryName + "\" + $file.BaseName + ".mp4";      
    $progress = ($Workflow:i / $filecount) * 100
    $progress = [Math]::Round($progress,2)
    Clear-Host
    Write-Host -------------------------------------------------------------------------------
    Write-Host Handbrake Batch Encoding
    Write-Host "Processing - $oldfile"
    Write-Host "File $Workflow:i of $filecount - $progress%"
    Write-Host -------------------------------------------------------------------------------
    Start-Process "C:\Program Files\HandBrake\HandBrakeCLI.exe" -ArgumentList "-i `"$oldfile`" -t 1 --angle 1 -c 1 -o `"$newfile`" -f mp4  -O  --decomb --modulus 16 -e x264 -q 32 --vfr -a 1 -E lame -6 dpl2 -R Auto -B 48 -D 0 --gain 0 --audio-fallback ffac3 --x264-preset=veryslow  --x264-profile=high  --x264-tune=`"animation`"  --h264-level=`"4.1`"  --verbose=0" -Wait -NoNewWindow
    del $oldfile
}

Simple enough, but it is not Parallel this syntax does not work, and no error is shown to help me determine why it does not work.

Workflow ParallelTest
{
  $filelist = Get-ChildItem G:\GoodFilesForServer\ -filter *.mkv
  $num = $filelist | measure
  $filecount = $num.count 
  $i = 0;
  ForEach -Parallel -ThrottleLimit 20 ($file in $filelist)
  {
    $Workflow:i++;
    $oldfile = $file.DirectoryName + "\" + $file.BaseName + $file.Extension;
    $newfile = $file.DirectoryName + "\" + $file.BaseName + ".mp4";      
    $progress = ($Workflow:i / $filecount) * 100
    $progress = [Math]::Round($progress,2)
    Clear-Host
    Write-Host -------------------------------------------------------------------------------
    Write-Host Handbrake Batch Encoding
    Write-Host "Processing - $oldfile"
    Write-Host "File $Workflow:i of $filecount - $progress%"
    Write-Host -------------------------------------------------------------------------------
    Start-Process "C:\Program Files\HandBrake\HandBrakeCLI.exe" -ArgumentList "-i `"$oldfile`" -t 1 --angle 1 -c 1 -o `"$newfile`" -f mp4  -O  --decomb --modulus 16 -e x264 -q 32 --vfr -a 1 -E lame -6 dpl2 -R Auto -B 48 -D 0 --gain 0 --audio-fallback ffac3 --x264-preset=veryslow  --x264-profile=high  --x264-tune=`"animation`"  --h264-level=`"4.1`"  --verbose=0" -Wait -NoNewWindow
    del $oldfile
  }
}

What should I alter in my Workflow syntax in order to be able to run the conversion in Parallel (btw, if a ThrottleLimit of 20 is insane to try, we can decrease that)

7
  • you surely can't write-host inside a workflow without an InlineScript. what happens if you delete all x-host from your workflow?
    – SimonS
    Jul 6, 2016 at 9:59
  • @SimonS - if I delete all the write-host lines, still nothing appears to takes place. For example, in my powershell window when code was executing it would hang on the PS C:\Users\Me line, but when I execute this, there is no delay it immediately writes the syntax to the window and a new C:\Users\Me line is shown (which is what happens when the code has completed execution) Jul 6, 2016 at 11:38
  • hmm... are you loading the workflow into the cache first and then call it by executing ParallelTest? a workflow behaves like a function so it needs to be loaded into the cache first and then called.
    – SimonS
    Jul 6, 2016 at 15:24
  • @SimonS - I am not sure what you mean by "Loading the workflow into the cache" but I am calling it by using ParallelTest Jul 6, 2016 at 15:28
  • you are executing your whole workflow parallelTest { #stuff } (this is the loading into cache) and then you're executing ParallelTest (call the workflow). that's what I mean. Just to be sure.
    – SimonS
    Jul 6, 2016 at 15:31

0

You must log in to answer this question.

Browse other questions tagged .