-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathAdd-VsCodeDatabaseConnection.ps1
91 lines (82 loc) · 2.77 KB
/
Add-VsCodeDatabaseConnection.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
<#
.SYNOPSIS
Adds a VS Code MSSQL database connection to the repo.
.DESCRIPTION
The VSCode MSSQL extension can use saved database connections to connect to for queries,
and this allows adding those to the VSCode settings in the current git repo.
.INPUTS
Any object with these properties, used to construct a database connection entry:
* ProfileName or Name
* ServerInstance or Server or DataSource
* Database or InitialCatalog
* UserName or UID
.FUNCTIONALITY
VSCode
.LINK
https://marketplace.visualstudio.com/items?itemName=ms-mssql.mssql
.LINK
Get-VSCodeSetting.ps1
.LINK
Set-VSCodeSetting.ps1
.EXAMPLE
Add-VsCodeDatabaseConnection.ps1 ConnectionName ServerName\instance DatabaseName
Adds an MSSQL extension trusted connection named ConnectionName that
connects to the server ServerName\instance and database DatabaseName.
#>
#Requires -Version 3
[CmdletBinding()][OutputType([void])] Param(
# The name of the connection.
[Parameter(Position=0,Mandatory=$true,ValueFromPipelineByPropertyName=$true)]
[Alias('Name')][string] $ProfileName,
# The name of a server (and optional instance) to connect and use for the query.
[Parameter(Position=1,Mandatory=$true,ValueFromPipelineByPropertyName=$true)]
[Alias('Server','DataSource')][string] $ServerInstance,
# The the database to connect to on the server.
[Parameter(Position=2,Mandatory=$true,ValueFromPipelineByPropertyName=$true)]
[Alias('InitialCatalog')][string] $Database,
<#
The username to connect with. No password will be stored.
If no username is given, a trusted connection will be created.
#>
[Parameter(Position=3,ValueFromPipelineByPropertyName=$true)]
[Alias('UID')][string] $UserName,
# Overwrite an existing profile with the same name.
[switch] $Force
)
Begin
{
[psobject[]]$connections = Get-VSCodeSetting.ps1 /mssql.connections -Workspace
if(!$connections) {[psobject[]]$connections = @()}
}
Process
{
if($connections |Where-Object profileName -eq $ProfileName)
{
if($Force) {$connections = $connections |Where-Object profileName -ne $ProfileName}
else {Write-Verbose "Connection '$ProfileName' already exists"; return}
}
$connections +=
if($UserName)
{[pscustomobject]@{
server = $ServerInstance
database = $Database
authenticationType = 'SqlLogin'
profileName = $ProfileName
password = ''
user = $UserName
savePassword = $false
}}
else
{[pscustomobject]@{
server = $ServerInstance
database = $Database
authenticationType = 'Integrated'
profileName = $ProfileName
password = ''
}}
}
End
{
$connections |ConvertTo-Json -Compress |Write-Verbose
Set-VSCodeSetting.ps1 /mssql.connections $connections -Workspace
}