I'm running a PowerShell script triggered by MSBuild to distribute output binaries from my .NET application to a file server. Before copying over the new binaries, I create a zip file of the old files using the `Compress-Archive` command. However, I've noticed an odd issue where the zip file sometimes misses files entirely; it might include only one or two files or sometimes all of them. This happens even though there are usually a dozen files total that should be zipped. Does anyone have an idea why this might be happening?
3 Answers
Consider if there's a chance that the files being skipped are located in subdirectories. I usually just give `Compress-Archive` a path to the parent directory and let it handle things from there, rather than specifying the children directly. That might be worth trying.
You could experiment with using the pipeline method instead. For example, you can do something like `Get-ChildItem -Path C:LogFiles | Compress-Archive -DestinationPath C:ArchivesPipelineDir.zip`. Plus, using the `-PassThru` parameter could help you check if that makes the process blocking.
It might be a timing issue. You could be running the `Compress-Archive` command too quickly. Try adding a `Start-Sleep` or using a `do-while` loop to check if the compression is completed before proceeding. Some have suggested that `Compress-Archive` might run in a different thread without waiting for it to finish.

I think you're right about it being a race condition. I didn't realize `Compress-Archive` might not wait for completion. I tried adding a `do-while` loop to check for the .zip file, but that wasn't effective. Have you had experience with PowerShell jobs? Would that help in ensuring that the compression finishes before moving on?