0

Is there any way to make the function Invoke-Sqlcmd2 not terminate the rest of the script on failure?

I want the rest of my script to continue to run even if this command fails.

I've dug around the code and my thought is it has something to do with the -ErrorAction parameter.

Below is line 488 of the script:

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

Any ideas on how to get this command to 'non-terminate'?

Solution: Thanks for the info. I ended up wrapping the error handling section of invoke-sqlcmd2 in a try-catch trap.

Mike
  • 43
  • 1
  • 3
  • 7
  • The Catch {} is to handle exceptions thrown by the code. If you comment the switch Statement out, or the whole code, it should work. It stops on Stop, Continue AND the Default Action. – Martin Oct 30 '16 at 11:12
  • 1
    Or better, eliminate the error stated in $Err. What does it say? – Martin Oct 30 '16 at 11:14
  • Thanks Martin. This info helped with the above solution. – Mike Oct 30 '16 at 23:05

1 Answers1

0

Putting the Invoke-SQLCMD2 inside a try catch block and maybe log the error thrown. That way you will handle the error and the script will go on.

user2782999
  • 385
  • 1
  • 7
  • 23