functions/Stop-DbaProcess.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
Function Stop-DbaProcess
{
<#
.SYNOPSIS
This command finds and kills SQL Server processes.
 
.DESCRIPTION
This command kills all spids associated with a spid, login, host, program or database.
  
If you are attempting to kill your own login sessions, the process performing the kills will be skipped.
 
.PARAMETER SqlServer
The SQL Server instance.
 
.PARAMETER SqlCredential
Allows you to login to servers using SQL Logins as opposed to Windows Auth/Integrated/Trusted.
 
.PARAMETER Spids
This parameter is auto-populated from -SqlServer. You can specify one or more Spids to be killed.
 
.PARAMETER Logins
This parameter is auto-populated from-SqlServer and allows only login names that have active processes. You can specify one or more logins whose processes will be killed.
 
.PARAMETER Hosts
This parameter is auto-populated from -SqlServer and allows only host names that have active processes. You can specify one or more Hosts whose processes will be killed.
 
.PARAMETER Programs
This parameter is auto-populated from -SqlServer and allows only program names that have active processes. You can specify one or more Programs whose processes will be killed.
 
.PARAMETER Databases
This parameter is auto-populated from -SqlServer and allows only database names that have active processes. You can specify one or more Databases whose processes will be killed.
 
.PARAMETER Exclude
This parameter is auto-populated from -SqlServer. You can specify one or more Spids to exclude from being killed (goes well with Logins).
 
Exclude is the last filter to run, so even if a Spid matches, for example, Hosts, if it's listed in Exclude it wil be excluded.
 
.PARAMETER WhatIf
Shows what would happen if the command were to run. No actions are actually performed.
 
.PARAMETER Confirm
Prompts you for confirmation before executing any changing operations within the command.
  
.PARAMETER Process
This is the process object passed by Get-DbaProcess if using a pipeline
  
.NOTES
dbatools PowerShell module (https://dbatools.io, clemaire@gmail.com)
Copyright (C) 2016 Chrissy LeMaire
 
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
 
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 
You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
 
.LINK
https://dbatools.io/Stop-DbaProcess
 
.EXAMPLE
Stop-DbaProcess -SqlServer sqlserver2014a -Logins base\ctrlb, sa
 
Finds all processes for base\ctrlb and sa on sqlserver2014a, then kills them. Uses Windows Authentication to login to sqlserver2014a.
 
.EXAMPLE
Stop-DbaProcess -SqlServer sqlserver2014a -SqlCredential $credential -Spids 56, 77
  
Finds processes for spid 56 and 57, then kills them. Uses alternative (SQL or Windows) credentials to login to sqlserver2014a.
 
.EXAMPLE
Stop-DbaProcess -SqlServer sqlserver2014a -Programs 'Microsoft SQL Server Management Studio'
  
Finds processes that were created in Microsoft SQL Server Management Studio, then kills them.
 
.EXAMPLE
Stop-DbaProcess -SqlServer sqlserver2014a -Hosts workstationx, server100
  
Finds processes that were initiated by hosts (computers/clients) workstationx and server 1000, then kills them.
 
.EXAMPLE
Stop-DbaProcess -SqlServer sqlserver2014 -Databases tempdb -WhatIf
  
Shows what would happen if the command were executed.
  
.EXAMPLE
Get-DbaProcess -SqlServer sql2016 -Programs 'dbatools PowerShell module - dbatools.io' | Stop-DbaProcess
  
Finds processes that were created with dbatools, then kills them.
 
#>

[CmdletBinding(DefaultParameterSetName = "Default", SupportsShouldProcess = $true)]
Param (
[parameter(Mandatory = $true, ParameterSetName = "Server")]
[Alias("ServerInstance", "SqlInstance")]
[object]$SqlServer,
[object]$SqlCredential,
[parameter(ValueFromPipeline = $true, Mandatory = $true, ParameterSetName = "Process")]
[object[]]$Process
)

DynamicParam { if ($sqlserver) { Get-ParamSqlAllProcessInfo -SqlServer $sqlserver -SqlCredential $SqlCredential } }

BEGIN
{
$logins = $psboundparameters.Logins
$spids = $psboundparameters.Spids
$exclude = $psboundparameters.Exclude
$hosts = $psboundparameters.Hosts
$programs = $psboundparameters.Programs
$databases = $psboundparameters.Databases
}

PROCESS
{
if ($Process)
{
foreach ($session in $Process)
{
$sourceserver = $session.SqlServer

if (!$sourceserver)
{
Write-Warning "Only process objects can be passed through the pipeline"
break
}

$spid = $session.spid

if ($sourceserver.ConnectionContext.ProcessID -eq $spid)
{
Write-Warning "Skipping spid $spid because you cannot use KILL to kill your own process"
Continue
}

If ($Pscmdlet.ShouldProcess($sourceserver, "Killing spid $spid"))
{
try
{
$sourceserver.KillProcess($spid)
[pscustomobject]@{
SqlInstance = $sourceserver.name
Spid = $session.Spid
Login = $session.Login
Host = $session.Host
Database = $session.Database
Program = $session.Program
Status = 'Killed'
}
}
catch
{
Write-Warning "Couldn't kill spid $spid"
Write-Exception $_
}
}
}
return
}

$sourceserver = Connect-SqlServer -SqlServer $sqlserver -SqlCredential $SqlCredential

if ($logins.count -eq 0 -and $spids.count -eq 0 -and $hosts.count -eq 0 -and $programs.count -eq 0 -and $databases.count -eq 0)
{
Write-Warning "At least one login, spid, host, program or database must be specified."
continue
}

$allsessions = @()

$processes = $sourceserver.EnumProcesses() | Where-Object { $_.spid -gt 50 }

if ($logins.count -gt 0)
{
$allsessions += $processes | Where-Object { $_.Login -in $Logins }
}

if ($spids.count -gt 0)
{
$allsessions += $processes | Where-Object { $_.Spid -in $spids }
}

if ($hosts.count -gt 0)
{
$allsessions += $processes | Where-Object { $_.Host -in $hosts }
}

if ($programs.count -gt 0)
{
$allsessions += $processes | Where-Object { $_.Program -in $programs }
}

if ($databases.count -gt 0)
{
$allsessions += $processes | Where-Object { $_.Database -in $databases }
}

if ($exclude.count -gt 0)
{
$allsessions = $allsessions | Where-Object { $exclude -notcontains $_.Spid }
}

if ($allsessions.urn.count -eq 0)
{
Write-Warning "No sessions found"
}

$duplicates = @()

foreach ($session in $allsessions)
{
if ($session.spid -in $duplicates) { continue }
$duplicates += $session.spid

$spid = $session.spid
if ($sourceserver.ConnectionContext.ProcessID -eq $spid)
{
Write-Warning "Skipping spid $spid because you cannot use KILL to kill your own process"
Continue
}

If ($Pscmdlet.ShouldProcess($sqlserver, "Killing spid $spid"))
{
try
{
$sourceserver.KillProcess($spid)
[pscustomobject]@{
SqlInstance = $sourceserver.name
Spid = $session.Spid
Login = $session.Login
Host = $session.Host
Database = $session.Database
Program = $session.Program
Status = 'Killed'
}
}
catch
{
Write-Warning "Couldn't kill spid $spid"
Write-Exception $_
}
}
}
}
}