PowerShell Pscx Expand-Archive方法故障检测 [英] PowerShell Pscx Expand-Archive method failure detection

查看:415
本文介绍了PowerShell Pscx Expand-Archive方法故障检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PowerShell片段:

PowerShell snippet:

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

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

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)
...

问题:如何在Powershell中检测到Expand-Archive失败

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

谢谢

推荐答案

好像我找到了可行的解决方案:

Looks like I've found solution that works:

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

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

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.

这篇关于PowerShell Pscx Expand-Archive方法故障检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