-
Notifications
You must be signed in to change notification settings - Fork 1
/
KillConnections.ps1
56 lines (49 loc) · 1.29 KB
/
KillConnections.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<#!
.SYNOPSIS
Function to kill active sessions to Nav database
.DESCRIPTION
Function to kill SQL connections. Inspired by script from Bryan Christian
.NOTES
File Name: .PS1
Author: Mathew Ealy
Requires Powershell 5.0
.PARAMETERS
-ServerInfo
The Name of the SQL server
Required? True
Position? 0
Default value
Accept pipeline input? false
Accept wildcard characters? false
-Database
The database name
Required? True
Position? 1
Default value
Accept pipeline input? false
Accept wildcard characters? True
.LINK
https://github.com/MEaly58
#>
#Requires -RunAsAdministrator
function Kill-Connections
{
Param
(
[Parameter(Mandatory=$true, Position=0)]
[String] $ServerInfo,
[Parameter(Mandatory=$true, Position=1)]
[string] $Database
)
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo")
try
{
$sqlServer = new-object ("Microsoft.SqlServer.Management.Smo.Server") $ServerInfo
$sqlServer.KillAllProcesses($Database)
}
catch
{
Write-Error "Failed to kill connections to $database on $sqlserver. Either database not found or cannot connect to server."
}
Write-Output "Active Connections to $database on $sqlserver terminated"
}