我有下面的函数,我传入两个数组。我希望脚本在第一次出现错误时退出。 我尝试了不同的变体,但我无法让脚本提前停止。
我尝试使用($LastExitCode -eq 0)
它似乎不起作用,所有脚本仍在继续运行。
我还尝试了If ($? -ne "True")
它也不起作用,所有测试根本不运行。
function Run-Coverage {
param($openCoverPath,$testExecutorPath,$dllPath,$output)
& $openCoverPath `
-threshold:100 `
-register:user `
-target:$testExecutorPath `
-targetargs:"$dllPath" `
-output:$output `
}
function Run-Tests
{
param($Dll,$Xmls)
for ($i=0; $i -lt $Dlls.length; $i++)
{
$TestParam = $Dlls[$i]
$resultParam = $Xmls[$i]
$dllPath = -join("`"",$projectBasePath,$TestParam," ","/TestCaseFilter:`"(TestCategory!=RequiresData)`"","`"")
$output = -join($outputPath,$resultParam)
try
{
Run-Coverage -openCoverPath $openCoverPath -testExecutorPath $testExecutorPath -dllPath $dllPath -output $output
}
catch
{
Write-Host "Exiting loop"
break
}
}
}
最佳答案
如果添加 [CmdletBinding()]
作为函数内的第一行(param()
block 上方),您可以使用添加的 Common
来调用该函数。参数如 ErrorAction , ErrorVariable等等
try {
Run-Coverage -openCoverPath $openCoverPath -testExecutorPath $testExecutorPath -dllPath $dllPath -output $output -ErrorAction Stop
}
catch {
throw
}
关于powershell - 如何让脚本在 powershell 中第一次出现错误时退出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63439888/