title: PowerShell Kill all dead connections
body: <p>How do I kill all dead connections?</p>
  
  <p>I have exception caught in $error message:</p>
  
  <blockquote>
    <p>System.Management.Automation.MethodInvocationException: Exception calling "KillAllProcesses" with "1" argument(s): "Drop all active
    database connections failed for Server '172.0.0.1'. " ---></p>
    
    <p>Microsoft.SqlServer.Management.Smo.FailedOperationException: Drop all
    active database connections failed for Server '172.0.0.1'.  --->
    Microsoft.SqlServer.Management.Common.ExecutionFailureException: 
                                An exception occurred while executing a Transact-SQL statement or batch. --->
    System.Data.SqlClient.SqlException: Process ID 68 is not an active
    process ID.</p>
  </blockquote>
  
  <p>now my ps script:</p>
  
  <pre><code> # ==== check if db exists and drop db ==== #
  
          if($srv.Databases.Contains("$DbName"))
          { 
                  Write-Host -fore Yellow "Dropping existing db: $DbName on Server: $Server"
  
                  $srv.KillAllProcesses($DbName)
                  $srv.KillDatabase($DbName)
  
                  # show us errors
                  $error = $_.Exception
                  while ( $error.InnerException )
                  {
                      $error = $error.InnerException
                      Write-Host -fore Red $error.Message                     
                  }
  
                  Write-Host -fore Yellow "Completed deleting db: $DbName on Server: $Server"
          } 
          else { 
              Write-Host "Database $DbName does not exists"
          }     
  
  
      # ==== check if db exists and drop db ==== #
  </code></pre>
  
  <p>I thought that KillAllProcesses kills everything but according to error message it does not.</p>
  
  <p>Any ideas how to solve this?</p>