简体   繁体   English

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

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

I'm trying to write an msbuild postbuild event that will copy a file. 我正在尝试编写msbuild postbuild事件,该事件将复制文件。 It need not be 100% - the destination file might be used or locked in which case I would like the build to succeed. 它不必是100%-目标文件可以使用或锁定,在这种情况下,我希望构建成功。

so I have something like* 所以我有类似*的东西

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

when $(TargetDir)\\foo is used by another process I get an error and my build fails. $(TargetDir)\\foo被另一个进程使用时,出现错误,构建失败。 So I try 所以我尝试

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

yet the error is printed and it fails. 但错误已打印且失败。 So I try 所以我尝试

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

and now the error is not printed yet it STILL fails because if I execute the above $LastExitCode still equals 1. 现在错误没有被打印出来,但是它仍然失败,因为如果我执行上面的$LastExitCode仍然等于1。

I can wrap yet again in try catch and powershell but how do I suppress the error correctly? 我可以在try catch和powershell中再次包装,但是如何正确抑制错误?


*The actual command is the following - not that it matters *实际的命令如下-没关系

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