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.