functions/Remove-DbaCmConnection.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
function Remove-DbaCmConnection
{
    <#
        .SYNOPSIS
            Removes connection objects from the connection cache used for remote computer management.
         
        .DESCRIPTION
            Removes connection objects from the connection cache used for remote computer management.
         
        .PARAMETER ComputerName
            The computer whose connection to remove.
            Accepts both text as well as the output of Get-DbaCmConnection.
         
        .PARAMETER Silent
            Replaces user friendly yellow warnings with bloody red exceptions of doom!
            Use this if you want the function to throw terminating errors you want to catch.
         
        .EXAMPLE
            Remove-DbaCmConnection -ComputerName sql2014
     
            Removes the cached connection to the server sql2014 from the cache.
     
        .EXAMPLE
            Get-DbaCmConnection | Remove-DbaCmConnection
     
            Clears the entire connection cache.
    #>

    [CmdletBinding()]
    Param (
        [Parameter(ValueFromPipeline = $true, Mandatory = $true)]
        [Sqlcollective.Dbatools.Parameter.DbaCmConnectionParameter[]]
        $ComputerName,
        
        [switch]
        $Silent
    )
    
    Begin
    {
        Write-Message -Level InternalComment -Message "Starting"
        Write-Message -Level Verbose -Message "Bound parameters: $($PSBoundParameters.Keys -join ", ")"
    }
    Process
    {
        foreach ($connectionObject in $ComputerName)
        {
            if (-not $connectionObject.Success) { Stop-Function -Message "Failed to interpret computername input: $($connectionObject.InputObject)" -Category InvalidArgument -Target $connectionObject.InputObject -Continue }
            Write-Message -Level VeryVerbose -Message "Removing from connection cache: $($connectionObject.Connection.ComputerName)" -Target $connectionObject.Connection.ComputerName
            if ([sqlcollective.dbatools.Connection.ConnectionHost]::Connections.ContainsKey($connectionObject.Connection.ComputerName))
            {
                $null = [sqlcollective.dbatools.Connection.ConnectionHost]::Connections.Remove($connectionObject.Connection.ComputerName)
                Write-Message -Level Verbose -Message "Successfully removed $($connectionObject.Connection.ComputerName)" -Target $connectionObject.Connection.ComputerName
            }
            else
            {
                Write-Message -Level Verbose -Message "Not found: $($connectionObject.Connection.ComputerName)" -Target $connectionObject.Connection.ComputerName
            }
        }
    }
    End
    {
        Write-Message -Level InternalComment -Message "Ending"
    }
}