Search code examples
powershellstart-jobcompress-archive

PowerShell: using Compress-Archive with Start-Job won't work


I'm trying to use PowerShell to compress a bunch of video files on my H:\ drive. However, running this serially would take a long time as the drive is quite large. Here is a short snippet of the code that I'm using. Some parts have been withheld.

$shows = Get-ChildItem H:\
foreach($show in $shows){
    Start-Job -ArgumentList $show -ScriptBlock {
        param($show)
        $destPath = "$($show.DirectoryName)\$($show.BaseName).zip"
        Compress-Archive -Path $show.FullName -DestinationPath $destPath
    }
}

When I run a Get-Job, the job shows up as completed with no reason in the JobStateInfo, but no .zip was created. I've run some tests by replacing the Compress-Archive command with an Out-File of the $destPath variable using Start-Job as well.

Start-Job -ArgumentList $shows[0] -ScriptBlock {
    param($show)
    $show = [System.IO.FileInfo]$show
    $destPath = "$($show.DirectoryName)\$($show.BaseName).zip"
    $destPath | Out-File "$($show.DirectoryName)\test.txt"
}

A text file IS created and it shows the correct destination path. I've run PowerShell as an Administrator and tried again but that doesn't appear to work either. Not sure if it matters, but I'm running on Windows 10 (latest).

Any help would be appreciated. Thanks!


Solution

  • For some reason, inside the job, the serialized fileinfo object has no basename (it's a scriptproperty). If you have threadjobs, that works.

    dir | start-job { $input } | receive-job -wait -auto | 
      select name,basename,fullname
    
    Name      basename FullName
    ----      -------- --------
    file1.txt          C:\Users\js\foo\file1.txt
    file2.txt          C:\Users\js\foo\file2.txt
    file3.txt          C:\Users\js\foo\file3.txt