1

#PSTip Drop all Active Connections of SQL Database

Note: This tip requires PowerShell 2.0 or above.

In an earlier tip, we looked at how we can retrieve the active connection count for a SQL database. In today’s tip we will look at how we can drop all the active connections before we can perform a detach database operation.

For this purpose, we will use the KillAllProcesses() method of the Server SMO.

Add-Type -AssemblyName "Microsoft.SqlServer.Smo, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91"
$smo = New-Object Microsoft.SqlServer.Management.Smo.Server $env:COMPUTERNAME
$smo.KillAllProcesses('MyDB')

This code snippet will help you drop all active database connections of a given SQL database.

Filed in: Columns, Tips and Tricks Tags: , , ,

One Pingback/Trackback

    05 August 2013 at 6:08am
    […] #PSTip Drop all Active Connections of SQL Database by Ravikanth ...
  • Dell Community
© 2014 PowerShell Magazine. All rights reserved. XHTML / CSS Valid.
Proudly designed by Theme Junkie.
%d bloggers like this: