-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrestoreDatabases.ps1
More file actions
42 lines (32 loc) · 2.64 KB
/
restoreDatabases.ps1
File metadata and controls
42 lines (32 loc) · 2.64 KB
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
param($sqlserver = 'sql.tpondemand.net', $filepath = '.\')
$env:psmodulepath = Resolve-Path ".\Modules" | select -ExpandProperty Path
import-module sqlserver -force
Function restoreDatabase($backupFilePath) {
$backupFilePath = Resolve-Path $backupFilePath | select -ExpandProperty Path
$dbname = Get-ChildItem $backupFilePath | select -ExpandProperty basename
$dataPath = Get-SqlDefaultDir -sqlserver $server -dirtype Data
$logPath = Get-SqlDefaultDir -sqlserver $server -dirtype Log
$relocateFiles = @{}
Invoke-SqlRestore -sqlserver $server -filepath $backupFilePath -fileListOnly | foreach {
if ($_.Type -eq 'L') {
$physicalName = "$logPath\{0}_log.LDF" -f $dbname
}
else {
$physicalName = "$dataPath\{0}.mdf" -f $dbname
}
$relocateFiles.Add("$($_.LogicalName)", "$physicalName")
}
$server.KillAllProcesses($dbname)
Invoke-SqlRestore -sqlserver $server -dbname $dbname -filepath $backupFilePath -relocatefiles $relocateFiles -force
}
$server = get-sqlserver $sqlserver
$path = Get-Item $filepath
if (!$path.PSIsContainer) {
restoreDatabase $filepath
}
else {
$path | Get-ChildItem | where { $_.Extension -eq '.bak' } |
foreach {
restoreDatabase $_.Fullname
}
}