Search code examples
powershellcommand-linewinzip

Catch wzzip return codes in PowerShell


I currently have a PowerShell script that zips items that aren't already in a specified folder. I need my script to return wzzip's error codes if one is thrown, so I can handle it in the rest of my script. I've provided a bit of my script below:

foreach ($File in Get-ChildItem $Path\$Item -Recurse | Where {$_.PSIsContainer -ne $true}) {
    WZZIP.EXE $Path\$Item.zip $File.FullName
}

Solution

  • PowerShell automatically stores the exit code of external programs in the automatic variable $LASTEXITCODE.

    WZZIP.EXE ...
    if ($LASTEXITCODE -ne 0) {
        # do stuff here
    }