title: Catching permission errors with invoke-sqlcmd and powershell
body: <p>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.</p>
  <pre><code>$in_sql_deployment_instance = 'SQL_SERVER'
  $target_dba_database = 'TEST'
  $query = &quot;RAISERROR (15600,-1,-1, 'mysp_CreateCustomer') WITH log;  &quot;
  
  try{
      $null =  invoke-sqlcmd -ServerInstance &quot;$in_sql_deployment_instance&quot; -Query $query -Database $target_dba_database -Querytimeout 60 -OutputSqlErrors $true -ConnectionTimeout 10 -ErrorAction Continue 
  
  }
  catch
  {
  
      $error_message = $_.Exception.Message
      write-output &quot;Error occured $error_message&quot;
  
  }
  </code></pre>
  <p>I get the error back on the Ps console, but it appears I cannot catch the error etc or store in a variable.</p>
  <pre><code>invoke-sqlcmd : Only System Administrator can specify WITH LOG option for RAISERROR command.
  At line:2 char:14
  + ...    $null =  invoke-sqlcmd -ServerInstance &quot;$in_sql_deployment_instanc ...
  +                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : InvalidOperation: (:) [Invoke-Sqlcmd], SqlPowerShellSqlExecutionException
      + FullyQualifiedErrorId : SqlError,Microsoft.SqlServer.Management.PowerShell.GetScriptCommand
  </code></pre>