internal/Remove-SqlDatabase.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
function Remove-SqlDatabase {
    <#
    .SYNOPSIS
    Internal function. Uses SMO's KillDatabase to drop all user connections then drop a database. $server is
    an SMO server object.
    THIS FUNCTION IS HERE BECAUSE OF LEGACY REQUIREMENTS. (Copy-DbaDatabase, Test-DbaLastBackup, Remove-DbaDatabaseSafely)
#>

    [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseShouldProcessForStateChangingFunctions", "")]
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true)]
        [Alias("ServerInstance", "SqlServer")]
        [object]$SqlInstance,
        [Parameter(Mandatory = $true)]
        [string]$DBName,
        [PSCredential]$SqlCredential
    )

    $escapedname = "[$dbname]"

    try {
        $server = Connect-SqlInstance -SqlInstance $SqlInstance -SqlCredential $SqlCredential
        $server.KillDatabase($dbname)
        $server.Refresh()
        return "Successfully dropped $dbname on $($server.name)"
    }
    catch {
        try {
            $null = $server.Query("DROP DATABASE $escapedname")
            return "Successfully dropped $dbname on $($server.name)"
        }
        catch {
            try {
                $server.databases[$dbname].Drop()
                $server.Refresh()
                return "Successfully dropped $dbname on $($server.name)"
            }
            catch {
                return $_
            }
        }
    }
}