-
Notifications
You must be signed in to change notification settings - Fork 68
/
FindSqlPackagePath.ps1
520 lines (429 loc) · 16.6 KB
/
FindSqlPackagePath.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
#
# from https://raw.githubusercontent.com/Microsoft/vsts-tasks/master/Tasks/SqlAzureDacpacDeploymentV1/FindSqlPackagePath.ps1
#
function Find-File {
[CmdletBinding()]
param(
[string]$fileName
)
$file_location = Get-Command $fileName -erroraction 'silentlycontinue'
if(!$file_location){
$file_location = Get-Command $PSScriptRoot\$fileName -erroraction 'silentlycontinue'
}
if(!$file_location){
throw [System.IO.FileNotFoundException] "$fileName not found."
}
$file_location = $file_location | Select-Object -ExpandProperty Definition
return $file_location
}
function Get-SqlPackageOnTargetMachine
{
try
{
$sqlDacPath, $sqlVersion = Locate-HighestVersionSqlPackageWithSql
$sqlVersionNumber = [decimal] $sqlVersion
}
catch [System.Exception]
{
Write-Verbose ("Failed to get Dac Framework (installed with SQL Server) location with exception: " + $_.Exception.Message)
$sqlVersionNumber = 0
}
try
{
$sqlMsiDacPath, $sqlMsiVersion = Locate-HighestVersionSqlPackageWithDacMsi
$sqlMsiVersionNumber = [decimal] $sqlMsiVersion
}
catch [System.Exception]
{
Write-Verbose ("Failed to get Dac Framework (installed with DAC Framework) location with exception: " + $_.Exception.Message) -verbose
$sqlMsiVersionNumber = 0
}
try
{
$vsDacPath, $vsVersion = Locate-HighestVersionSqlPackageInVS
$vsVersionNumber = [decimal] $vsVersion
}
catch [System.Exception]
{
Write-Verbose ("Failed to get Dac Framework (installed with Visual Studio) location with exception: " + $_.Exception.Message)
$vsVersionNumber = 0
}
$maximumVersion = [decimal]$(@($vsVersionNumber, $sqlVersionNumber, $sqlMsiVersionNumber) | Measure-Object -Maximum).Maximum
if ($sqlMsiVersion -eq $maximumVersion)
{
$dacPath = $sqlMsiDacPath
}
elseif ($vsVersionNumber -eq $maximumVersion)
{
$dacPath = $vsDacPath
}
elseif ($sqlVersionNumber -eq $maximumVersion)
{
$dacPath = $sqlDacPath
}
if ($dacPath -eq $null)
{
throw "Unable to find the location of Dac Framework (SqlPackage.exe) from registry on machine $env:COMPUTERNAME"
}
else
{
return $dacPath
}
}
function Get-RegistryValueIgnoreError
{
param
(
[parameter(Mandatory = $true)]
[Microsoft.Win32.RegistryHive]
$RegistryHive,
[parameter(Mandatory = $true)]
[System.String]
$Key,
[parameter(Mandatory = $true)]
[System.String]
$Value,
[parameter(Mandatory = $true)]
[Microsoft.Win32.RegistryView]
$RegistryView
)
try
{
$baseKey = [Microsoft.Win32.RegistryKey]::OpenBaseKey($RegistryHive, $RegistryView)
$subKey = $baseKey.OpenSubKey($Key)
if($subKey -ne $null)
{
return $subKey.GetValue($Value)
}
}
catch
{
}
return $null
}
function Get-RegistrySubKeysIgnoreError
{
param
(
[parameter(Mandatory = $true)]
[Microsoft.Win32.RegistryHive]
$RegistryHive,
[parameter(Mandatory = $true)]
[System.String]
$Key,
[parameter(Mandatory = $true)]
[Microsoft.Win32.RegistryView]
$RegistryView
)
try
{
$baseKey = [Microsoft.Win32.RegistryKey]::OpenBaseKey($RegistryHive, $RegistryView)
$subKey = $baseKey.OpenSubKey($Key)
return $subKey
}
catch
{
}
return $null
}
function Get-SubKeysInFloatFormat($keys)
{
$targetKeys = @()
foreach ($key in $keys)
{
try {
$targetKeys += [decimal] $key
}
catch {}
}
$targetKeys
}
function Get-SqlPackageForSqlVersion([int] $majorVersion, [bool] $wow6432Node)
{
$sqlInstallRootRegKey = "SOFTWARE", "Microsoft", "Microsoft SQL Server", "$majorVersion" -join [System.IO.Path]::DirectorySeparatorChar
if ($wow6432Node -eq $true)
{
$sqlInstallRootPath = Get-RegistryValueIgnoreError LocalMachine "$sqlInstallRootRegKey" "VerSpecificRootDir" Registry64
}
else
{
$sqlInstallRootPath = Get-RegistryValueIgnoreError LocalMachine "$sqlInstallRootRegKey" "VerSpecificRootDir" Registry32
}
if ($sqlInstallRootPath -eq $null)
{
return $null
}
Write-Verbose "Sql Version Specific Root Dir for version $majorVersion as read from registry: $sqlInstallRootPath"
$DacInstallPath = [System.IO.Path]::Combine($sqlInstallRootPath, "Dac", "bin", "SqlPackage.exe")
if (Test-Path $DacInstallPath)
{
Write-Verbose "Dac Framework installed with SQL Version $majorVersion found at $DacInstallPath on machine $env:COMPUTERNAME"
return $DacInstallPath
}
else
{
return $null
}
}
function Locate-HighestVersionSqlPackageWithSql()
{
$sqlRegKey = "HKLM:", "SOFTWARE", "Wow6432Node", "Microsoft", "Microsoft SQL Server"-join [System.IO.Path]::DirectorySeparatorChar
$sqlRegKey64 = "HKLM:", "SOFTWARE", "Microsoft", "Microsoft SQL Server"-join [System.IO.Path]::DirectorySeparatorChar
if (-not (Test-Path $sqlRegKey))
{
$sqlRegKey = $sqlRegKey64
}
if (-not (Test-Path $sqlRegKey))
{
return $null, 0
}
$keys = Get-Item $sqlRegKey | %{$_.GetSubKeyNames()}
$versions = Get-SubKeysInFloatFormat $keys | Sort-Object -Descending
Write-Verbose "Sql Versions installed on machine $env:COMPUTERNAME as read from registry: $versions"
foreach ($majorVersion in $versions)
{
$DacInstallPathWow6432Node = Get-SqlPackageForSqlVersion $majorVersion $true
$DacInstallPath = Get-SqlPackageForSqlVersion $majorVersion $false
if ($DacInstallPathWow6432Node -ne $null)
{
return $DacInstallPathWow6432Node, $majorVersion
}
elseif ($DacInstallPath -ne $null)
{
return $DacInstallPath, $majorVersion
}
}
Write-Verbose "Dac Framework (installed with SQL) not found on machine $env:COMPUTERNAME"
return $null, 0
}
function Locate-HighestVersionSqlPackageWithDacMsi()
{
$sqlDataTierFrameworkRegKeyWow = "HKLM:", "SOFTWARE", "Wow6432Node", "Microsoft", "Microsoft SQL Server", "Data-Tier Application Framework" -join [System.IO.Path]::DirectorySeparatorChar
$sqlDataTierFrameworkRegKey = "HKLM:", "SOFTWARE", "Microsoft", "Microsoft SQL Server", "Data-Tier Application Framework" -join [System.IO.Path]::DirectorySeparatorChar
if (-not (Test-Path $sqlDataTierFrameworkRegKey))
{
$sqlDataTierFrameworkRegKey = $sqlDataTierFrameworkRegKeyWow
}
if ((Test-Path $sqlDataTierFrameworkRegKey))
{
$keys = Get-Item $sqlDataTierFrameworkRegKey | %{$_.GetSubKeyNames()}
$versions = Get-SubKeysInFloatFormat $keys | Sort-Object -Descending
$installedMajorVersion = 0
foreach ($majorVersion in $versions)
{
$sqlInstallRootRegKey = "SOFTWARE", "Microsoft", "Microsoft SQL Server", "Data-Tier Application Framework", "$majorVersion" -join [System.IO.Path]::DirectorySeparatorChar
$sqlInstallRootPath64 = Get-RegistryValueIgnoreError LocalMachine "$sqlInstallRootRegKey" "InstallDir" Registry64
$sqlInstallRootPath32 = Get-RegistryValueIgnoreError LocalMachine "$sqlInstallRootRegKey" "InstallDir" Registry32
if ($sqlInstallRootPath64 -ne $null)
{
$sqlInstallRootPath = $sqlInstallRootPath64
break
}
if ($sqlInstallRootPath32 -ne $null)
{
$sqlInstallRootPath = $sqlInstallRootPath32
break
}
}
$DacInstallPath = [System.IO.Path]::Combine($sqlInstallRootPath, "SqlPackage.exe")
if (Test-Path $DacInstallPath)
{
Write-Verbose "Dac Framework installed with SQL Version $majorVersion found at $DacInstallPath on machine $env:COMPUTERNAME"
return $DacInstallPath, $majorVersion
}
}
$sqlRegKeyWow = "HKLM:", "SOFTWARE", "Wow6432Node", "Microsoft", "Microsoft SQL Server", "DACFramework", "CurrentVersion" -join [System.IO.Path]::DirectorySeparatorChar
$sqlRegKey = "HKLM:", "SOFTWARE", "Microsoft", "Microsoft SQL Server", "DACFramework", "CurrentVersion" -join [System.IO.Path]::DirectorySeparatorChar
$sqlKey = "SOFTWARE", "Microsoft", "Microsoft SQL Server", "DACFramework", "CurrentVersion" -join [System.IO.Path]::DirectorySeparatorChar
if (Test-Path $sqlRegKey)
{
$dacVersion = Get-RegistryValueIgnoreError LocalMachine "$sqlKey" "Version" Registry64
$majorVersion = $dacVersion.Substring(0, $dacVersion.IndexOf(".")) + "0"
}
if (Test-Path $sqlRegKeyWow)
{
$dacVersionX86 = Get-RegistryValueIgnoreError LocalMachine "$sqlKey" "Version" Registry32
$majorVersionX86 = $dacVersionX86.Substring(0, $dacVersionX86.IndexOf(".")) + "0"
}
if ((-not($dacVersion)) -and (-not($dacVersionX86)))
{
Write-Verbose "Dac Framework (installed with DAC Framework) not found on machine $env:COMPUTERNAME"
return $null, 0
}
if ($majorVersionX86 -gt $majorVersion)
{
$majorVersion = $majorVersionX86
}
$dacRelativePath = "Microsoft SQL Server", "$majorVersion", "DAC", "bin", "SqlPackage.exe" -join [System.IO.Path]::DirectorySeparatorChar
$programFiles = $env:ProgramFiles
$programFilesX86 = "${env:ProgramFiles(x86)}"
if (-not ($programFilesX86 -eq $null))
{
$dacPath = $programFilesX86, $dacRelativePath -join [System.IO.Path]::DirectorySeparatorChar
if (Test-Path("$dacPath"))
{
Write-Verbose "Dac Framework (installed with DAC Framework Msi) found on machine $env:COMPUTERNAME at $dacPath"
return $dacPath, $majorVersion
}
}
if (-not ($programFiles -eq $null))
{
$dacPath = $programFiles, $dacRelativePath -join [System.IO.Path]::DirectorySeparatorChar
if (Test-Path($dacPath))
{
Write-Verbose "Dac Framework (installed with DAC Framework Msi) found on machine $env:COMPUTERNAME at $dacPath"
return $dacPath, $majorVersion
}
}
return $null, 0
}
function Locate-SqlPackageInVS_15_0()
{
$vs15 = Get-VisualStudio_15_0
if ($vs15 -and $vs15.installationPath) {
# End with "\" for consistency with old ShellFolder values.
$shellFolder15 = $vs15.installationPath.TrimEnd('\'[0]) + "\"
# Test for the DAC directory.
$dacParentDir = [System.IO.Path]::Combine($shellFolder15, 'Common7', 'IDE', 'Extensions', 'Microsoft', 'SQLDB', 'DAC')
$dacInstallPath, $dacInstallVersion = Get-LatestVersionSqlPackageInDacDirectory -dacParentDir $dacParentDir
if($dacInstallPath)
{
return $dacInstallPath, $dacInstallVersion
}
}
return $null, 0
}
function Locate-SqlPackageInVS([string] $version)
{
$vsRegKeyForVersion = "SOFTWARE", "Microsoft", "VisualStudio", $version -join [System.IO.Path]::DirectorySeparatorChar
$vsInstallDir = Get-RegistryValueIgnoreError LocalMachine "$vsRegKeyForVersion" "InstallDir" Registry64
if ($vsInstallDir -eq $null)
{
$vsInstallDir = Get-RegistryValueIgnoreError LocalMachine "$vsRegKeyForVersion" "InstallDir" Registry32
}
if ($vsInstallDir)
{
Write-Verbose "Visual Studio install location: $vsInstallDir"
$dacExtensionPath = [System.IO.Path]::Combine("Extensions", "Microsoft", "SQLDB", "DAC")
$dacParentDir = [System.IO.Path]::Combine($vsInstallDir, $dacExtensionPath)
$dacInstallPath, $dacInstallVersion = Get-LatestVersionSqlPackageInDacDirectory -dacParentDir $dacParentDir
if($dacInstallPath)
{
return $dacInstallPath, $dacInstallVersion
}
}
return $null, 0
}
function Get-LatestVersionSqlPackageInDacDirectory([string] $dacParentDir)
{
if (Test-Path $dacParentDir)
{
$dacVersionDirs = Get-ChildItem $dacParentDir | Sort-Object @{e={$_.Name -as [int]}} -Descending
foreach ($dacVersionDir in $dacVersionDirs)
{
$dacVersion = $dacVersionDir.Name
$dacFullPath = [System.IO.Path]::Combine($dacVersionDir.FullName, "SqlPackage.exe")
if(Test-Path $dacFullPath -pathtype leaf)
{
Write-Verbose "Dac Framework installed with Visual Studio found at $dacFullPath on machine $env:COMPUTERNAME"
return $dacFullPath, $dacVersion
}
else
{
Write-Verbose "Unable to find Dac framework installed with Visual Studio at $($dacVersionDir.FullName) on machine $env:COMPUTERNAME"
}
}
}
return $null, 0
}
function Locate-HighestVersionSqlPackageInVS()
{
# Locate SqlPackage.exe in VS 15.0
$dacFullPath, $dacVersion = Locate-SqlPackageInVS_15_0
if ($dacFullPath -ne $null)
{
return $dacFullPath, $dacVersion
}
#Locate SqlPackage.exe in older version
$vsRegKey = "HKLM:", "SOFTWARE", "Wow6432Node", "Microsoft", "VisualStudio" -join [System.IO.Path]::DirectorySeparatorChar
$vsRegKey64 = "HKLM:", "SOFTWARE", "Microsoft", "VisualStudio" -join [System.IO.Path]::DirectorySeparatorChar
if (-not (Test-Path $vsRegKey))
{
$vsRegKey = $vsRegKey64
}
if (-not (Test-Path $vsRegKey))
{
Write-Verbose "Visual Studio not found on machine $env:COMPUTERNAME"
return $null, 0
}
$keys = Get-Item $vsRegKey | %{$_.GetSubKeyNames()}
$versions = Get-SubKeysInFloatFormat $keys | Sort-Object -Descending
Write-Verbose "Visual Studio versions found on machine $env:COMPUTERNAME as read from registry: $versions"
foreach ($majorVersion in $versions)
{
$dacFullPath, $dacVersion = Locate-SqlPackageInVS $majorVersion
if ($dacFullPath -ne $null)
{
return $dacFullPath, $dacVersion
}
}
Write-Verbose "Dac Framework (installed with Visual Studio) not found on machine $env:COMPUTERNAME "
return $null, 0
}
function Get-VisualStudio_15_0 {
[CmdletBinding()]
param()
$visualStudioInstallDir = $null
try {
# Query for the latest 15.* version.
#
# Note, the capability is registered as VisualStudio_15.0, however the actual version
# may be something like 15.2.
Write-Verbose "Getting latest Visual Studio 15 setup instance."
$output = New-Object System.Text.StringBuilder
# Attempt to locate the vswhere.exe command which could either be in the path or in
# the script root.
$vswhere_location = Find-File -fileName "vswhere.exe"
Invoke-VstsTool -FileName "$vswhere_location" -Arguments "-version [15.0,16.0) -latest -format json" -RequireExitCodeZero 2>&1 |
ForEach-Object {
if ($_ -is [System.Management.Automation.ErrorRecord]) {
Write-Verbose "STDERR: $($_.Exception.Message)"
}
else {
Write-Verbose $_
$null = $output.AppendLine($_)
}
}
$visualStudioInstallDir = (ConvertFrom-Json -InputObject $output.ToString()) |
Select-Object -First 1
if (!$visualStudioInstallDir) {
# Query for the latest 15.* BuildTools.
#
# Note, whereas VS 15.x version number is always 15.0.*, BuildTools does not follow the
# the same scheme. It appears to follow the 15.<UPDATE_NUMBER>.* versioning scheme.
Write-Verbose "Getting latest BuildTools 15 setup instance."
$output = New-Object System.Text.StringBuilder
Invoke-VstsTool -FileName "$vswhere_location" -Arguments "-version [15.0,16.0) -products Microsoft.VisualStudio.Product.BuildTools -latest -format json" -RequireExitCodeZero 2>&1 |
ForEach-Object {
if ($_ -is [System.Management.Automation.ErrorRecord]) {
Write-Verbose "STDERR: $($_.Exception.Message)"
}
else {
Write-Verbose $_
$null = $output.AppendLine($_)
}
}
$visualStudioInstallDir = (ConvertFrom-Json -InputObject $output.ToString()) |
Select-Object -First 1
}
} catch {
Write-Verbose ($_ | Out-String)
$visualStudioInstallDir = $null
}
return $visualStudioInstallDir
}
function Get-SQLPackagePath
{
$sqlPackage = Get-SqlPackageOnTargetMachine
return $sqlPackage
}