在 PowerShell 中使用 7-Zip 测试存档的完整性

Test integrity of archive with 7-Zip in PowerShell

我正在尝试测试 ZIP 文件的完整性

看起来这个命令正在运行,但我得到了关于该文件的许多详细信息。 我想得到测试通过或失败的结果,以便进行下一步。

知道怎么做吗? 类似于:

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

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

输出为:

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

Everything is Ok

Files: 902
Size:       5927324719
Compressed: 5738248794

试过这个:

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

if ($ok) {write-host "Integrity test for Zip file passed"}

通过 exit code so just check it. No need to parse the output, you can just redirect the output to null 报告错误。在 PowerShell 中检查退出状态的方法是 $?$LASTEXITCODE

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

更新:

在较旧的 PowerShell(7.1.x 及更早版本)中重定向标准错误时使用 if ($LASTEXITCODE -eq 0) 而不是 if ($?) 以获得更可靠的结果


$?

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