PowerShell Pscx Expand-Archive 方法失败检测

发布于 2024-10-16 11:38:18 字数 624 浏览 1 评论 0原文

PowerShell 代码片段:

Import-Module Pscx
Expand-Archive ConsoleApplication1.zip ./
Write-Host $?
Write-Host $LastExitCode

$?$LastExitCode 均未报告错误。但出现错误,因为文件 ConsoleApplication1.exe 已锁定(我启动了此应用程序)。我可以通过以下输出看到失败:

WARNING: ArchiveCallBack->GetStream error: System.IO.IOException:
The process cannot access the file 'D:\tmp\ConsoleApplication1.exe'
 because it is being used by another process.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
...

问题:如何在 powershell 中检测展开存档失败

谢谢

PowerShell snippet:

Import-Module Pscx
Expand-Archive ConsoleApplication1.zip ./
Write-Host $?
Write-Host $LastExitCode

Neither $? nor $LastExitCode report about error. But there is error, because file ConsoleApplication1.exe is locked (i started this app). I can see failure by following output:

WARNING: ArchiveCallBack->GetStream error: System.IO.IOException:
The process cannot access the file 'D:\tmp\ConsoleApplication1.exe'
 because it is being used by another process.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
...

Question: how can I detect in powershell that Expand-Archive failed

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

萌面超妹 2024-10-23 11:38:18

看起来我已经找到了有效的解决方案:

$w = $null
Expand-Archive ConsoleApplication1.zip ./ -WarningVariable w

如果发生错误(或称其为警告),它们将收集在 $w 变量中。如果$w.Count -gt 0则意味着发生了一些错误/警告。

Looks like I've found solution that works:

$w = $null
Expand-Archive ConsoleApplication1.zip ./ -WarningVariable w

If error occurs (or call them warnings) they are collected in $w variable. If $w.Count -gt 0 it means some error/warning have occured.

玩世 2024-10-23 11:38:18

$LastExitCode 纯粹用于本机 EXE 退出代码。它不适用于 cmdlet。如果 cmdlet 检测到它出错并写出错误对象,则 $? 应该可以工作。此 cmdlet 似乎未在内部检测到该错误。如果您运行 $error.Clear(),然后执行 Expand-Archive 命令,$error[0] 是否包含错误?

另外,当您尝试执行 cmdlet 时,该 cmdlet 是否可能仍在扩展 exe?我假设您正在等待 cmdlet 完成,然后再尝试执行控制台应用程序。我想也可能存在文件被关闭/处置的错误。如果您在 Expand-Archive 之后尝试 [gc]::collect() 会怎样。您仍然收到错误吗?

$LastExitCode is purely for native EXE exit codes. It doesn't apply to cmdlets. $? should work if the cmdlet detects that it has errored and writes out an error object. It appears this cmdlet isn't internally detecting the error. If you run $error.Clear(), then the Expand-Archive command, does $error[0] contain an error?

Also, is it possible that the cmdlet is still expanding the exe when you try to execute it? I assume you're waiting for the cmdlet to finish before attempting to execute the console app. I guess it's also possible there's a bug where the file is being closed/disposed. What if you try a [gc]::collect() after the Expand-Archive. Do you still get the error?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文