繁体   English   中英

使用命令调用Powershell时如何充分抑制错误?

[英]How to fully suppress an error when invoking powershell with a command?

我正在尝试编写msbuild postbuild事件,该事件将复制文件。 它不必是100%-目标文件可以使用或锁定,在这种情况下,我希望构建成功。

所以我有类似*的东西

PowerShell -NoProfile -ExecutionPolicy Bypass -Command "cp foo `$(TargetDir)"

$(TargetDir)\\foo被另一个进程使用时,出现错误,构建失败。 所以我尝试

PowerShell -NoProfile -ExecutionPolicy Bypass -Command "cp foo `$(TargetDir) -ErrorAction SilentlyContinue"

但错误已打印且失败。 所以我尝试

PowerShell -NoProfile -ExecutionPolicy Bypass -Command "try { cp foo `$(TargetDir) } catch {}"

现在错误没有被打印出来,但是它仍然失败,因为如果我执行上面的$LastExitCode仍然等于1。

我可以在try catch和powershell中再次包装,但是如何正确抑制错误?


*实际的命令如下-没关系

PowerShell -NoProfile -ExecutionPolicy Bypass -Command `"try { ls '`$(SolutionDir)\packages\GhostScriptSharp.*\Tools\gsdll32.dll' | Sort -Descending | Select -First 1 | cp -Destination '`$(TargetDir)' } catch {}`""

您可以使用exit关键字来显式设置脚本的退出代码:

PowerShell -NoProfile -ExecutionPolicy Bypass -Command "cp foo `$(TargetDir) -ErrorAction SilentlyContinue; exit 0"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM