Skip to content

Commit b83e8a5

Browse files
authored
New-DbaConnectionStringBuilder, param reorganization (#9613)
1 parent 7346448 commit b83e8a5

File tree

2 files changed

+123
-34
lines changed

2 files changed

+123
-34
lines changed

public/New-DbaConnectionStringBuilder.ps1

+34-15
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ function New-DbaConnectionStringBuilder {
4444
4545
.PARAMETER Legacy
4646
Use this switch to create a connection string using System.Data.SqlClient instead of Microsoft.Data.SqlClient.
47+
48+
.PARAMETER EnableException
49+
By default, when something goes wrong we try to catch it, interpret it and give you a friendly warning message.
50+
This avoids overwhelming you with "sea of red" exceptions, but is inconvenient because it basically disables advanced scripting.
51+
Using this switch turns this "nice by default" feature off and enables you to catch exceptions with your own try/catch.
4752
4853
.NOTES
4954
Tags: SqlBuild, ConnectionString, Connection
@@ -89,21 +94,19 @@ function New-DbaConnectionStringBuilder {
8994
[string]$ColumnEncryptionSetting,
9095
[switch]$Legacy,
9196
[switch]$NonPooledConnection,
92-
[string]$WorkstationId = $env:COMPUTERNAME
97+
[string]$WorkstationID = $env:COMPUTERNAME,
98+
[switch]$EnableException
9399
)
94100
process {
95101
$pooling = (-not $NonPooledConnection)
96102
if ($SqlCredential -and ($Username -or $Password)) {
97-
Stop-Function -Message "You can only specify SQL Credential or Username/Password, not both."
103+
Stop-Function -Message "You can only specify SQL Credential or Username/Password, not both." -EnableException $EnableException
98104
return
99105
}
100106
if ($SqlCredential) {
101107
$UserName = $SqlCredential.UserName
102108
$Password = $SqlCredential.GetNetworkCredential().Password
103109
}
104-
if (-not $UserName) {
105-
$PSBoundParameters.IntegratedSecurity = $true
106-
}
107110

108111
foreach ($cs in $ConnectionString) {
109112
if ($Legacy) {
@@ -112,34 +115,50 @@ function New-DbaConnectionStringBuilder {
112115
$builder = New-Object Microsoft.Data.SqlClient.SqlConnectionStringBuilder $cs
113116
}
114117

115-
if ($builder.ApplicationName -in "Framework Microsoft SqlClient Data Provider", ".Net SqlClient Data Provider") {
118+
if (!$builder.ShouldSerialize('Application Name')) {
116119
$builder['Application Name'] = $ApplicationName
117120
}
118-
if ($PSBoundParameters.DataSource) {
121+
if (Test-Bound -ParameterName DataSource) {
119122
$builder['Data Source'] = $DataSource
120123
}
121-
if ($PSBoundParameters.InitialCatalog) {
124+
if (Test-Bound -ParameterName InitialCatalog) {
122125
$builder['Initial Catalog'] = $InitialCatalog
123126
}
124-
if ($PSBoundParameters.IntegratedSecurity) {
125-
$builder['Integrated Security'] = $PSBoundParameters.IntegratedSecurity
127+
if (Test-Bound -ParameterName IntegratedSecurity) {
128+
if ($IntegratedSecurity) {
129+
$builder['Integrated Security'] = $true
130+
} else {
131+
$builder['Integrated Security'] = $false
132+
}
126133
}
127134
if ($UserName) {
128135
$builder["User ID"] = $UserName
136+
} elseif (!$IntegratedSecurity) {
137+
$builder['Integrated Security'] = $false
129138
}
130139
if ($Password) {
131140
$builder['Password'] = $Password
132141
}
133-
if ($WorkstationId) {
134-
$builder['Workstation ID'] = $WorkstationId
142+
if (!$builder.ShouldSerialize('Workstation ID')) {
143+
$builder['Workstation ID'] = $WorkstationID
144+
}
145+
if (Test-Bound -ParameterName WorkstationID) {
146+
$builder['Workstation ID'] = $WorkstationID
135147
}
136-
if ($MultipleActiveResultSets -eq $true) {
137-
$builder['MultipleActiveResultSets'] = $true
148+
if (Test-Bound -ParameterName MultipleActiveResultSets) {
149+
if ($MultipleActiveResultSets) {
150+
$builder['MultipleActiveResultSets'] = $true
151+
} else {
152+
$builder['MultipleActiveResultSets'] = $false
153+
}
138154
}
139155
if ($ColumnEncryptionSetting -eq "Enabled") {
140156
$builder['Column Encryption Setting'] = "Enabled"
141157
}
142-
if ($pooling) {
158+
if (-not($builder.ShouldSerialize('Pooling'))) {
159+
$builder['Pooling'] = $pooling
160+
}
161+
if (Test-Bound -ParameterName NonPooledConnection) {
143162
$builder['Pooling'] = $pooling
144163
}
145164
$builder

tests/New-DbaConnectionStringBuilder.Tests.ps1

+89-19
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ $global:TestConfig = Get-TestConfig
55
Describe "$CommandName Unit Tests" -Tag 'UnitTests' {
66
Context "Validate parameters" {
77
[object[]]$params = (Get-Command $CommandName).Parameters.Keys | Where-Object {$_ -notin ('whatif', 'confirm')}
8-
[object[]]$knownParameters = 'ConnectionString', 'ApplicationName', 'DataSource', 'InitialCatalog', 'IntegratedSecurity', 'UserName', 'Password', 'MultipleActiveResultSets', 'ColumnEncryptionSetting', 'WorkstationId', 'Legacy', 'SqlCredential', 'NonPooledConnection'
8+
[object[]]$knownParameters = 'ConnectionString', 'ApplicationName', 'DataSource', 'InitialCatalog', 'IntegratedSecurity', 'UserName', 'Password', 'MultipleActiveResultSets', 'ColumnEncryptionSetting', 'WorkstationId', 'Legacy', 'SqlCredential', 'NonPooledConnection', 'EnableException'
99
$knownParameters += [System.Management.Automation.PSCmdlet]::CommonParameters
1010
It "Should only contain our specific parameters" {
11-
(@(Compare-Object -ReferenceObject ($knownParameters | Where-Object {$_}) -DifferenceObject $params).Count ) | Should Be 0
11+
(@(Compare-Object -ReferenceObject ($knownParameters | Where-Object {$_}) -DifferenceObject $params).Count ) | Should -Be 0
1212
}
1313
}
1414
}
@@ -17,28 +17,28 @@ Describe "$CommandName Integration Tests" -Tags "IntegrationTests" {
1717
Context "Get a ConnectionStringBuilder and assert its values" {
1818
$results = New-DbaConnectionStringBuilder "Data Source=localhost,1433;Initial Catalog=AlwaysEncryptedSample;UID=sa;PWD=alwaysB3Encrypt1ng;Column Encryption Setting=enabled"
1919
It "Should be a connection string builder" {
20-
$results.GetType() | Should Be Microsoft.Data.SqlClient.SqlConnectionStringBuilder
20+
$results.GetType() | Should -Be Microsoft.Data.SqlClient.SqlConnectionStringBuilder
2121
}
2222
It "Should enable Always Encrypted" {
23-
$results.ColumnEncryptionSetting | Should Be Enabled
23+
$results.ColumnEncryptionSetting | Should -Be Enabled
2424
}
2525
It "Should have a user name of sa" {
26-
$results.UserID | Should Be "sa"
26+
$results.UserID | Should -Be "sa"
2727
}
2828
It "Should have an Application name of `"dbatools Powershell Module`"" {
29-
$results.ApplicationName | Should Be "dbatools Powershell Module"
29+
$results.ApplicationName | Should -Be "dbatools Powershell Module"
3030
}
3131
It "Should have an Workstation ID of `"${env:COMPUTERNAME}`"" {
32-
$results.WorkstationID | Should Be $env:COMPUTERNAME
32+
$results.WorkstationID | Should -Be $env:COMPUTERNAME
3333
}
3434
It "Should have a null MultipeActiveRcordSets" {
35-
$results.MultipeActiveRcordSets | Should Be $null
35+
$results.MultipeActiveRcordSets | Should -Be $null
3636
}
3737
}
3838
Context "Assert that the default Application name is preserved" {
3939
$results = New-DbaConnectionStringBuilder "Data Source=localhost,1433;Initial Catalog=AlwaysEncryptedSample;UID=sa;PWD=alwaysB3Encrypt1ng;Application Name=Always Encrypted MvcString;Column Encryption Setting=enabled"
4040
It "Should have the Application name of `"Always Encrypted MvcString`"" {
41-
$results.ApplicationName | Should Be "Always Encrypted MvcString"
41+
$results.ApplicationName | Should -Be "Always Encrypted MvcString"
4242
}
4343
}
4444
Context "Build a ConnectionStringBuilder by parameters" {
@@ -48,47 +48,117 @@ Describe "$CommandName Integration Tests" -Tags "IntegrationTests" {
4848
-UserName "sa" `
4949
-Password "alwaysB3Encrypt1ng"
5050
It "Should be a connection string builder" {
51-
$results.GetType() | Should Be Microsoft.Data.SqlClient.SqlConnectionStringBuilder
51+
$results.GetType() | Should -Be Microsoft.Data.SqlClient.SqlConnectionStringBuilder
5252
}
5353
It "Should have a user name of sa" {
54-
$results.UserID | Should Be "sa"
54+
$results.UserID | Should -Be "sa"
5555
}
5656
It "Should have a password of alwaysB3Encrypt1ng" {
57-
$results.Password | Should Be "alwaysB3Encrypt1ng"
57+
$results.Password | Should -Be "alwaysB3Encrypt1ng"
5858
}
5959
It "Should have a WorkstationID of {$env:COMPUTERNAME}" {
60-
$results.WorkstationID | Should Be $env:COMPUTERNAME
60+
$results.WorkstationID | Should -Be $env:COMPUTERNAME
6161
}
6262
It "Should have an Application name of `"dbatools Powershell Module`"" {
63-
$results.ApplicationName | Should Be "dbatools Powershell Module"
63+
$results.ApplicationName | Should -Be "dbatools Powershell Module"
6464
}
6565
It "Should have an Workstation ID of `"${env:COMPUTERNAME}`"" {
66-
$results.WorkstationID | Should Be ${env:COMPUTERNAME}
66+
$results.WorkstationID | Should -Be ${env:COMPUTERNAME}
67+
}
68+
It "Should have an InitialCatalog of `AlwaysEncryptedSample`"" {
69+
$results.InitialCatalog | Should -Be 'AlwaysEncryptedSample'
6770
}
6871
}
6972
Context "Explicitly set MARS to false" {
7073
$results = New-DbaConnectionStringBuilder `
7174
-MultipleActiveResultSets:$false
7275
It "Should not enable Multipe Active Record Sets" {
73-
$results.MultipleActiveResultSets | Should Be $false
76+
$results.MultipleActiveResultSets | Should -Be $false
7477
}
7578
}
7679
Context "Set MARS via alias" {
7780
$results = New-DbaConnectionStringBuilder -MARS
7881
It "Should have a MultipeActiveResultSets value of true" {
79-
$results.MultipleActiveResultSets | Should Be $true
82+
$results.MultipleActiveResultSets | Should -Be $true
8083
}
8184
}
8285
Context "Set AlwaysEncrypted" {
8386
$results = New-DbaConnectionStringBuilder -AlwaysEncrypted "Enabled"
8487
It "Should have a `"Column Encryption Setting`" value of `"Enabled`"" {
85-
$results.ColumnEncryptionSetting | Should Be 'Enabled'
88+
$results.ColumnEncryptionSetting | Should -Be 'Enabled'
8689
}
8790
}
8891
Context "Set IntegratedSecurity" {
8992
$results = New-DbaConnectionStringBuilder -IntegratedSecurity
9093
It "Should have a `"Integrated Security Setting`" value of `"True`"" {
91-
$results.IntegratedSecurity | Should Be $True
94+
$results.IntegratedSecurity | Should -Be $True
95+
}
96+
$results = New-DbaConnectionStringBuilder -IntegratedSecurity:$false
97+
It "Should have a `"Integrated Security Setting`" value of `"False`"" {
98+
$results.IntegratedSecurity | Should -Be $false
99+
}
100+
}
101+
102+
Context "Can still return legacy builder" {
103+
$results = New-DbaConnectionStringBuilder -Legacy
104+
It "Should be a connection string builder" {
105+
$results.GetType() | Should -Be System.Data.SqlClient.SqlConnectionStringBuilder
106+
}
107+
}
108+
109+
Context "Can use a SQL Credential" {
110+
$securePassword = ConvertTo-SecureString 'somepass' -AsPlainText -Force
111+
$cred = New-Object System.Management.Automation.PSCredential ('somelogin', $securePassword)
112+
$results = New-DbaConnectionStringBuilder -SqlCredential $cred
113+
It "Should have a user name of somelogin" {
114+
$results.UserID | Should -Be 'somelogin'
115+
}
116+
It "Should have a password of somepass" {
117+
$results.Password | Should -Be 'somepass'
118+
}
119+
}
120+
121+
Context "Errors out for multiple 'credentials' passed in" {
122+
$securePassword = ConvertTo-SecureString 'somepass' -AsPlainText -Force
123+
$cred = New-Object System.Management.Automation.PSCredential ('somelogin', $securePassword)
124+
125+
It "Should throw an error" {
126+
{ New-DbaConnectionStringBuilder -Username 'test' -SqlCredential $cred -EnableException } | Should -Throw "You can only specify SQL Credential or Username/Password, not both"
127+
}
128+
}
129+
130+
Context "Overrides (see #9606)" {
131+
Context "Workstation ID" {
132+
$results = New-DbaConnectionStringBuilder "Data Source=localhost,1433;Workstation ID=mycomputer"
133+
It "Shouldn't override WorkstationId unless specified" {
134+
$results.WorkstationID | Should -Be 'mycomputer'
135+
}
136+
$results = New-DbaConnectionStringBuilder "Data Source=localhost,1433;Workstation ID=mycomputer" -WorkstationID "another"
137+
It "Overrides WorkstationId when specified" {
138+
$results.WorkstationID | Should -Be 'another'
139+
}
140+
}
141+
142+
Context "Integrated Security" {
143+
$results = New-DbaConnectionStringBuilder "Data Source=localhost,1433;Integrated Security=False"
144+
It "Shouldn't override unless specified" {
145+
$results.IntegratedSecurity | Should -Be $False
146+
}
147+
$results = New-DbaConnectionStringBuilder "Data Source=localhost,1433;Integrated Security=False" -IntegratedSecurity
148+
It "Overrides Integrated Security when specified" {
149+
$results.IntegratedSecurity | Should -Be $True
150+
}
151+
}
152+
153+
Context "Pooling" {
154+
$results = New-DbaConnectionStringBuilder "Data Source=localhost,1433;Pooling=False"
155+
It "Shouldn't override Pooling unless specified" {
156+
$results.Pooling | Should -Be $False
157+
}
158+
$results = New-DbaConnectionStringBuilder "Data Source=localhost,1433;Pooling=True" -NonPooledConnection
159+
It "Overrides Pooling when specified" {
160+
$results.Pooling | Should -Be $False
161+
}
92162
}
93163
}
94164
}

0 commit comments

Comments
 (0)