0

I am trying to test integrity of ZIP file

It looks like this command is working but I got many details about the file. I want to get a result if the test passed or failed in order to move to the next step.

Any idea how to do it? Something like:

$TestZip = 7z t \\bandit\global\QA\AgileTeam\bandit\Builds\8.6.22.607\8.6.22.607.10.zip

if ($TestZip)
{The test passed}
else
{Test failed}

The output is:

Path = \\bandit\global\QA\AgileTeam\bandit\Builds\8.6.22.607\8.6.22.607.10.zip
Type = zip
Physical Size = 5738248794
64-bit = +
Characteristics = Zip64

Everything is Ok

Files: 902
Size:       5927324719
Compressed: 5738248794

Tried this:

$TestZip = 7z t \\bandit\global\QA\AgileTeam\bandit\Builds\8.6.22.607\8.6.22.607.10.zip | set out
$ok = $out -like '*Everything is Ok*'

if ($ok) {write-host "Integrity test for Zip file passed"}
phuclv
  • 37,963
  • 15
  • 156
  • 475
Bandit
  • 399
  • 3
  • 10

1 Answers1

2

Errors are reported via exit code so just check it. No need to parse the output, you can just redirect the output to null. The way to check exit status in PowerShell is $? and $LASTEXITCODE

7z t $zip_file_path 2>$null 1>$null
if ($?) {
    echo "Success"
} else {
    echo "Failed"
}

Update:

Use if ($LASTEXITCODE -eq 0) instead of if ($?) when stderr is redirected in older PowerShell (7.1.x and older) for more reliable result


$?

Contains the execution status of the last command. It contains True if the last command succeeded and False if it failed.

$LastExitCode

Contains the exit code of the last native program that was run.

about_Automatic_Variables

phuclv
  • 37,963
  • 15
  • 156
  • 475
  • 2
    Great advice; unfortunately, `$?` doesn't work robustly in Windows PowerShell and PowerShell (Core) up to v7.1.x when a `2>` redirection is involved, only `$LASTEXITCODE -eq 0` works reliably there. See [this answer](https://stackoverflow.com/a/59376457/45375) for details. – mklement0 May 04 '22 at 13:21
  • So I need to use $LASTEXITCODE -eq 0 instead of S$? When the script run I can see the information about the file and I want to see only success or failed – Bandit May 04 '22 at 13:36
  • @Bandit if you want to see the output then don't redirect it. In case stderr is redirected then use `if ($LASTEXITCODE -eq 0)` – phuclv May 04 '22 at 13:43
  • I mean when the 7z t is running there information that I can see and I want to hide it – Bandit May 04 '22 at 14:22
  • This is printed when running the command 7-Zip 21.07 (x64) : Copyright (c) 1999-2021 Igor Pavlov : 2021-12-26 Scanning the drive for archives: 1 file, 5662785272 bytes (5401 MiB) – Bandit May 04 '22 at 14:56
  • @Bandit that's what I said in the answer: redirect the streams. `2` is the stderr and `1` is `stdout`. Did you even try to run it? Redirecting them to `$null` will output nothing on the screen – phuclv May 04 '22 at 15:01
  • Just now I saw the 2>$null 1>$null Should I need to do both of them? – Bandit May 04 '22 at 15:35
  • yes, both of them is required to suppress both output streams – phuclv May 04 '22 at 15:57
  • 1
    @Bandit stream is a very important concept in all shells. If you don't know about it please read [Standard streams](https://en.wikipedia.org/wiki/Standard_streams), [redirection](https://en.wikipedia.org/wiki/Redirection_(computing)), [about_Redirection](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_redirection?view=powershell-7.2), [about Output Streams](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_output_streams?view=powershell-7.2) – phuclv May 05 '22 at 03:05