-
Notifications
You must be signed in to change notification settings - Fork 29
/
Import-VsCodeDatabaseConnections.ps1
64 lines (52 loc) · 1.81 KB
/
Import-VsCodeDatabaseConnections.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
<#
.SYNOPSIS
Adds config XDT connection strings to VSCode settings.
.FUNCTIONALITY
VSCode
.LINK
https://marketplace.visualstudio.com/items?itemName=ms-mssql.mssql
.LINK
http://code.visualstudio.com/
.LINK
https://git-scm.com/docs/git-rev-parse
.LINK
Add-VsCodeDatabaseConnection.ps1
.LINK
Get-ConfigConnectionStringBuilders.ps1
.LINK
Import-Variables.ps1
.EXAMPLE
Import-VsCodeDatabaseConnections.ps1
Adds any new (by name) connection strings found in XDT .config files into
the .vscode/settings.json mssql.connections collection for the mssql extension.
#>
#Requires -Version 3
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseSingularNouns','',
Justification='All configurations are returned as a collection.')]
[CmdletBinding()][OutputType([void])] Param()
function Get-ConfigConnections
{
$connections = @()
foreach($config in (Get-ChildItem -Recurse -Filter *.config |Select-Object -ExpandProperty FullName))
{
foreach($cs in (Get-ConfigConnectionStringBuilders.ps1 $config))
{
$name = $cs.Name + '.' + ($config |Split-Path -LeafBase |Split-Path -Extension).Trim('.')
if($connections -and $name -in $connections.profileName){Write-Verbose "A '$name' connection already exists."; continue}
Import-Variables.ps1 $cs.ConnectionString
$vsconn = @{
ProfileName = $name
ServerInstance = ${Data Source}
Database = ${Data Source}
}
if(!${Integrated Security}) {$vsconn += @{UserName=${User ID}}}
Write-Verbose "Found '$($vsconn.ProfileName)' in $config"
$connections += [pscustomobject]$vsconn
}
}
Write-Verbose "Found $($connections.Count) connection strings."
$connections
}
$connections = Get-ConfigConnections
if(!$connections){Write-Verbose 'Nothing to do.'}
else{$connections |Add-VsCodeDatabaseConnection.ps1}