functions/Get-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
65
66
67
68
69
function Get-DbaCmConnection
{
    <#
        .SYNOPSIS
            Retrieves windows management connections from the cache
         
        .DESCRIPTION
            Retrieves windows management connections from the cache
         
        .PARAMETER ComputerName
            The computername to ComputerName for.
         
        .PARAMETER UserName
            Username on credentials to look for. Will not find connections using the default windows credentials.
         
        .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
            PS C:\> Get-DbaCmConnection
             
            List all cached connections.
         
        .EXAMPLE
            PS C:\> Get-DbaCmConnection sql2014
             
            List the cached connection - if any - to the server sql2014.
         
        .EXAMPLE
            PS C:\> Get-DbaCmConnection -UserName "*charles*"
             
            List all cached connection that use a username containing "charles" as default or override credentials.
    #>

    
    [CmdletBinding()]
    param
    (
        [Parameter(Position = 0, ValueFromPipeline = $true)]
        [Alias('Filter')]
        [String[]]
        $ComputerName = "*",
        
        [String]
        $UserName = "*",
        
        [switch]
        $Silent
    )
    
    Begin
    {
        Write-Message -Level InternalComment -Message "Starting"
        Write-Message -Level Verbose -Message "Bound parameters: $($PSBoundParameters.Keys -join ", ")"
    }
    Process
    {
        foreach ($name in $ComputerName)
        {
            Write-Message -Level VeryVerbose -Message "Processing search. ComputerName: '$name' | Username: '$UserName'"
            ([sqlcollective.dbatools.Connection.ConnectionHost]::Connections.Values | Where-Object { ($_.ComputerName -like $name) -and ($_.Credentials.UserName -like $UserName) })
        }
    }
    End
    {
        Write-Message -Level InternalComment -Message "Ending"
    }
}