問題描述
如何在錯誤時繼續使用 &$var 調用可執行文件 (How to continue on error a call to an executable using &$var)
假設我想在使用 &
調用可執行文件時繼續出錯。示例:
&$executable
有什麼辦法可以得到這個嗎?
參考解法
方法 1:
Consider using a function for that to reset the old ErrorActionPreference
:
function Invoke‑MyFunctorSilently
{
param
(
[scriptblock]$func
)
$currentEa = $ErrorActionPreference
try
{
$ErrorActionPreference = 'SilentlyContinue'
$func.Invoke()
}
finally
{
$ErrorActionPreference = $currentEa
}
}
So you can invoke it like this:
Invoke‑MyFunctorSilently ‑func { & calc.exe}
方法 2:
Set the $ErrorActionPreference
Global Variable to one of this options:
SilentlyContinue, Stop, Continue, Inquire, Ignore
For example:
$ErrorActionPreference = 'SilentlyContinue'
&$executable
(by Matías Fidemraizer、Martin Brandl、Avshalom)