Skip to content

Commit d446f0f

Browse files
committed
Add comments to the powershell module.
1 parent 219680b commit d446f0f

File tree

1 file changed

+190
-105
lines changed

1 file changed

+190
-105
lines changed

RestoreAutomation.psm1

Lines changed: 190 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,14 @@
1-
<#
2-
.SYNOPSIS
3-
<A brief description of the script>
4-
.DESCRIPTION
5-
<A detailed description of the script>
6-
.PARAMETER <paramName>
7-
<Description of script parameter>
8-
.EXAMPLE
9-
<An example of using the script>
10-
#>
11-
121
#load assemblies
13-
142
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | out-null
153
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoExtended") | Out-Null
164

17-
#processing functions
18-
function Get-Restore{
5+
function Get-RestoreObject{
6+
<#
7+
.SYNOPSIS
8+
Internal function used by New-Restore.
9+
.DESCRIPTION
10+
Internal funciton, returns SMO restore object for processing.
11+
#>
1912
Param($db,
2013
$backupfile)
2114

@@ -26,110 +19,178 @@ $rs.NoRecovery=$true
2619
$rs.Action="Database"
2720

2821
return $rs
29-
}#Get-Restore
22+
}#Get-RestoreObject
3023

3124
function Get-Header{
32-
Param($rs,$srv)
25+
<#
26+
.SYNOPSIS
27+
Internal function used by New-Restore.
28+
.DESCRIPTION
29+
Internal funciton, returns SMO backup header for processing.
30+
#>
31+
Param($rs,$srv)
3332

3433
$dt = $restore.ReadBackupHeader($srv)
3534
return $dt.Rows[0]
3635
}#Get-Header
3736

