I am running the following PowerShell script, and what strikes me is that its not catching permission specific errors. I know that the error action has been set to continue, which is what I want, however I would like to track such errors but for them not to terminate the loop that I am running.
$in_sql_deployment_instance = 'SQL_SERVER'
$target_dba_database = 'TEST'
$query = "RAISERROR (15600,-1,-1, 'mysp_CreateCustomer') WITH log; "
try{
$null = invoke-sqlcmd -ServerInstance "$in_sql_deployment_instance" -Query $query -Database $target_dba_database -Querytimeout 60 -OutputSqlErrors $true -ConnectionTimeout 10 -ErrorAction Continue
}
catch
{
$error_message = $_.Exception.Message
write-output "Error occured $error_message"
}
I get the error back on the Ps console, but it appears I cannot catch the error etc or store in a variable.
invoke-sqlcmd : Only System Administrator can specify WITH LOG option for RAISERROR command.
At line:2 char:14
+ ... $null = invoke-sqlcmd -ServerInstance "$in_sql_deployment_instanc ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Invoke-Sqlcmd], SqlPowerShellSqlExecutionException
+ FullyQualifiedErrorId : SqlError,Microsoft.SqlServer.Management.PowerShell.GetScriptCommand
As far as I know, in order for the catch to be executed, you would need to set your -ErrorAction
to Stop
.
I suppose what you could do is: set: $ErrorActionPreference = "Continue"
at the top of your cmdlet and then have -ErrorAction stop
on the call to invoke-sql
, this should cause it to drop into the catch
but continue to behave with the continue
behaviour in the rest of your script.
Another option if you want to leave -ErrorAction continue
would be to also set -ErrorVariable invokeSqlError
and then check if that variable contains a value. If it does an error has occurred.
Something like:
if ($invokeSqlError){
write-output "Error occured $invokeSqlError"
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.