简体   繁体   中英

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. It need not be 100% - the destination file might be used or locked in which case I would like the build to succeed.

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

I can wrap yet again in try catch and powershell but how do I suppress the error correctly?


*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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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