【问题标题】:Invoke-Sqlcmd2 do not terminate on errorInvoke-Sqlcmd2 不会因错误而终止
【发布时间】:2016-10-30 05:11:12
【问题描述】:

有没有办法让函数Invoke-Sqlcmd2 在失败时不终止脚本的其余部分?

即使此命令失败,我也希望脚本的其余部分继续运行。

我已经挖掘了代码,我的想法是它与-ErrorAction 参数有关。

下面是脚本的第 488 行:

Catch # For other exception
{
    Write-Verbose "Capture Other Error"  

    $Err = $_

    if ($PSBoundParameters.Verbose) {Write-Verbose "Other Error:  $Err"} 

    switch ($ErrorActionPreference.tostring())
    {
        {'SilentlyContinue','Ignore' -contains $_} {}
        'Stop' {     Throw $Err} # Removing this line doesn't work
        'Continue' { Throw $Err}
        Default {    Throw $Err}
    }
}

关于如何让这个命令“不终止”的任何想法?

解决方案: 谢谢(你的)信息。我最终将 invoke-sqlcmd2 的错误处理部分包装在一个 try-catch 陷阱中。

【问题讨论】:

  • Catch {} 是处理代码抛出的异常。如果您将 switch 语句或整个代码注释掉,它应该可以工作。它在停止、继续和默认操作时停止。
  • 或者更好的是,消除$Err中所述的错误。它说什么?
  • 谢谢马丁。此信息有助于上述解决方案。

标签: sql powershell invoke-sqlcmd


【解决方案1】:

将 Invoke-SQLCMD2 放在 try catch 块中,并可能记录抛出的错误。 这样你就可以处理错误并且脚本会继续运行。

【讨论】:

    猜你喜欢
    • 2017-01-13
    • 2016-01-30
    • 1970-01-01
    • 1970-01-01
    • 2020-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多