powershell - 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 触发了一些东西,但是什么?
解决方案
@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 {
}
推荐阅读
- css - 是否可以在反应中使用 css 自定义 FullCalendar?
- javascript - 将 Mousetrap Hotkey 绑定到某些 DOM 元素 - 仅在 div 处于活动状态时触发?
- flutter - 颤振错误:任务':app:compileDebugJavaWithJavac'的执行失败
- jquery - 点击后将 UL/LI 替换为 OPTION/SELECT
- javascript - 来自节点模块导入的意外令牌错误
- bluetooth-lowenergy - 如何开始使用 Nordic nRF52810 蓝牙 5.0 模块的初学者
- c# - 从服务器获取日期和时间
- azure - 创建 Azure SQL Server VM,包括备份
- jenkins - 由非管理员为 Jenkins 作业设置 cron 计划
- java - 默认导航抽屉活动的问题