PowerShell Pscx Expand-Archive 方法失败检测
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来我已经找到了有效的解决方案:
如果发生错误(或称其为警告),它们将收集在 $w 变量中。如果
$w.Count -gt 0
则意味着发生了一些错误/警告。Looks like I've found solution that works:
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.$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 theExpand-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 theExpand-Archive
. Do you still get the error?