Mike Mike - 1 month ago 18
SQL Question

Invoke-Sqlcmd2 do not terminate on error

Is there any way to make the function

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.

Answer

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.