3837
function New-Restore{
39-
param([parameter(Mandatory=$true)][string] $dir
40-
,[parameter(Mandatory=$true)][string] $server
41-
,[string] $database
42-
,[string] $outputdir = ([Environment]::GetFolderPath("MyDocuments"))
43-
,[string] $newdata
44-
,[string] $newlog
45-
,[Switch] $Execute
46-
,[Switch] $NoRecovery)
47-
48-
$sqlout = @()
49-
$smosrv = new-object ('Microsoft.SqlServer.Management.Smo.Server') $server
50-
51-
$full = gci $dir | where {$_.name -like "*.bak"} | Sort-Object LastWriteTime -desc | Select-Object -first 1
52-
$diff = gci $dir | where {$_.name -like "*.dff"} | sort-object LastWriteTime -desc | select-object -first 1
53-
$trns = gci $dir | where {$_.name -like "*.trn"} | sort-object LastWriteTime
54-
55-
#initialize and process full backup
56-
$restore = Get-Restore $database $full
57-
$hfull = Get-Header $restore $smosrv
58-
if($database.Length -eq 0)
59-
{
60-
$database = $hfull.DatabaseName
61-
$restore.Database=$database
62-
}
63-
64-
$LSNCheck = $hfull.FirstLSN
65-
$files = $restore.ReadFileList($smosrv)
66-
foreach($file in $files){
67-
$pfile = $file.PhysicalName
68-
if($newdata -ne $null -and $file.Type -eq "D"){
69-
$pfile=$newdata + $pfile.Substring($pfile.LastIndexOf("\"))
70-
}
38+
<#
39+
.SYNOPSIS
40+
Builds Database Restore script, coordinating Full, Diff, and Log backups.
41+
.DESCRIPTION
42+
Generates a database restore .sql script for restoring a database. This script can be executed by the function
43+
or simply generate the script for fine-tuning for manual execution.
44+
45+
Script acquires files based on extension:
46+
bak = Full
47+
dff = Differential
48+
trn = Transaction log
49+
50+
Mike Fal (http://www.mikefal.net)
51+
52+
.EXAMPLE
53+
New-Restore -dir "C:\database_backups" -server "localhost"
54+
New-Restore -dir "C:\database_backups" -server "localhost" -newdata "X:\MSSQL\data\" -newlog "Y:\MSSQL\logs\"
55+
56+
.PARAMETER dir
57+
Target directory where backup files reside (REQUIRED)
58+
.PARAMETER server
59+
Target server restore used by SMO to build restore script. Should be server you want to restore to. (REQUIRED)
60+
.PARAMETER database
61+
Database name to restore. If blank, database name from the backup will be used.
62+
.PARAMETER outputdir
63+
Output directory for script. If empty, user's My Documents will be used.
64+
.PARAMETER newdata
65+
New location of database data files. If not specified, restore will assume default location of the data files.
66+
.PARAMETER newlog
67+
New location of database log files. If not specified, restore will assume default location of the log files.
68+
.PARAMETER Execute
69+
Switch parameter. If true, restore will be executed after script is generated.
70+
.PARAMETER NoRecovery
71+
Switch parameter. If true, script will not fully recover database.
72+
73+
.RELATEDLINKS
74+
http://www.mikefal.net
75+
https://github.com/MikeFal/PowerShell
76+
#>
77+
78+
param([parameter(Mandatory=$true)][string] $dir
79+
,[parameter(Mandatory=$true)][string] $server
80+
,[string] $database
81+
,[string] $outputdir = ([Environment]::GetFolderPath("MyDocuments"))
82+
,[string] $newdata
83+
,[string] $newlog
84+
,[Switch] $Execute
85+
,[Switch] $NoRecovery)
86+
87+
$sqlout = @()
88+
$smosrv = new-object ('Microsoft.SqlServer.Management.Smo.Server') $server
89+
90+
$full = gci $dir | where {$_.name -like "*.bak"} | Sort-Object LastWriteTime -desc | Select-Object -first 1
91+
$diff = gci $dir | where {$_.name -like "*.dff"} | sort-object LastWriteTime -desc | select-object -first 1
92+
$trns = gci $dir | where {$_.name -like "*.trn"} | sort-object LastWriteTime
93+
94+
#initialize and process full backup
95+
$restore = Get-RestoreObject $database $full
96+
$hfull = Get-Header $restore $smosrv
97+
if($database.Length -eq 0)
98+
{
99+
$database = $hfull.DatabaseName
100+
$restore.Database=$database
101+
}
102+
103+
$LSNCheck = $hfull.FirstLSN
104+
$files = $restore.ReadFileList($smosrv)
105+
foreach($file in $files){
106+
$pfile = $file.PhysicalName
107+
if($newdata -ne $null -and $file.Type -eq "D"){
108+
$pfile=$newdata + $pfile.Substring($pfile.LastIndexOf("\"))
109+
}
71110

72-
if($newlog -ne $null -and $file.Type -eq "L"){
73-
$pfile=$newlog + $pfile.Substring($pfile.LastIndexOf("\"))
74-
}
111+
if($newlog -ne $null -and $file.Type -eq "L"){
112+
$pfile=$newlog + $pfile.Substring($pfile.LastIndexOf("\"))
113+
}
75114

76-
$newfile = New-Object("Microsoft.SqlServer.Management.Smo.RelocateFile") ($file.LogicalName,$pfile)
77-
$restore.RelocateFiles.Add($newfile) | out-null
78-
}
79-
80-
$sqlout += "/****************************************************"
81-
$sqlout += "Restore Database Script Generated $(Get-Date)"
82-
$sqlout += "Database: "+$database
83-
$sqlout += "****************************************************/"
84-
$sqlout += "--FULL RESTORE"
85-
$sqlout += $restore.Script($smosrv)
86-
87-
#process differential backups
88-
if($diff -ne $null){
89-
$restore = Get-Restore $database $diff
90-
$hdiff = Get-Header $restore $smosrv
91-
92-
if($hdiff.DifferentialBaseLSN -eq $LSNCheck){
93-
$sqlout += "--DIFF RESTORE"
94-
$sqlout += $restore.Script($smosrv)
95-
$LSNCheck = $hdiff.LastLSN
96-
}
97-
else{
98-
$LSNCheck = $hfull.LastLSN
99-
}
100-
}
101-
102-
#process transaction log backups
103-
if($trns -ne $null){
104-
$sqlout += "--TRN LOG RESTORE"
105-
106-
foreach ($trn in $trns){
107-
$restore = Get-Restore $database $trn
108-
$htrn = Get-Header $restore $smosrv
109-
if($htrn.FirstLSN -le $LSNCheck -and $htrn.LastLSN -ge $LSNCheck){
110-
$sqlout += $restore.Script($smosrv)
111-
$LSNCheck = $htrn.LastLSN
112-
}
113-
}
114-
}
115-
116-
#Write final recovery line if necessary
117-
if(!($NoRecovery)){
118-
$sqlout += "`r`n"
119-
$sqlout += "--COMPLETE RESTORE/ONLINE DB"
120-
$sqlout += "RESTORE DATABASE "+$database + " WITH RECOVERY"
121-
}
122-
123-
#output script file
124-
$sqlout | Out-File "$outputdir\restore_$database.sql"
125-
126-
#If called, execute script
127-
if($Execute){
128-
sqlcmd -S "$server" -E -i "$outputdir\restore_$database.sql"
129-
}
115+
$newfile = New-Object("Microsoft.SqlServer.Management.Smo.RelocateFile") ($file.LogicalName,$pfile)
116+
$restore.RelocateFiles.Add($newfile) | out-null
117+
}
118+
119+
$sqlout += "/****************************************************"
120+
$sqlout += "Restore Database Script Generated $(Get-Date)"
121+
$sqlout += "Database: "+$database
122+
$sqlout += "****************************************************/"
123+
$sqlout += "--FULL RESTORE"
124+
$sqlout += $restore.Script($smosrv)
125+
126+
#process differential backups
127+
if($diff -ne $null){
128+
$restore = Get-RestoreObject $database $diff
129+
$hdiff = Get-Header $restore $smosrv
130+
131+
if($hdiff.DifferentialBaseLSN -eq $LSNCheck){
132+
$sqlout += "--DIFF RESTORE"
133+
$sqlout += $restore.Script($smosrv)
134+
$LSNCheck = $hdiff.LastLSN
135+
}
136+
else{
137+
$LSNCheck = $hfull.LastLSN
138+
}
139+
}
140+
141+
#process transaction log backups
142+
if($trns -ne $null){
143+
$sqlout += "--TRN LOG RESTORE"
144+
145+
foreach ($trn in $trns){
146+
$restore = Get-RestoreObject $database $trn
147+
$htrn = Get-Header $restore $smosrv
148+
if($htrn.FirstLSN -le $LSNCheck -and $htrn.LastLSN -ge $LSNCheck){
149+
$sqlout += $restore.Script($smosrv)
150+
$LSNCheck = $htrn.LastLSN
151+
}
152+
}
153+
}
154+
155+
#Write final recovery line if necessary
156+
if(!($NoRecovery)){
157+
$sqlout += "`r`n"
158+
$sqlout += "--COMPLETE RESTORE/ONLINE DB"
159+
$sqlout += "RESTORE DATABASE "+$database + " WITH RECOVERY"
160+
}
161+
162+
#output script file
163+
$sqlout | Out-File "$outputdir\restore_$database.sql"
164+
165+
#If called, execute script
166+
if($Execute){
167+
sqlcmd -S "$server" -E -i "$outputdir\restore_$database.sql"
168+
}
130169
} #New-Restore
131170

132171
function Sync-DBUsers{
172+
<#
173+
.SYNOPSIS
174+
Synchronizes orphaned users with logins. Returns a list of users that have no corresponding login to sync to.
175+
.DESCRIPTION
176+
Repairs orphaned logins for newly restored databases. It will compare login names to unmatched SIDs and execute
177+
ALTER USER commands to correct the relationship. Any users that can not be matched to a login will be listed
178+
as the output.
179+
180+
Mike Fal (http://www.mikefal.net)
181+
182+
.EXAMPLE
183+
Sync-DBUsers -server "localhosts" -database "tpcc"
184+
185+
.PARAMETER server
186+
Server where the database resides that is being synchronized.
187+
.PARAMETER database
188+
Database that has users that needs to be synchronized.
189+
190+
.RELATEDLINKS
191+
http://www.mikefal.net
192+
https://github.com/MikeFal/PowerShell
193+
#>
133194
param([parameter(Mandatory=$true)][string] $server
134195
,[parameter(Mandatory=$true)][string] $database)
135196

@@ -160,6 +221,30 @@ function Sync-DBUsers{
160221
}#Sync-DBUsers
161222

162223
function Get-DBCCCheckDB{
224+
<#
225+
.SYNOPSIS
226+
Executes simple DBCC CHECKDB agasints a target database, providing output of the execution.
227+
.DESCRIPTION
228+
Executes a DBCC CHECKDB WITH PHYSICAL_ONLY, TABLERESULTS against a target database. Output
229+
is a datatable of the check output. If the -Full parameter is passed, the command will run a
230+
full DBCC check.
231+
232+
Mike Fal (http://www.mikefal.net)
233+
234+
.EXAMPLE
235+
Sync-DBUsers -server "localhosts" -database "tpcc"
236+
237+
.PARAMETER server
238+
Server where the database resides that is being checked.
239+
.PARAMETER database
240+
Database that has users that needs to be checked.
241+
.PARAMETER Full
242+
Switch parameter. If true, a full DBCC check is done against the target database.
243+
244+
.RELATEDLINKS
245+
http://www.mikefal.net
246+
https://github.com/MikeFal/PowerShell
247+
#>
163248
param([parameter(Mandatory=$true)][string] $server
164249
,[parameter(Mandatory=$true)][string] $database
165250
,[Switch] $Full)

0 commit comments

Comments
 (0)