forked from dataplat/dbatools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDisconnect-DbaInstance.ps1
111 lines (94 loc) · 4.74 KB
/
Disconnect-DbaInstance.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
function Disconnect-DbaInstance {
<#
.SYNOPSIS
Disconnects or closes a connection to a SQL Server instance
.DESCRIPTION
Disconnects or closes a connection to a SQL Server instance
To clear all of your connection pools, use Clear-DbaConnectionPool
.PARAMETER InputObject
The server object to disconnect from, usually piped in from Get-DbaConnectedInstance
.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 EnableException
By default, when something goes wrong we try to catch it, interpret it and give you a friendly warning message.
This avoids overwhelming you with "sea of red" exceptions, but is inconvenient because it basically disables advanced scripting.
Using this switch turns this "nice by default" feature off and enables you to catch exceptions with your own try/catch.
.NOTES
Tags: Connection
Author: Chrissy LeMaire (@cl), netnerds.net
Website: https://dbatools.io
Copyright: (c) 2021 by dbatools, licensed under MIT
License: MIT https://opensource.org/licenses/MIT
.LINK
https://dbatools.io/Disconnect-DbaInstance
.EXAMPLE
PS C:\> Get-DbaConnectedInstance | Disconnect-DbaInstance
Disconnects all connected instances
.EXAMPLE
PS C:\> Get-DbaConnectedInstance | Out-GridView -Passthru | Disconnect-DbaInstance
Disconnects selected SQL Server instances
.EXAMPLE
PS C:\> $server = Connect-DbaInstance -SqlInstance sql01
PS C:\> $server | Disconnect-DbaInstance
Disconnects the $server connection
#>
[CmdletBinding(SupportsShouldProcess, ConfirmImpact = "Low")]
param (
[parameter(ValueFromPipeline)]
[psobject[]]$InputObject,
[switch]$EnableException
)
process {
# to avoid enumeration problems when piped
$objects += $InputObject
}
end {
foreach ($object in $objects) {
if ($object.ConnectionObject) {
$servers = $object.ConnectionObject
} else {
$servers = $object
}
foreach ($server in $servers) {
try {
if ($server.ConnectionContext) {
if ($Pscmdlet.ShouldProcess($server.Name, "Disconnecting SQL Connection")) {
$null = $server.ConnectionContext.Disconnect()
if ($script:connectionhash[$server.ConnectionContext.ConnectionString]) {
Write-Message -Level Verbose -Message "removing from connection hash"
$null = $script:connectionhash.Remove($server.ConnectionContext.ConnectionString)
}
[PSCustomObject]@{
SqlInstance = $server.Name
ConnectionString = (Hide-ConnectionString -ConnectionString $server.ConnectionContext.ConnectionString)
ConnectionType = $server.GetType().FullName
State = "Disconnected"
} | Select-DefaultView -Property SqlInstance, ConnectionType, State
}
}
if ($server.GetType().Name -eq "SqlConnection") {
if ($Pscmdlet.ShouldProcess($server.DataSource, "Closing SQL Connection")) {
if ($server.State -eq "Open") {
$null = $server.Close()
}
if ($script:connectionhash[$server.ConnectionString]) {
Write-Message -Level Verbose -Message "removing from connection hash"
$null = $script:connectionhash.Remove($server.ConnectionString)
}
[PSCustomObject]@{
SqlInstance = $server.DataSource
ConnectionString = (Hide-ConnectionString -ConnectionString $server.ConnectionString)
ConnectionType = $server.GetType().FullName
State = $server.State
} | Select-DefaultView -Property SqlInstance, ConnectionType, State
}
}
} catch {
Stop-Function -Message "Failed to disconnect $object" -ErrorRecord $PSItem -Continue
}
}
}
}
}