powershell - 如何让脚本在 powershell 中第一次出现错误时退出?

标签 powershell

我有下面的函数,我传入两个数组。我希望脚本在第一次出现错误时退出。 我尝试了不同的变体,但我无法让脚本提前停止。

我尝试使用($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/

相关文章:

带有变量和字符串的 PowerShell Get-ChildItem

shell - 在Windows Powershell中格式化大文本文件

powershell - Powershell用序列号替换字符串中的第26个字符

xml - 如何使用 Powershell 访问 CDATA 内的 XML 数据

.net - 使用 -notcontains 过滤文件

Powershell:捕获组合输出,仅错误输出,将组合输出发送到控制台

powershell - 如何使用CR LF换行符在Powershell中的一次调用中写入控制台和日志文件

.net - 将整个文件夹上传到FTP的PowerShell脚本

powershell - 从Powershell POST时如何解析 “Content”输出?

c# - PowerShell 变量和 C#