Powershell - Zip Verification Fail Condition RRS feed

  • Question

  • Hi guys,

    I hope somebody can point me in the right direction....

    I wrote a script which archives a folder with a name starting with "BU", then it copies the zip file to a specific server and then deletes the original files. My script runs well but I would like to put some kind of verification in place. Since the files inside the BU folders are extremely important, I want to make sure that the zip file is not corrupted before deleting the original files.

    My thinking was a loop which will run until it gets a message saying that "Everything is OK". In other words, if it doesn't get the "Everything is OK" message it will restart and do the archiving again. Now although I managed to get the script going (with a lot of help from Google), I am not experienced enough to know where even to start to get the loop inside my script. My first step would probably be to try and capture the verification message but I have no idea how this is done. This is what I have so far:

    function create-7zip([String] $aDirectory, [String] $aZipfile){
        [string]$pathToZipExe = "$($Env:ProgramFiles)\7-Zip\7z.exe"; 
        [Array]$arguments = "a", "-tzip", "$aZipfile", "$aDirectory", "-mx9"; 
        & $pathToZipExe $arguments;

    function verify-7zip([String] $aZipfile){
        [string]$pathToZipExe = "$($Env:ProgramFiles)\7-Zip\7z.exe"; 
        [Array]$arguments = "t", "$aZipfile"; 
        & $pathToZipExe $arguments;

    cd D:\Logs\
    create-7zip BU* Logs_servername_"$(get-date -f yyyy-MM-dd).zip" 
    Robocopy D:\Logs\ \\SERVER_IP\e$\Logs\Archive\ Logs_server_*.zip
    Remove-Item D:\Logs\BU* -recurse
    Del D:\Logs\Logs_servername_*.zip

    I know I need to use the following to get the verification done:

    verify-7zip Logs_servername_*.zip

    But how do I capture the verification output and place it inside a loop?

    Any help will be greatly appreciated.
    Wednesday, June 17, 2015 2:05 PM


All replies

  • You are over-complicating this.

    Run your zips and check the exit code on each one.  If it is non-zero look up the code and report it and decide what to do next.

    After a successful zip then delete only those files and output a "success" message

    You will have to do this.  Consider your homework and that it will teach you how to design a solution.


    Wednesday, June 17, 2015 3:33 PM
  • The "Everything is OK" message right after the zip process, is this verification also confirmation that the zipped file is not corrupted? If so then obviously I can remove the verify-7zip function from the script. The whole point of the script is to automate the process so I can not check the error codes every time. There should only be 2 conditions, zip is corrupted or it is not. If corrupted it should zip again, if not corrupted it should continue. But how do I capture the output message after the zip process is completed? 
    Thursday, June 18, 2015 6:44 AM
  • 7-Zip returns a code that tells how it completed.  Test that code to see if it completed without errors.  THat is allyou have to do,

    Look up 7-Zip result codes in the documentation.

    This is how all WIndows command line programs work.


    • Marked as answer by johnslippers Thursday, June 18, 2015 9:28 AM
    Thursday, June 18, 2015 8:32 AM
  • Hi jrv,

    Thanks for the reply.

    I am going to use an IF statement instead that would look for a failed error code (using $LastExitCode). If the archiving process fails I will let the script stop and send an email notification so that we can fix the cause of the failure manually.

    Thank you for pointing out the error codes.
    Thursday, June 18, 2015 9:28 AM