functions/Get-DbaLogin.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
function Get-DbaLogin {
    <#
        .SYNOPSIS
            Function to get an SMO login object of the logins for a given SQL Instance. Takes a server object from the pipe
 
        .DESCRIPTION
            The Get-DbaLogin function returns an SMO Login object for the logins passed, if there are no users passed it will return all logins.
 
        .PARAMETER SqlInstance
            The SQL Server instance, or instances.You must have sysadmin access and server version must be SQL Server version 2000 or higher.
 
        .PARAMETER SqlCredential
            Allows you to login to servers using SQL Logins as opposed to Windows Auth/Integrated/Trusted.
 
        .PARAMETER Login
            The login(s) to process - this list is auto-populated from the server. If unspecified, all logins will be processed.
 
        .PARAMETER ExcludeLogin
            The login(s) to exclude - this list is auto-populated from the server
 
        .PARAMETER IncludeFilter
            A list of logins to include - accepts wildcard patterns
 
        .PARAMETER ExcludeFilter
            A list of logins to exclude - accepts wildcard patterns
 
        .PARAMETER NoSystem
            A Switch to remove System Logins from the output.
 
        .PARAMETER SQLLogins
            A Switch to return Logins of type SQLLogin only.
 
        .PARAMETER WindowsLogins
            A Switch to return Logins of type Windows only.
 
        .PARAMETER Locked
            A Switch to return locked Logins.
 
        .PARAMETER Disabled
            A Switch to return disabled Logins.
 
        .PARAMETER HasAccess
            A Switch to return Logins that have access to the instance of SQL Server.
 
        .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
            Author: Mitchell Hamann (@SirCaptainMitch)
            Author: Klaas Vandenberghe (@powerdbaklaas)
            Author: Robert Corrigan (@rjcorrig)
            Author: Rob Sewell (@SQLDBaWithBeard)
 
            Website: https://dbatools.io
            Copyright: (C) Chrissy LeMaire, clemaire@gmail.com
            License: GNU GPL v3 https://opensource.org/licenses/GPL-3.0
 
        .LINK
            https://dbatools.io/Get-DbaLogin
 
        .EXAMPLE
            Get-DbaLogin -SqlInstance sql2016
 
            Gets all the logins from server sql2016 using NT authentication and returns the SMO login objects
 
        .EXAMPLE
            Get-DbaLogin -SqlInstance sql2016 -SqlCredential $sqlcred
 
            Gets all the logins for a given SQL Server using a passed credential object and returns the SMO login objects
 
        .EXAMPLE
            Get-DbaLogin -SqlInstance sql2016 -SqlCredential $sqlcred -Login dbatoolsuser,TheCaptain
 
            Get specific logins from server sql2016 returned as SMO login objects.
 
        .EXAMPLE
            Get-DbaLogin -SqlInstance sql2016 -IncludeFilter '##*','NT *'
 
            Get all user objects from server sql2016 beginning with '##' or 'NT ', returned as SMO login objects.
 
        .EXAMPLE
            Get-DbaLogin -SqlInstance sql2016 -ExcludeLogin dbatoolsuser
 
            Get all user objects from server sql2016 except the login dbatoolsuser, returned as SMO login objects.
 
        .EXAMPLE
            Get-DbaLogin -SqlInstance sql2016 -WindowsLogins
 
            Get all user objects from server sql2016 that are Windows Logins
 
        .EXAMPLE
            Get-DbaLogin -SqlInstance sql2016 -WindowsLogins -IncludeFilter *Rob*
 
            Get all user objects from server sql2016 that are Windows Logins and have Rob in the name
 
        .EXAMPLE
            Get-DbaLogin -SqlInstance sql2016 -SQLLogins
 
            Get all user objects from server sql2016 that are SQLLogins
 
        .EXAMPLE
            Get-DbaLogin -SqlInstance sql2016 -SQLLogins -IncludeFilter *Rob*
 
            Get all user objects from server sql2016 that are SQLLogins and have Rob in the name
 
        .EXAMPLE
            Get-DbaLogin -SqlInstance sql2016 -NoSystem
 
            Get all user objects from server sql2016 that are not system objects
 
        .EXAMPLE
            Get-DbaLogin -SqlInstance sql2016 -ExcludeFilter '##*','NT *'
 
            Get all user objects from server sql2016 except any beginning with '##' or 'NT ', returned as SMO login objects.
 
        .EXAMPLE
            'sql2016', 'sql2014' | Get-DbaLogin -SqlCredential $sqlcred
 
            Using Get-DbaLogin on the pipeline, you can also specify which names you would like with -Login.
 
        .EXAMPLE
            'sql2016', 'sql2014' | Get-DbaLogin -SqlCredential $sqlcred -Locked
 
            Using Get-DbaLogin on the pipeline to get all locked logins on servers sql2016 and sql2014.
 
        .EXAMPLE
            'sql2016', 'sql2014' | Get-DbaLogin -SqlCredential $sqlcred -HasAccess -Disabled
 
            Using Get-DbaLogin on the pipeline to get all Disabled logins that have access on servers sql2016 or sql2014.
    #>

    [CmdletBinding()]
    Param (
        [parameter(Position = 0, Mandatory = $true, ValueFromPipeline = $true)]
        [Alias("ServerInstance", "SqlServer")]
        [DbaInstanceParameter[]]$SqlInstance,
        [PSCredential]
        $SqlCredential,
        [object[]]$Login,
        [object[]]$IncludeFilter,
        [object[]]$ExcludeLogin,
        [object[]]$ExcludeFilter,
        [switch]$NoSystem,
        [switch]$SQLLogins,
        [switch]$WindowsLogins,
        [switch]$HasAccess,
        [switch]$Locked,
        [switch]$Disabled,
        [switch][Alias('Silent')]$EnableException
    )

    process {
        foreach ($Instance in $sqlInstance) {
            try {
                Write-Message -Level Verbose -Message "Connecting to $instance"
                $server = Connect-SqlInstance -SqlInstance $instance -SqlCredential $SqlCredential
            }
            catch {
                Stop-Function -Message "Failure" -Category ConnectionError -ErrorRecord $_ -Target $instance -Continue
            }

            $serverLogins = $server.Logins

            if ($Login) {
                $serverLogins = $serverLogins | Where-Object Name -in $Login
            }

            if ($NoSystem) {
                $serverLogins = $serverLogins | Where-Object IsSystemObject -eq $false
            }

            if ($SQLLogins) {
                $serverLogins = $serverLogins | Where-Object LoginType -eq 'SqlLogin'
            }

            if ($WindowsLogins) {
                $serverLogins = $serverLogins | Where-Object LoginType -eq 'WindowsUser'
            }

            if ($IncludeFilter) {
                $serverLogins = $serverLogins | Where-Object {
                    ForEach ($filter in $IncludeFilter) {
                        if ($_.Name -like $filter) {
                            return $true;
                        }
                    }
                }
            }

            if ($ExcludeLogin) {
                $serverLogins = $serverLogins | Where-Object Name -NotIn $ExcludeLogin
            }

            if ($ExcludeFilter) {
                ForEach ($filter in $ExcludeFilter) {
                    $serverLogins = $serverLogins | Where-Object Name -NotLike $filter
                }
            }

            if ($HasAccess) {
                $serverLogins = $serverLogins | Where-Object HasAccess
            }

            if ($Locked) {
                $serverLogins = $serverLogins | Where-Object IsLocked
            }

            if ($Disabled) {
                $serverLogins = $serverLogins | Where-Object IsDisabled
            }

            foreach ($serverLogin in $serverlogins) {
                Write-Message -Level Verbose -Message "Processing $serverLogin on $instance"

                if ($server.VersionMajor -gt 9) {
                    # There's no reliable method to get last login time with SQL Server 2000, so only show on 2005+
                    Write-Message -Level Verbose -Message "Getting last login time"
                    $sql = "SELECT MAX(login_time) AS [login_time] FROM sys.dm_exec_sessions WHERE login_name = '$($serverLogin.name)'"
                    Add-Member -Force -InputObject $serverLogin -MemberType NoteProperty -Name LastLogin -Value $server.ConnectionContext.ExecuteScalar($sql)
                }
                else {
                    Add-Member -Force -InputObject $serverLogin -MemberType NoteProperty -Name LastLogin -Value $null
                }

                Add-Member -Force -InputObject $serverLogin -MemberType NoteProperty -Name ComputerName -Value $server.NetName
                Add-Member -Force -InputObject $serverLogin -MemberType NoteProperty -Name InstanceName -Value $server.ServiceName
                Add-Member -Force -InputObject $serverLogin -MemberType NoteProperty -Name SqlInstance -Value $server.DomainInstanceName

                Select-DefaultView -InputObject $serverLogin -Property ComputerName, InstanceName, SqlInstance, Name, LoginType, CreateDate, LastLogin, HasAccess, IsLocked, IsDisabled
            } #foreach serverlogin
        } #foreach instance
    } #process
} #function