forked from stcu/SharedScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Add-VsCodeDatabaseConnection.ps1
77 lines (71 loc) · 2.36 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
<#
.SYNOPSIS
Adds a VS Code MSSQL database connection to the repo.
.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
}