functions/Get-DbaDatabase.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
function Get-DbaDatabase {
    <#
  .SYNOPSIS
   Gets SQL Database information for each database that is present in the target instance(s) of SQL Server.
 
  .DESCRIPTION
   The Get-DbaDatabase command gets SQL database information for each database that is present in the target instance(s) of
   SQL Server. If the name of the database is provided, the command will return only the specific database information.
 
  .PARAMETER SqlInstance
   SQL Server name or SMO object representing the SQL Server to connect to. This can be a collection and receive pipeline input to allow the function
   to be executed against multiple SQL Server instances.
 
  .PARAMETER SqlCredential
   PSCredential object to connect as. If not specified, current Windows login will be used.
 
  .PARAMETER Database
   The database(s) to process. If unspecified, all databases will be processed.
 
  .PARAMETER ExcludeDatabase
   The database(s) to exclude.
 
  .PARAMETER ExcludeAllUserDb
   Returns only databases that are not User Databases.
            This parameter cannot be used together with -ExcludeAllSystemDb.
 
  .PARAMETER ExcludeAllSystemDb
   Returns only databases that are not System Databases.
            This parameter cannot be used together with -ExcludeAllUserDb.
 
  .PARAMETER Status
   Returns SQL Server databases in the status(es) listed.
            Could include Emergency, Online, Offline, Recovering, Restoring, Standby or Suspect.    
 
  .PARAMETER Access
   Returns SQL Server databases that are Read Only or Read/Write.
            To collect both, don't use this parameter.
 
  .PARAMETER Owner
   Returns list of databases owned by the specified logins.
 
  .PARAMETER Encrypted
   Returns list of databases that have TDE enabled from the SQL Server instance(s) executed against.
 
  .PARAMETER RecoveryModel
   Returns list of databases in listed recovery models (Full, Simple or Bulk Logged).
 
  .PARAMETER NoFullBackup
   Returns databases without a full backup recorded by SQL Server. Will indicate those which only have CopyOnly full backups.
 
  .PARAMETER NoFullBackupSince
   DateTime value. Returns list of databases that haven't had a full backup since the passed in DateTime.
 
  .PARAMETER NoLogBackup
   Returns databases without a Log backup recorded by SQL Server. Will indicate those which only have CopyOnly Log backups.
 
  .PARAMETER NoLogBackupSince
   DateTime value. Returns list of databases that haven't had a Log backup since the passed in DateTime.
 
  .PARAMETER IncludeLastUsed
   Returns the Last used read and write Times for the Database using the sys.dm_db_index_usage_stats DMV which will show
   the information since the last restart of SQL
 
  .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 Silent
   Use this switch to disable any kind of verbose messages
 
  .NOTES
   Tags: Database
   Original Author: Garry Bargsley (@gbargsley | http://blog.garrybargsley.com)
            Author: Klaas Vandenberghe ( @PowerDbaKlaas )
            Author: Simone Bizzotto ( @niphlod )
 
   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-DbaDatabase
 
  .EXAMPLE
   Get-DbaDatabase -SqlInstance localhost
 
   Returns all databases on the local default SQL Server instance
 
  .EXAMPLE
   Get-DbaDatabase -SqlInstance localhost -ExcludeAllUserDb
 
   Returns only the system databases on the local default SQL Server instance
 
  .EXAMPLE
   Get-DbaDatabase -SqlInstance localhost -ExcludeAllSystemDb
 
   Returns only the user databases on the local default SQL Server instance
 
  .EXAMPLE
   'localhost','sql2016' | Get-DbaDatabase
 
   Returns databases on multiple instances piped into the function
 
  .EXAMPLE
   Get-DbaDatabase -SqlInstance SQL1\SQLExpress -RecoveryModel full,Simple
 
   Returns only the user databases in Full or Simple recovery model from SQL1\SQLExpress
 
  .EXAMPLE
   Get-DbaDatabase -SqlInstance SQL1\SQLExpress -Status Normal
 
   Returns only the user databases with status 'normal' from sql instance SQL1\SQLExpress
  .EXAMPLE
   Get-DbaDatabase -SqlInstance SQL1\SQLExpress -IncludeLastUsed
 
   Returns the databases from sql instance SQL1\SQLExpress including the last used information
   from the sys.dm_db_index_usage_stats DMV
 
  .EXAMPLE
   Get-DbaDatabase -SqlInstance SQL1\SQLExpress,SQL2 -ExcludeDatabase model,master
 
   Returns all databases except master and model from sql instances SQL1\SQLExpress and SQL2
 
  .EXAMPLE
   Get-DbaDatabase -SqlInstance SQL1\SQLExpress,SQL2 -Encrypted
 
   Returns only encrypted databases from sql instances SQL1\SQLExpress and SQL2
 
  .EXAMPLE
   Get-DbaDatabase -SqlInstance SQL1\SQLExpress,SQL2 -Access ReadOnly
 
   Returns only read only databases from sql instances SQL1\SQLExpress and SQL2
 
  .EXAMPLE
   Get-DbaDatabase -SqlInstance SQL2,SQL3 -Database OneDB,OtherDB
 
   Returns databases 'OneDb' and 'OtherDB' from sql instances SQL2 and SQL3 if the databases exist on those instances
 #>

    [CmdletBinding(DefaultParameterSetName = "Default")]
    [OutputType([Microsoft.SqlServer.Management.Smo.Database[]])]
    Param (
        [parameter(Position = 0, Mandatory = $true, ValueFromPipeline = $True)]
        [Alias("ServerInstance", "SqlServer")]
        [DbaInstanceParameter[]]$SqlInstance,
        [PSCredential]$SqlCredential,
        [Alias("Databases")]
        [object[]]$Database,
        [object[]]$ExcludeDatabase,
        [Alias("SystemDbOnly", "NoUserDb")]
        [switch]$ExcludeAllUserDb,
        [Alias("UserDbOnly", "NoSystemDb")]
        [switch]$ExcludeAllSystemDb,
        [string[]]$Owner,
        [switch]$Encrypted,
        [ValidateSet('EmergencyMode', 'Normal', 'Offline', 'Recovering', 'Restoring', 'Standby', 'Suspect')]
        [string[]]$Status = @('EmergencyMode', 'Normal', 'Offline', 'Recovering', 'Restoring', 'Standby', 'Suspect'),
        [ValidateSet('ReadOnly', 'ReadWrite')]
        [string]$Access,
        [ValidateSet('Full', 'Simple', 'BulkLogged')]
        [string[]]$RecoveryModel = @('Full', 'Simple', 'BulkLogged'),
        [switch]$NoFullBackup,
        [datetime]$NoFullBackupSince,
        [switch]$NoLogBackup,
        [datetime]$NoLogBackupSince,
        [switch]$Silent,
        [switch]$IncludeLastUsed
    )
    
    begin {
        
        if ($ExcludeAllUserDb -and $ExcludeAllSystemDb) {
            Stop-Function -Message "You cannot specify both ExcludeAllUserDb and ExcludeAllSystemDb" -Continue -Silent $Silent
        }
        
    }
    process {
        if (Test-FunctionInterrupt) { return }
        
        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
            }
            
            if (!$IncludeLastUsed) {
                $dblastused = $null
            }
            else {
                ## Get last used information from the DMV
                $querylastused = "WITH agg AS
    (
      SELECT
           max(last_user_seek) last_user_seek,
           max(last_user_scan) last_user_scan,
           max(last_user_lookup) last_user_lookup,
           max(last_user_update) last_user_update,
           sd.name dbname
       FROM
           sys.dm_db_index_usage_stats, master..sysdatabases sd
       WHERE
         database_id = sd.dbid AND database_id > 4
       group by sd.name
    )
    SELECT
       dbname,
       last_read = MAX(last_read),
       last_write = MAX(last_write)
    FROM
    (
       SELECT dbname, last_user_seek, NULL FROM agg
       UNION ALL
       SELECT dbname, last_user_scan, NULL FROM agg
       UNION ALL
       SELECT dbname, last_user_lookup, NULL FROM agg
       UNION ALL
       SELECT dbname, NULL, last_user_update FROM agg
    ) AS x (dbname, last_read, last_write)
    GROUP BY
       dbname
    ORDER BY 1;"

                # put a function around this to enable Pester Testing and also to ease any future changes
                function Invoke-QueryDBlastUsed {
                    $server.Query($querylastused)
                }
                $dblastused = Invoke-QueryDBlastUsed
            }
            
            if ($ExcludeAllUserDb) {
                $DBType = @($true)
            }
            elseif ($ExcludeAllSystemDb) {
                $DBType = @($false)
            }
            else {
                $DBType = @($false, $true)
            }
            
            $Readonly = switch ($Access) { 'Readonly' { @($true) } 'ReadWrite' { @($false) }
                default { @($true, $false) } }
            $Encrypt = switch (Test-Bound $Encrypted) { $true { @($true) }
                default { @($true, $false, $null) } }
            
            $inputobject = $server.Databases |
            Where-Object {
                ($_.Name -in $Database -or !$Database) -and
                ($_.Name -notin $ExcludeDatabase -or !$ExcludeDatabase) -and
                ($_.Owner -in $Owner -or !$Owner) -and
                $_.ReadOnly -in $Readonly -and
                $_.IsSystemObject -in $DBType -and
                ((Compare-Object @($_.Status.tostring().split(',').trim()) $Status -ExcludeDifferent -IncludeEqual).inputobject.count -ge 1 -or !$status) -and
                $_.RecoveryModel -in $RecoveryModel -and
                $_.EncryptionEnabled -in $Encrypt
            }
            
            if ($NoFullBackup -or $NoFullBackupSince) {
                $dabs = (Get-DbaBackupHistory -SqlInstance $server -LastFull -IgnoreCopyOnly)
                if ($null -ne $NoFullBackupSince) {
                    $dabsWithinScope = ($dabs | Where-Object End -lt $NoFullBackupSince)
                    
                    $inputobject = $inputobject | Where-Object { $_.Name -in $dabsWithinScope.Database -and $_.Name -ne 'tempdb' }
                }
                else {
                    $inputObject = $inputObject | Where-Object { $_.Name -notin $dabs.Database -and $_.Name -ne 'tempdb' }
                }
                
            }
            if ($NoLogBackup -or $NoLogBackupSince) {
                $dabs = (Get-DbaBackupHistory -SqlInstance $server -LastLog -IgnoreCopyOnly)
                if ($null -ne $NoLogBackupSince) {
                    $dabsWithinScope = ($dabs | Where-Object End -lt $NoLogBackupSince)
                    $inputobject = $inputobject |
                    Where-Object { $_.Name -in $dabsWithinScope.Database -and $_.Name -ne 'tempdb' -and $_.RecoveryModel -ne 'Simple' }
                }
                else {
                    $inputobject = $inputObject |
                    Where-Object { $_.Name -notin $dabs.Database -and $_.Name -ne 'tempdb' -and $_.RecoveryModel -ne 'Simple' }
                }
            }
            
            $defaults = 'ComputerName', 'InstanceName', 'SqlInstance', 'Name', 'Status', 'IsAccessible', 'RecoveryModel',
            'LogReuseWaitStatus', 'Size as SizeMB', 'CompatibilityLevel as Compatibility', 'Collation', 'Owner',
            'LastBackupDate as LastFullBackup', 'LastDifferentialBackupDate as LastDiffBackup',
            'LastLogBackupDate as LastLogBackup'
            
            if ($NoFullBackup -or $NoFullBackupSince -or $NoLogBackup -or $NoLogBackupSince) {
                $defaults += ('Notes')
            }
            if ($IncludeLastUsed) {
                # Add Last Used to the default view
                $defaults += ('LastRead as LastIndexRead', 'LastWrite as LastIndexWrite')
            }
            
            try {
                foreach ($db in $inputobject) {
                    
                    $Notes = $null
                    if ($NoFullBackup -or $NoFullBackupSince) {
                        if (@($db.EnumBackupSets()).count -eq @($db.EnumBackupSets() | Where-Object { $_.IsCopyOnly }).count -and (@($db.EnumBackupSets()).count -gt 0)) {
                            $Notes = "Only CopyOnly backups"
                        }
                    }
                    
                    $lastusedinfo = $dblastused | Where-Object { $_.dbname -eq $db.name }
                    Add-Member -Force -InputObject $db -MemberType NoteProperty BackupStatus -value $Notes
                    Add-Member -Force -InputObject $db -MemberType NoteProperty -Name ComputerName -value $server.NetName
                    Add-Member -Force -InputObject $db -MemberType NoteProperty -Name InstanceName -value $server.ServiceName
                    Add-Member -Force -InputObject $db -MemberType NoteProperty -Name SqlInstance -value $server.DomainInstanceName
                    Add-Member -Force -InputObject $db -MemberType NoteProperty -Name LastRead -value $lastusedinfo.last_read
                    Add-Member -Force -InputObject $db -MemberType NoteProperty -Name LastWrite -value $lastusedinfo.last_write
                    Select-DefaultView -InputObject $db -Property $defaults
                }
            }
            catch {
                Stop-Function -ErrorRecord $_ -Target $instance -Message "Failure. Collection may have been modified. If so, please use parens (Get-DbaDatabase ....) | when working with commands that modify the collection such as Remove-DbaDatabase" -Continue
            }
        }
    }
}