functions/Get-DbaRestoreHistory.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
Function Get-DbaRestoreHistory
{
<#
.SYNOPSIS
Returns restore history details for databases on a SQL Server
  
.DESCRIPTION
By default, this command will return the server name, database, username, restore type, date, from file and to files.
 
Thanks to https://www.mssqltips.com/sqlservertip/1724/when-was-the-last-time-your-sql-server-database-was-restored/ for the query and https://sqlstudies.com/2016/07/27/when-was-this-database-restored/ for the idea.
  
.PARAMETER SqlServer
The SQL Server that you're connecting to.
 
.PARAMETER Credential
Credential object used to connect to the SQL Server as a different user
 
.PARAMETER Databases
Return restore information for only specific databases. These are only the databases that currently exist on the server.
  
.PARAMETER Exclude
Return restore information for all but these specific databases
 
.PARAMETER Since
Datetime object used to narrow the results to a date
  
.PARAMETER Detailed
Returns default information plus From (\\server\backups\test.bak) and To (the mdf and ldf locations) information
  
.PARAMETER Force
Returns a ton of information about the backup history with no max rows
 
.NOTES
Tags: DisasterRecovery, Backup, Restore
dbatools PowerShell module (https://dbatools.io, clemaire@gmail.com)
Copyright (C) 2016 Chrissy LeMaire
 
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
 
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 
You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
 
.LINK
https://dbatools.io/Get-DbaRestoreHistory
 
.EXAMPLE
Get-DbaRestoreHistory -SqlServer sqlserver2014a
 
Returns server name, database, username, restore type, date for all restored databases on sqlserver2014a.
 
.EXAMPLE
Get-DbaRestoreHistory -SqlServer sqlserver2014a -Databases db1, db2 -Since '7/1/2016 10:47:00'
 
Returns restore information only for databases db1 and db2 on sqlserve2014a since July 1, 2016 at 10:47 AM.
  
.EXAMPLE
Get-DbaRestoreHistory -SqlServer sqlserver2014a, sql2016 -Detailed -Exclude db1
 
Lots of detailed information for all databases except db1 on sqlserver2014a and sql2016
 
.EXAMPLE
Get-DbaRestoreHistory -SqlServer sql2014 -Databases AdventureWorks2014, pubs -Detailed | Format-Table
 
Adds From and To file information to output, returns information only for AdventureWorks2014 and pubs, and makes the output pretty
 
.EXAMPLE
Get-SqlRegisteredServerName -SqlServer sql2016 | Get-DbaRestoreHistory
 
Returns database restore information for every database on every server listed in the Central Management Server on sql2016
  
#>

[CmdletBinding()]
Param (
[parameter(Mandatory = $true, ValueFromPipeline = $true)]
[Alias("ServerInstance", "SqlInstance")]
[string[]]$SqlServer,
[Alias("SqlCredential")]
[PsCredential]$Credential,
[datetime]$Since,
[switch]$Detailed,
[switch]$Force
)

DynamicParam { if ($SqlServer) { return Get-ParamSqlDatabases -SqlServer $SqlServer[0] -SqlCredential $Credential } }

BEGIN
{
# Convert from RuntimeDefinedParameter object to regular array
$databases = $psboundparameters.Databases
$exclude = $psboundparameters.Exclude

if ($Since -ne $null)
{
$Since = $Since.ToString("yyyy-MM-dd HH:mm:ss")
}
}

PROCESS
{
foreach ($server in $SqlServer)
{
try
{
$sourceserver = Connect-SqlServer -SqlServer $server -SqlCredential $Credential

if ($sourceserver.VersionMajor -lt 9)
{
Write-Warning "SQL Server 2000 not supported"
continue
}

if ($force -eq $true)
{
$select = "SELECT * "
}
else
{
$select = "SELECT
         '$server' AS [Server],
         rsh.destination_database_name AS [Database],
         --rsh.restore_history_id as RestoreHistoryID,
         rsh.user_name AS [Username],
         CASE
       WHEN rsh.restore_type = 'D' THEN 'Database'
       WHEN rsh.restore_type = 'F' THEN 'File'
       WHEN rsh.restore_type = 'G' THEN 'Filegroup'
       WHEN rsh.restore_type = 'I' THEN 'Differential'
       WHEN rsh.restore_type = 'L' THEN 'Log'
       WHEN rsh.restore_type = 'V' THEN 'Verifyonly'
       WHEN rsh.restore_type = 'R' THEN 'Revert'
       ELSE rsh.restore_type
         END AS [RestoreType],
         rsh.restore_date AS [Date],
         ISNULL(STUFF((SELECT ', ' + bmf.physical_device_name
         FROM msdb.dbo.backupmediafamily bmf
           WHERE bmf.media_set_id = bs.media_set_id
         FOR XML PATH('')), 1, 2, ''), '') AS [From],
         ISNULL(STUFF((SELECT ', ' + rf.destination_phys_name
         FROM msdb.dbo.restorefile rf
           WHERE rsh.restore_history_id = rf.restore_history_id
         FOR XML PATH('')), 1, 2, ''), '') AS [To]
      "

}

$from = " FROM msdb.dbo.restorehistory rsh
     INNER JOIN msdb.dbo.backupset bs ON rsh.backup_set_id = bs.backup_set_id"


if ($exclude.length -gt 0 -or $databases.length -gt 0 -or $Since.length -gt 0)
{
$where = " WHERE "
}

$wherearray = @()

if ($exclude.length -gt 0)
{
$dblist = $exclude -join "','"
$wherearray += " destination_database_name not in ('$dblist')"
}

if ($databases.length -gt 0)
{
$dblist = $databases -join "','"
$wherearray += "destination_database_name in ('$dblist')"
}

if ($Since -ne $null)
{
$wherearray += "rsh.restore_date >= '$since'"
}

if ($where.length -gt 0)
{
$wherearray = $wherearray -join " and "
$where = "$where $wherearray"
}

$sql = "$select $from $where"
Write-Debug $sql

if ($Detailed -eq $true -or $Force -eq $true)
{
return $sourceserver.ConnectionContext.ExecuteWithResults($sql).Tables.Rows
}

$sourceserver.ConnectionContext.ExecuteWithResults($sql).Tables.Rows | Select-Object * -ExcludeProperty From, To, RowError, Rowstate, table, itemarray, haserrors
}
catch
{
Write-Warning "$_ `nMoving on"
continue
}
}
}
}