-
Notifications
You must be signed in to change notification settings - Fork 0
/
RestoreDatabaseToRemoteDB.ps1
76 lines (61 loc) · 2.33 KB
/
RestoreDatabaseToRemoteDB.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
Param (
[Parameter(Position=0, Mandatory=$true)][string] $User,
[Parameter(Position=0, Mandatory=$true)][string] $Password,
[Parameter(Position=0, Mandatory=$true)][string] $DBname,
[Parameter(Position=0, Mandatory=$true)][string] $DatabasesFolder,
[Parameter(Position=0, Mandatory=$false)][string]$serverName = "DB01"
)
cd c:\
<# Config style
<config>
<DB1>aaa.bak</DB1>
<DB2>bbb.bak</DB2>
</config>
#>
function RestoreDatabase([string]$database,
[string]$serverName,
[string]$backupFile,
[string]$User,
[string]$Password)
{
Try
{
Write-host "Starting to restore the database $database to the server $serverName"
$strPass = ConvertTo-SecureString -String $Password -AsPlainText -Force
$cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList ($User, $strPass)
Restore-SqlDatabase -ServerInstance $serverName -Database $database -BackupFile $backupFile -ReplaceDatabase -Credential $cred
}
Catch
{
Write-Error "Error! While invoking the Config bat"
Write-Error ($_.Exception)
}
}
Try
{
dir $DatabasesFolder
$XML = Get-ChildItem $DatabasesFolder config.xml
write-host "XML: "$XML
if ($XML -eq $null)
{
# Write to TeamCity log
Write-host "##teamcity[message text='Faild To find XML file' errorDetails='stack trace' status='ERROR']"
}
Write-Host "Reading XML file" $XML.FullName
[xml]$Config = Get-Content -Path $XML.FullName
$DB1Location = join-path $DatabasesFolder $Config.config.DB1
Write-host "DB1: " $DB1Location
$DB2Location = join-path $DatabasesFolder $Config.config.DB2
Write-host "DB2: " $DB2Location
}
Catch
{
# Write to Team City log
Write-host "##teamcity[message text='Error locating or parsing the config.xml file' errorDetails='($_.Exception)' status='ERROR']"
}
Import-Module “sqlps” -DisableNameChecking | out-null
##prep databases
Write-host "RestoreDatabase1: " $DB1 ", " $serverName ", " $DB1Location ", " $User ", " $Password
RestoreDatabase $DB1 $serverName $DB1Location $User $Password
Write-host "RestoreDatabase2: " $DB2 ", " $serverName ", " $DB2Location ", " $User ", " $Password
RestoreDatabase $DB2 $serverName $DB2Location $User $Password