Why Does Compress-Archive Sometimes Miss Files in My PowerShell Script?

0
3
Asked By SillySauce88 On

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

Answered By CuriousCoder42 On

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.

Answered By ZipWizard55 On

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.

Answered By TechieTurtle99 On

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.

FastFox24 -

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?

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.