【问题标题】:PowerShell Pscx Expand-Archive method failure detectionPowerShell Pscx Expand-Archive 方法故障检测
【发布时间】:2011-02-10 05:04:59
【问题描述】:

PowerShell sn-p:

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 中检测 Expand-Archive 失败

谢谢

【问题讨论】:

  • 嗨 Roman - 请在 pscx.codeplex.com 上记录一个错误,我会在下一个版本 - Oisin 修复这个问题。
  • 嗨,奥辛。完成:pscx.codeplex.com/workitem/30139

标签: powershell error-handling pscx


【解决方案1】:

看起来我找到了可行的解决方案:

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

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

【讨论】:

  • 不错的解决方法 - 请在 pscx.codeplex.com 上提交问题...谢谢!
【解决方案2】:

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

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

【讨论】:

  • $errors.Count 保持不变。使用控制台应用程序 - 这只是一个如何使 Expand-Archive cmd-let 失败的示例。主要想法是以某种方式锁定文件
  • 在这种情况下,Expand-Archive 认为它不会失败,所以使用 $?是不行的。
  • 我的错 - 我真的应该写一个非终止错误而不是警告记录。
猜你喜欢
  • 2012-12-05
  • 1970-01-01
  • 2018-01-23
  • 1970-01-01
  • 2014-11-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多