-
Notifications
You must be signed in to change notification settings - Fork 92
/
Copy pathSetupDatabase.ps1
175 lines (145 loc) · 8 KB
/
SetupDatabase.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
# INPUT
# $restartingInstance (optional)
# $bakFile (optional)
# $appBacpac and tenantBacpac (optional)
# $databaseCredentials (optional)
#
# OUTPUT
# $databaseServer
# $databaseInstance
# $databaseName
#
if ($restartingInstance) {
# Nothing to do
} elseif ($bakfile -ne "") {
# .bak file specified - restore and use
# if bakfile specified, download, restore and use
if ($bakfile.StartsWith("https://") -or $bakfile.StartsWith("http://"))
{
$bakfileurl = $bakfile
$databaseFile = (Join-Path $runPath "mydatabase.bak")
Write-Host "Downloading database backup file '$bakfileurl'"
(New-Object System.Net.WebClient).DownloadFile($bakfileurl, $databaseFile)
} else {
Write-Host "Using Database .bak file '$bakfile'"
if (!(Test-Path -Path $bakfile -PathType Leaf)) {
Write-Error "ERROR: Database Backup File not found."
Write-Error "The file must be uploaded to the container or available on a share."
exit 1
}
$databaseFile = $bakFile
}
# Restore database
$databaseFolder = "c:\databases\my"
if (!(Test-Path -Path $databaseFolder -PathType Container)) {
New-Item -Path $databaseFolder -itemtype Directory | Out-Null
}
$databaseServerInstance = $databaseServer
if ("$databaseInstance" -ne "") {
$databaseServerInstance += "\$databaseInstance"
}
Write-Host "Using database server $databaseServerInstance"
if (!$multitenant) {
New-NAVDatabase -DatabaseServer $databaseServer `
-DatabaseInstance $databaseInstance `
-DatabaseName "$databaseName" `
-FilePath "$databaseFile" `
-DestinationPath "$databaseFolder" `
-Timeout $SqlTimeout | Out-Null
Set-DatabaseCompatibilityLevel -DatabaseServer $databaseServer -DatabaseInstance $databaseInstance -DatabaseName $databaseName
if ($roleTailoredClientFolder -and (Test-Path "$roleTailoredClientFolder\finsql.exe")) {
Start-Process -FilePath "$roleTailoredClientFolder\finsql.exe" -ArgumentList "Command=upgradedatabase, Database=$databaseName, ServerName=$databaseServerInstance, ntauthentication=1, logFile=c:\run\errorlog.txt" -Wait
}
else {
Invoke-NAVApplicationDatabaseConversion -databaseServer $databaseServerInstance -databaseName $databaseName -Force | Out-Null
}
} else {
New-NAVDatabase -DatabaseServer $databaseServer `
-DatabaseInstance $databaseInstance `
-DatabaseName "tenant" `
-FilePath "$databaseFile" `
-DestinationPath "$databaseFolder" `
-Timeout $SqlTimeout | Out-Null
Set-DatabaseCompatibilityLevel -DatabaseServer $databaseServer -DatabaseInstance $databaseInstance -DatabaseName "tenant"
if ($roleTailoredClientFolder -and (Test-Path "$roleTailoredClientFolder\finsql.exe")) {
Start-Process -FilePath "$roleTailoredClientFolder\finsql.exe" -ArgumentList "Command=upgradedatabase, Database=$databaseName, ServerName=$databaseServerInstance, ntauthentication=1, logFile=c:\run\errorlog.txt" -Wait
}
else {
Invoke-NAVApplicationDatabaseConversion -databaseServer $databaseServerInstance -databaseName "tenant" -force | Out-Null
}
Write-Host "Exporting Application to $DatabaseName"
Invoke-sqlcmd -serverinstance $databaseServerInstance -Database "tenant" -query 'CREATE USER "NT AUTHORITY\SYSTEM" FOR LOGIN "NT AUTHORITY\SYSTEM";'
Export-NAVApplication -DatabaseServer $DatabaseServer -DatabaseInstance $DatabaseInstance -DatabaseName "tenant" -DestinationDatabaseName $databaseName -Force -ServiceAccount 'NT AUTHORITY\SYSTEM' | Out-Null
Write-Host "Removing Application from tenant"
Remove-NAVApplication -DatabaseServer $DatabaseServer -DatabaseInstance $DatabaseInstance -DatabaseName "tenant" -Force | Out-Null
}
} elseif ("$appBacpac" -ne "") {
# appBacpac and tenantBacpac specified - restore and use
if (Test-NavDatabase -DatabaseName "tenant") {
Remove-NavDatabase -DatabaseName "tenant"
}
if (Test-NavDatabase -DatabaseName "default") {
Remove-NavDatabase -DatabaseName "default"
}
$dbName = "app"
$appBacpac, $tenantBacpac | % {
if ($_) {
if ($_.StartsWith("https://") -or $_.StartsWith("http://"))
{
$databaseFile = (Join-Path $runPath "${dbName}.bacpac")
Write-Host "Downloading ${dbName}.bacpac"
(New-Object System.Net.WebClient).DownloadFile($_, $databaseFile)
} else {
if (!(Test-Path -Path $_ -PathType Leaf)) {
Write-Error "ERROR: Database Backup File not found."
Write-Error "The file must be uploaded to the container or available on a share."
exit 1
}
$databaseFile = $_
}
Restore-BacpacWithRetry -Bacpac $databaseFile -DatabaseName $dbName
}
$dbName = "tenant"
}
$databaseServer = "localhost"
$databaseInstance = "SQLEXPRESS"
$databaseName = "app"
if ("$licenseFile" -eq "") {
$licenseFile = Join-Path $serviceTierFolder "Cronus.flf"
}
} elseif ($databaseCredentials) {
if (Test-Path $myPath -PathType Container) {
$EncryptionKeyFile = Join-Path $myPath 'DynamicsNAV.key'
} else {
$EncryptionKeyFile = Join-Path $runPath 'DynamicsNAV.key'
}
if (!(Test-Path $EncryptionKeyFile -PathType Leaf)) {
New-NAVEncryptionKey -KeyPath $EncryptionKeyFile -Password $EncryptionSecurePassword -Force | Out-Null
}
Set-NAVServerConfiguration -ServerInstance $ServerInstance -KeyName "EnableSqlConnectionEncryption" -KeyValue "true" -WarningAction SilentlyContinue
Set-NAVServerConfiguration -ServerInstance $ServerInstance -KeyName "TrustSQLServerCertificate" -KeyValue "true" -WarningAction SilentlyContinue
$databaseServerInstance = $databaseServer
if ("$databaseInstance" -ne "") {
$databaseServerInstance += "\$databaseInstance"
}
Write-Host "Import Encryption Key"
Import-NAVEncryptionKey -ServerInstance $ServerInstance `
-ApplicationDatabaseServer $databaseServerInstance `
-ApplicationDatabaseCredentials $DatabaseCredentials `
-ApplicationDatabaseName $DatabaseName `
-KeyPath $EncryptionKeyFile `
-Password $EncryptionSecurePassword `
-WarningAction SilentlyContinue `
-Force
Set-NavServerConfiguration -serverinstance $ServerInstance -databaseCredentials $DatabaseCredentials -WarningAction SilentlyContinue
} elseif ($databaseServer -eq "localhost" -and $databaseInstance -eq "SQLEXPRESS" -and $multitenant) {
if (!(Test-NavDatabase -DatabaseName "tenant")) {
Copy-NavDatabase -SourceDatabaseName $databaseName -DestinationDatabaseName "tenant"
Remove-NavDatabase -DatabaseName $databaseName
Write-Host "Exporting Application to $DatabaseName"
Invoke-sqlcmd -serverinstance "$DatabaseServer\$DatabaseInstance" -Database tenant -query 'CREATE USER "NT AUTHORITY\SYSTEM" FOR LOGIN "NT AUTHORITY\SYSTEM";'
Export-NAVApplication -DatabaseServer $DatabaseServer -DatabaseInstance $DatabaseInstance -DatabaseName "tenant" -DestinationDatabaseName $databaseName -Force -ServiceAccount 'NT AUTHORITY\SYSTEM' | Out-Null
Write-Host "Removing Application from tenant"
Remove-NAVApplication -DatabaseServer $DatabaseServer -DatabaseInstance $DatabaseInstance -DatabaseName "tenant" -Force | Out-Null
}
}