1

I have an powershell script which is executing sql scripts from a given folder. This is working, but in case of error I don't see the reason of the error. In some cases the error is displayed, when the powershell itself throws the error. But when it's a more simple failure such as a primary key conflict, no error is shown.

I've tried it the following way, but this does not work when using the "-InputFile" parameter. Replacing this by the "-Query" Parameter and the error will be shown.

try 
{
    sqlps Invoke-Sqlcmd -ServerInstance "$dbInstance" -Database "$dbName" -InputFile "$updateScriptsFolder\$updateSql" -username $username -password $password -ErrorAction 'Stop' -Verbose -OutputSqlErrors 1
}
catch
{
    Write-Host "Error while executing $updateScriptsFolder\$updateSql" -foregroundcolor red
    Write-Host $error -foregroundcolor red
}

The only solution for now is to add the error handling to every sql script. But it's very likely that developers forget to add the errorhandling to their script. I want a more general error handling. Can anybody help?

Thanks, Michael

2
  • 1
    Is there a reason you don't read the file into a string variable and use that variable as the value for the -query parameter? Commented Oct 13, 2011 at 7:09
  • Cool... I'll add my comment as an answer to stop this appearing unanswered (although I'm not sure if that's the correct S.O. etiquette). Commented Oct 18, 2011 at 16:24

2 Answers 2

1

As per the comments above, read the file into a string variable and use that variable as the value for the -query parameter.

Sign up to request clarification or add additional context in comments.

Comments

0

I got this same error when using -InputFile instead of -Query, but that wasn't the actual problem. The problem was that I had been improperly attempting to set a value (false) for -TrustServerCertificate. Once I removed that value, -InputFile started working just fine.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.