Skip to content

Commit 352435f

Browse files
authored
SqlServerBase: Support connection protocol and port (#2382)
1 parent 82bd423 commit 352435f

18 files changed

+1213
-30
lines changed

CHANGELOG.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,22 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
### Added
99

10+
- Added public command `Get-SqlDscServerProtocolTcpIp` to retrieve TCP/IP address
11+
group information for SQL Server instances. Returns `ServerIPAddress` objects
12+
containing port configuration including `TcpPort`, `TcpDynamicPorts`, `Enabled`,
13+
and `Active` properties. Supports filtering by specific IP address group name
14+
and accepts pipeline input from `Get-SqlDscServerProtocol`.
15+
- `SqlResourceBase`
16+
- Added `Protocol` property to specify the network protocol (`tcp`, `np`, `lpc`)
17+
when connecting to SQL Server instances
18+
([issue #2041](https://github.com/dsccommunity/SqlServerDsc/issues/2041)).
19+
- Added `Port` property to specify the TCP port number when connecting to SQL
20+
Server instances
21+
([issue #2041](https://github.com/dsccommunity/SqlServerDsc/issues/2041)).
22+
- `Connect-SqlDscDatabaseEngine`
23+
- Added `Protocol` parameter to specify the network protocol when connecting.
24+
- Added `Port` parameter to specify the TCP port number when connecting.
25+
Connection strings now support the format `[protocol:]hostname[\instance][,port]`.
1026
- `Install-SqlDscServer`
1127
- Added parameter `AllowDqRemoval` to the `Upgrade` parameter set
1228
([issue #2155](https://github.com/dsccommunity/SqlServerDsc/issues/2155)).

azure-pipelines.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,7 @@ stages:
321321
'tests/Integration/Commands/Get-SqlDscServerProtocolName.Integration.Tests.ps1'
322322
'tests/Integration/Commands/ConvertTo-SqlDscEditionName.Integration.Tests.ps1'
323323
'tests/Integration/Commands/Get-SqlDscServerProtocol.Integration.Tests.ps1'
324+
'tests/Integration/Commands/Get-SqlDscServerProtocolTcpIp.Integration.Tests.ps1'
324325
'tests/Integration/Commands/Get-SqlDscTraceFlag.Integration.Tests.ps1'
325326
'tests/Integration/Commands/Disable-SqlDscLogin.Integration.Tests.ps1'
326327
'tests/Integration/Commands/Enable-SqlDscLogin.Integration.Tests.ps1'

source/Classes/011.SqlResourceBase.ps1

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,29 @@
1717
If parameter **Credential'* is not provided then the resource instance is
1818
run using the credential that runs the configuration.
1919
20+
.PARAMETER Protocol
21+
Specifies the network protocol to use when connecting to the _SQL Server_
22+
instance. Valid values are `'tcp'` for TCP/IP, `'np'` for Named Pipes,
23+
and `'lpc'` for Shared Memory.
24+
25+
If not specified, the connection will use the default protocol order
26+
configured on the client.
27+
28+
.PARAMETER Port
29+
Specifies the TCP port number to use when connecting to the _SQL Server_
30+
instance. This parameter is only applicable when connecting via TCP/IP.
31+
32+
If not specified for a named instance, the SQL Server Browser service
33+
will be used to determine the port. For default instances, port 1433
34+
is used by default.
35+
2036
.PARAMETER Reasons
2137
Returns the reason a property is not in desired state.
38+
39+
.NOTES
40+
The protocol values (`'tcp'`, `'np'`, `'lpc'`) are lowercase to match
41+
the SQL Server connection string prefix format, e.g.,
42+
`tcp:ServerName\Instance,Port`.
2243
#>
2344
class SqlResourceBase : ResourceBase
2445
{
@@ -42,6 +63,15 @@ class SqlResourceBase : ResourceBase
4263
[PSCredential]
4364
$Credential
4465

66+
[DscProperty()]
67+
[ValidateSet('tcp', 'np', 'lpc')]
68+
[System.String]
69+
$Protocol
70+
71+
[DscProperty()]
72+
[Nullable[System.UInt16]]
73+
$Port
74+
4575
[DscProperty(NotConfigurable)]
4676
[SqlReason[]]
4777
$Reasons
@@ -50,6 +80,18 @@ class SqlResourceBase : ResourceBase
5080
SqlResourceBase () : base ($PSScriptRoot)
5181
{
5282
$this.SqlServerObject = $null
83+
84+
<#
85+
These connection properties will not be enforced. Child classes
86+
should use += to append their own properties to this list.
87+
#>
88+
$this.ExcludeDscProperties = @(
89+
'ServerName'
90+
'InstanceName'
91+
'Credential'
92+
'Protocol'
93+
'Port'
94+
)
5395
}
5496

5597
<#
@@ -75,6 +117,16 @@ class SqlResourceBase : ResourceBase
75117
$connectSqlDscDatabaseEngineParameters.Credential = $this.Credential
76118
}
77119

120+
if ($this.Protocol)
121+
{
122+
$connectSqlDscDatabaseEngineParameters.Protocol = $this.Protocol
123+
}
124+
125+
if ($this.Port)
126+
{
127+
$connectSqlDscDatabaseEngineParameters.Port = $this.Port
128+
}
129+
78130
$this.SqlServerObject = Connect-SqlDscDatabaseEngine @connectSqlDscDatabaseEngineParameters
79131
}
80132

source/Classes/020.SqlAgentAlert.ps1

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,8 @@ class SqlAgentAlert : SqlResourceBase
140140

141141
SqlAgentAlert () : base ()
142142
{
143-
# Property names that cannot be enforced
144-
$this.ExcludeDscProperties = @(
145-
'InstanceName',
146-
'ServerName',
147-
'Credential',
143+
# Append to the properties set in SqlResourceBase that will not be enforced.
144+
$this.ExcludeDscProperties += @(
148145
'Name'
149146
)
150147
}

source/Classes/020.SqlAudit.ps1

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -191,12 +191,9 @@ class SqlAudit : SqlResourceBase
191191

192192
SqlAudit () : base ()
193193
{
194-
# These properties will not be enforced.
195-
$this.ExcludeDscProperties = @(
196-
'ServerName'
197-
'InstanceName'
194+
# Append to the properties set in SqlResourceBase that will not be enforced.
195+
$this.ExcludeDscProperties += @(
198196
'Name'
199-
'Credential'
200197
'Force'
201198
)
202199
}

source/Classes/020.SqlDatabasePermission.ps1

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -163,13 +163,10 @@ class SqlDatabasePermission : SqlResourceBase
163163

164164
SqlDatabasePermission() : base ()
165165
{
166-
# These properties will not be enforced.
167-
$this.ExcludeDscProperties = @(
168-
'ServerName'
169-
'InstanceName'
166+
# Append to the properties set in SqlResourceBase that will not be enforced.
167+
$this.ExcludeDscProperties += @(
170168
'DatabaseName'
171169
'Name'
172-
'Credential'
173170
)
174171
}
175172

source/Classes/020.SqlPermission.ps1

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -164,12 +164,9 @@ class SqlPermission : SqlResourceBase
164164

165165
SqlPermission() : base ()
166166
{
167-
# These properties will not be enforced.
168-
$this.ExcludeDscProperties = @(
169-
'ServerName'
170-
'InstanceName'
167+
# Append to the properties set in SqlResourceBase that will not be enforced.
168+
$this.ExcludeDscProperties += @(
171169
'Name'
172-
'Credential'
173170
)
174171
}
175172

source/Modules/SqlServerDsc.Common/SqlServerDsc.Common.psm1

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,15 @@ function Connect-SQL
368368
[System.String]
369369
$LoginType = 'WindowsUser',
370370

371+
[Parameter()]
372+
[ValidateSet('tcp', 'np', 'lpc')]
373+
[System.String]
374+
$Protocol,
375+
376+
[Parameter()]
377+
[System.UInt16]
378+
$Port,
379+
371380
[Parameter()]
372381
[ValidateNotNull()]
373382
[System.Int32]
@@ -380,6 +389,18 @@ function Connect-SQL
380389

381390
Import-SqlDscPreferredModule
382391

392+
<#
393+
Build the connection string in the format: [protocol:]hostname[\instance][,port]
394+
Examples:
395+
- ServerName (default instance, no protocol/port)
396+
- ServerName\Instance (named instance)
397+
- tcp:ServerName (default instance with protocol)
398+
- tcp:ServerName\Instance (named instance with protocol)
399+
- ServerName,1433 (default instance with port)
400+
- ServerName\Instance,50200 (named instance with port)
401+
- tcp:ServerName,1433 (default instance with protocol and port)
402+
- tcp:ServerName\Instance,50200 (named instance with protocol and port)
403+
#>
383404
if ($InstanceName -eq 'MSSQLSERVER')
384405
{
385406
$databaseEngineInstance = $ServerName
@@ -389,6 +410,18 @@ function Connect-SQL
389410
$databaseEngineInstance = '{0}\{1}' -f $ServerName, $InstanceName
390411
}
391412

413+
# Append port if specified
414+
if ($PSBoundParameters.ContainsKey('Port'))
415+
{
416+
$databaseEngineInstance = '{0},{1}' -f $databaseEngineInstance, $Port
417+
}
418+
419+
# Prepend protocol if specified
420+
if ($PSBoundParameters.ContainsKey('Protocol'))
421+
{
422+
$databaseEngineInstance = '{0}:{1}' -f $Protocol, $databaseEngineInstance
423+
}
424+
392425
$sqlServerObject = New-Object -TypeName 'Microsoft.SqlServer.Management.Smo.Server'
393426
$sqlConnectionContext = $sqlServerObject.ConnectionContext
394427
$sqlConnectionContext.ServerInstance = $databaseEngineInstance

source/Public/Connect-SqlDscDatabaseEngine.ps1

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,23 @@
2828
If set to 'SqlLogin' then it will impersonate using the native SQL
2929
login specified in the parameter Credential.
3030
31+
.PARAMETER Protocol
32+
Specifies the network protocol to use when connecting to the SQL Server
33+
instance. Valid values are 'tcp' for TCP/IP, 'np' for Named Pipes,
34+
and 'lpc' for Shared Memory.
35+
36+
If not specified, the connection will use the default protocol order
37+
configured on the client.
38+
39+
.PARAMETER Port
40+
Specifies the TCP port number to use when connecting to the SQL Server
41+
instance. This parameter is only applicable when connecting via TCP/IP
42+
(Protocol = 'tcp'). Valid values are 1-65535.
43+
44+
If not specified for a named instance, the SQL Server Browser service
45+
will be used to determine the port. For default instances, port 1433
46+
is used by default.
47+
3148
.PARAMETER StatementTimeout
3249
Set the query StatementTimeout in seconds. Default 600 seconds (10 minutes).
3350
@@ -59,13 +76,29 @@
5976
6077
Connects to the default instance on the local server using the SQL login 'sa'.
6178
79+
.EXAMPLE
80+
Connect-SqlDscDatabaseEngine -ServerName '192.168.1.1' -InstanceName 'MyInstance' -Protocol 'tcp' -Port 50200
81+
82+
Connects to the named instance 'MyInstance' on server '192.168.1.1' using
83+
TCP/IP on port 50200. The connection string format is 'tcp:192.168.1.1\MyInstance,50200'.
84+
85+
.EXAMPLE
86+
Connect-SqlDscDatabaseEngine -ServerName '192.168.1.1' -Protocol 'tcp' -Port 1433
87+
88+
Connects to the default instance on server '192.168.1.1' using TCP/IP on
89+
port 1433. The connection string format is 'tcp:192.168.1.1,1433'.
90+
6291
.INPUTS
6392
None.
6493
6594
.OUTPUTS
6695
`Microsoft.SqlServer.Management.Smo.Server`
6796
6897
Returns the SQL Server server object.
98+
99+
.NOTES
100+
The protocol values ('tcp', 'np', 'lpc') are lowercase to match the SQL
101+
Server connection string prefix format, e.g., 'tcp:ServerName\Instance,Port'.
69102
#>
70103
function Connect-SqlDscDatabaseEngine
71104
{
@@ -97,6 +130,16 @@ function Connect-SqlDscDatabaseEngine
97130
[System.String]
98131
$LoginType = 'WindowsUser',
99132

133+
[Parameter()]
134+
[ValidateSet('tcp', 'np', 'lpc')]
135+
[System.String]
136+
$Protocol,
137+
138+
[Parameter()]
139+
[ValidateRange(1, 65535)]
140+
[System.UInt16]
141+
$Port,
142+
100143
[Parameter()]
101144
[ValidateNotNull()]
102145
[System.Int32]

0 commit comments

Comments
 (0)