首页 > 解决方案 > PowerShell,只有 "> $out" 捕获 7Zip 提取失败的完整错误,为什么?

问题描述

我尝试了有关如何捕获 7Zip 错误的所有建议,如下所述:

并玩了try/catch。

第二只包含

找不到驱动器。名为“7-Zip 18.05 (x64)”的驱动器不存在。

Error[0]

如果我写控制台输出

7-Zip 18.05 (x64) : Copyright (c) 1999-2018 Igor Pavlov : 2018-04-30

Scanning the drive for archives:
1 file, 51273 bytes (51 KiB)

Extracting archive: \\...\850\DAY01
--
7z.exe : ERROR: Data Error : DAY01.RAW
At C:\Users\MyUser\Code\7Zip.ps1:6 char:1
+ & $7ZIP_FullPath x $IN_FullPath -o$OUT_Directory -y
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (ERROR: Data Error : DAY01.RAW:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError
 
Path = \\...\850\DAY01
Type = gzip
Headers Size = 20


Sub items Errors: 1

Archives with Errors: 1

Sub items Errors: 1

在一个变量中,该变量将只包含

7-Zip 18.05 (x64) : Copyright (c) 1999-2018 Igor Pavlov : 2018-04-30

Scanning the drive for archives:
1 file, 51273 bytes (51 KiB)

Extracting archive: \\...\850\DAY01
--    
Path = \\...\850\DAY01
Type = gzip
Headers Size = 20


Sub items Errors: 1

Archives with Errors: 1

Sub items Errors: 1

看起来,那个

7z.exe : ERROR: Data Error : DAY01.RAW
    At C:\Users\MyUser\Code\7Zip.ps1:6 char:1
    + & $7ZIP_FullPath x $IN_FullPath -o$OUT_Directory -y
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (ERROR: Data Error : DAY01.RAW:String) [], RemoteException
        + FullyQualifiedErrorId : NativeCommandError

由 PowerShell 创建(解释什么7z.exe : ERROR:..),只有在我使用时才能捕获$out

& $7ZIP_FullPath x $IN_FullPath -o$OUT_Directory -y > $out

看起来 $out 触发了一些东西,但是什么?

标签: powershellerror-handling7zip

解决方案


@daniel 评论让我走上了正确的道路,但我仍然无法完全解释为什么只有这个解决方案有效。

$7ZIPExtractResult= & $7ZIP_FullPath x $IN_FullPath -o$OUT_Directory -y *>&1

将指定的流重定向到 Success 流。

我的猜测是,只有这样它才能存储在变量中。我在 PowerShell 中 2>&1 的含义 - 堆栈内存溢出about_Redirection - PowerShell |中找到了帮助 微软文档

另外,变量$7ZIPExtractResult是 a System.Object[],所以我必须将数组转换为字符串7ZIPExtractResultAsString = "$7ZIPExtractResult"期望这总是可能的并且不会引发不同的错误。

我仍然不知道为什么这个错误没有被

try {
}
catch {
}

推荐阅读