1

I have a Azure DevOps pipeline (classic) to build our code. At the end of the pipeline, I'm using the task PowerShellV2 to execute a powershell script. When executing that script, sometimes it fails with the following error:

Compress-Archive : The term 'Compress-Archive' is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:4 char:5
+     Compress-Archive -Path "$dirToZip\\*" -DestinationPath "$zipFileP …
+     ~~~~~~~~~~~~~~~~
+ CategoryInfo          : ObjectNotFound: (Compress-Archive:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException

The success/fail ratio is about 25/1. The task uses the installed PowerShell version C:\Program Files\PowerShell\7\pwsh.exe. That's at least PowerShell v7.2.6 (depends on the build server used. The fails and successes are not dependent on the build servers, so they occur randomly on everyone of them.)

Here's the failing part of the script:

# Zip form directories
Write-Host "`nZip form directories:"
Get-ChildItem -Directory $targetFormsDir | ForEach-Object -Parallel {
    $dirToZip = $_.FullName
    $zipFilePath = $dirToZip + ".zip"
    Compress-Archive -Path "$dirToZip\\*" -DestinationPath "$zipFilePath" -CompressionLevel Optimal -Force
    Write-Host "'$dirToZip' -> '$zipFilePath'"
}

I suppose, it has something to do with parallel execution, but if so, I would like to understand, why.

roli09
  • 817
  • 1
  • 8
  • 20
  • 1
    I believe the issue is likely similar to the one from this question https://stackoverflow.com/q/74257757/15339544, `Compress-Archive` is one wrapper on top of each other, i.e.: `& (Get-Module Microsoft.PowerShell.Archive) { $function:CompressArchiveHelper }` in any case, compressing file in parallel will most likely not bring any performance improvement. I think this problem might be solved by directly using the .NET Apis – Santiago Squarzon Nov 08 '22 at 13:00
  • Thx! So the easiest/quickest solution will be just removing `-Parallel`, right? – roli09 Nov 08 '22 at 13:12
  • 1
    That will fix your issue for sure, but if you want to give parallel processing a try, the best option would be to point to the APIs directly, you could use `[IO.Compression.ZipFile]::CreateFromDirectory` which is pretty straight forward, but this one only works with directories as source – Santiago Squarzon Nov 08 '22 at 13:14
  • Thank you very much. I'm gonna try without `-Parallel` and if it's significant slower than before, then I'll try your second suggested approach. – roli09 Nov 08 '22 at 13:29
  • 1
    Other option is to include the function from [this answer](https://stackoverflow.com/a/72611161/15339544) inside your parallel scriptblock, it makes use of the .NET APIs and has similar functionalities to `Compress-Archive` – Santiago Squarzon Nov 08 '22 at 13:30
  • Hi @roli09, have you already tried santiago's suggestions? Are the suggestions helpful? Does the issue exist now? Thanks. – Antonia Wu-MSFT Nov 16 '22 at 05:59
  • Yes, I just removed `-Parallel` and then it worked! – roli09 Nov 16 '22 at 06:44
  • 1
    Thanks for reply. Just as a reminder, you could post the answer and vote in your answer. That will help others' who meet the same issue with you find the answer quicker and easier. – Antonia Wu-MSFT Nov 18 '22 at 05:34

1 Answers1

0

As @Santiago Squarzon pointed out, I just had to get rid of -Parallel. Without it, my command was a bit slower, which wasn't a concern in my case.

For further details, please read @Santiago Squarzon comments.

roli09
  • 817
  • 1
  • 8
  • 20