Skip to content

Commit 1440fce

Browse files
authored
Add new public commands (#2108)
1 parent 26ebcc9 commit 1440fce

16 files changed

+2042
-9
lines changed

.github/instructions/dsc-community-style-guidelines-powershell.instructions.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ applyTo: "**/*.ps?(m|d)1"
8282
- `$PSCmdlet.ShouldProcess` must use required pattern
8383
- Inside `$PSCmdlet.ShouldProcess`-block, avoid using `Write-Verbose`
8484
- Never use backtick as line continuation in production code.
85-
- Set `$ErrorActionPreference = 'Stop'` before commands using `-ErrorAction 'Stop'`; restore previous value after
85+
- Set `$ErrorActionPreference = 'Stop'` before commands using `-ErrorAction 'Stop'`; restore previous value directly after invocation (do not use try-catch-finally)
8686

8787
## Output streams
8888

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
118118
- Added private function `Get-CommandParameter` to filter command parameters
119119
by excluding specified parameter names and common parameters, providing a
120120
reusable way to determine settable properties on objects.
121+
- `Get-SqlDscServerProtocolName`
122+
- New public command for SQL Server protocol name mappings with support
123+
for protocol name, display name, and short name parameter sets.
124+
- `Get-SqlDscManagedComputerInstance`
125+
- New public command for retrieving SQL Server managed computer instance
126+
information with pipeline support.
127+
- `Get-SqlDscServerProtocol`
128+
- Enhanced to support multiple parameter sets including pipeline input
129+
from managed computer and instance objects.
130+
- Enhanced to optionally return all protocols when ProtocolName parameter
131+
is not specified.
121132

122133
### Changed
123134

azure-pipelines.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,9 @@ stages:
296296
'tests/Integration/Commands/Get-SqlDscConfigurationOption.Integration.Tests.ps1'
297297
'tests/Integration/Commands/Set-SqlDscConfigurationOption.Integration.Tests.ps1'
298298
'tests/Integration/Commands/Test-SqlDscConfigurationOption.Integration.Tests.ps1'
299+
'tests/Integration/Commands/Get-SqlDscManagedComputerInstance.Integration.Tests.ps1'
300+
'tests/Integration/Commands/Get-SqlDscServerProtocolName.Integration.Tests.ps1'
301+
'tests/Integration/Commands/Get-SqlDscServerProtocol.Integration.Tests.ps1'
299302
'tests/Integration/Commands/Disable-SqlDscLogin.Integration.Tests.ps1'
300303
'tests/Integration/Commands/Enable-SqlDscLogin.Integration.Tests.ps1'
301304
'tests/Integration/Commands/Test-SqlDscIsLoginEnabled.Integration.Tests.ps1'

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1980,6 +1980,9 @@ function Find-ExceptionByNumber
19801980
ServerProtocol. The property DisplayName could potentially be localized
19811981
while the property Name must be exactly like it is returned by the
19821982
class ServerProtocol, with the correct casing.
1983+
1984+
The Get-ProtocolNameProperties function is deprecated and should be removed
1985+
in the future when existing code has moved to new public commands.
19831986
#>
19841987
function Get-ProtocolNameProperties
19851988
{
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
<#
2+
.SYNOPSIS
3+
Returns managed computer server instance information.
4+
5+
.DESCRIPTION
6+
Returns managed computer server instance information for a SQL Server instance
7+
using SMO (SQL Server Management Objects). The command can retrieve a specific
8+
instance or all instances on a managed computer.
9+
10+
.PARAMETER ServerName
11+
Specifies the name of the server where the SQL Server instance is running.
12+
Defaults to the local computer name.
13+
14+
.PARAMETER InstanceName
15+
Specifies the name of the SQL Server instance to retrieve.
16+
If not specified, all instances are returned.
17+
18+
.PARAMETER ManagedComputerObject
19+
Specifies a managed computer object from which to retrieve server instances.
20+
This parameter accepts pipeline input from Get-SqlDscManagedComputer.
21+
22+
.EXAMPLE
23+
Get-SqlDscManagedComputerInstance -InstanceName 'MSSQLSERVER'
24+
25+
Returns the default SQL Server instance information from the local computer.
26+
27+
.EXAMPLE
28+
Get-SqlDscManagedComputerInstance -ServerName 'MyServer' -InstanceName 'MyInstance'
29+
30+
Returns the MyInstance SQL Server instance information from the MyServer computer.
31+
32+
.EXAMPLE
33+
Get-SqlDscManagedComputerInstance -ServerName 'MyServer'
34+
35+
Returns all SQL Server instances information from the MyServer computer.
36+
37+
.EXAMPLE
38+
Get-SqlDscManagedComputer -ServerName 'MyServer' | Get-SqlDscManagedComputerInstance -InstanceName 'MyInstance'
39+
40+
Uses pipeline input to retrieve a specific instance from a managed computer object.
41+
42+
.INPUTS
43+
Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer
44+
45+
A managed computer object can be piped to this command.
46+
47+
.OUTPUTS
48+
Microsoft.SqlServer.Management.Smo.Wmi.ServerInstance
49+
50+
Returns server instance objects from SMO (SQL Server Management Objects).
51+
52+
.NOTES
53+
This command uses SMO (SQL Server Management Objects) to retrieve server
54+
instance information from the specified managed computer.
55+
#>
56+
function Get-SqlDscManagedComputerInstance
57+
{
58+
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('UseSyntacticallyCorrectExamples', '', Justification = 'Because the rule does not yet support parsing the code when a parameter type is not available. The ScriptAnalyzer rule UseSyntacticallyCorrectExamples will always error in the editor due to https://github.com/indented-automation/Indented.ScriptAnalyzerRules/issues/8.')]
59+
[CmdletBinding(DefaultParameterSetName = 'ByServerName')]
60+
[OutputType([Microsoft.SqlServer.Management.Smo.Wmi.ServerInstance])]
61+
param
62+
(
63+
[Parameter(ParameterSetName = 'ByServerName')]
64+
[ValidateNotNullOrEmpty()]
65+
[System.String]
66+
$ServerName = (Get-ComputerName),
67+
68+
[Parameter(ParameterSetName = 'ByServerName')]
69+
[Parameter(ParameterSetName = 'ByManagedComputerObject')]
70+
[ValidateNotNullOrEmpty()]
71+
[System.String]
72+
$InstanceName,
73+
74+
[Parameter(Mandatory = $true, ValueFromPipeline = $true, ParameterSetName = 'ByManagedComputerObject')]
75+
[Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer]
76+
$ManagedComputerObject
77+
)
78+
79+
process
80+
{
81+
if ($PSCmdlet.ParameterSetName -eq 'ByServerName')
82+
{
83+
Write-Verbose -Message (
84+
$script:localizedData.ManagedComputerInstance_GetFromServer -f $ServerName
85+
)
86+
87+
$ManagedComputerObject = Get-SqlDscManagedComputer -ServerName $ServerName
88+
}
89+
else
90+
{
91+
Write-Verbose -Message (
92+
$script:localizedData.ManagedComputerInstance_GetFromObject
93+
)
94+
}
95+
96+
if ($PSBoundParameters.ContainsKey('InstanceName'))
97+
{
98+
Write-Verbose -Message (
99+
$script:localizedData.ManagedComputerInstance_GetSpecificInstance -f $InstanceName
100+
)
101+
102+
$serverInstance = $ManagedComputerObject.ServerInstances[$InstanceName]
103+
104+
if (-not $serverInstance)
105+
{
106+
$errorMessage = $script:localizedData.ManagedComputerInstance_InstanceNotFound -f $InstanceName, $ManagedComputerObject.Name
107+
$errorRecord = [System.Management.Automation.ErrorRecord]::new(
108+
[System.InvalidOperationException]::new($errorMessage),
109+
'SqlServerInstanceNotFound',
110+
[System.Management.Automation.ErrorCategory]::ObjectNotFound,
111+
$InstanceName
112+
)
113+
$PSCmdlet.ThrowTerminatingError($errorRecord)
114+
}
115+
116+
return $serverInstance
117+
}
118+
else
119+
{
120+
Write-Verbose -Message (
121+
$script:localizedData.ManagedComputerInstance_GetAllInstances
122+
)
123+
124+
return $ManagedComputerObject.ServerInstances
125+
}
126+
}
127+
}
Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
<#
2+
.SYNOPSIS
3+
Returns server protocol information for a SQL Server instance.
4+
5+
.DESCRIPTION
6+
Returns server protocol information for a SQL Server instance using
7+
SMO (SQL Server Management Objects). The command supports getting
8+
information for TcpIp, NamedPipes, and SharedMemory protocols.
9+
If no protocol is specified, all protocols are returned.
10+
11+
.PARAMETER ServerName
12+
Specifies the name of the server where the SQL Server instance is running.
13+
Defaults to the local computer name.
14+
15+
.PARAMETER InstanceName
16+
Specifies the name of the SQL Server instance for which to return protocol
17+
information.
18+
19+
.PARAMETER ProtocolName
20+
Specifies the name of the network protocol to return information for.
21+
Valid values are 'TcpIp', 'NamedPipes', and 'SharedMemory'.
22+
If not specified, all protocols are returned.
23+
24+
.PARAMETER ManagedComputerObject
25+
Specifies a managed computer object from which to retrieve server protocol
26+
information. This parameter accepts pipeline input from Get-SqlDscManagedComputer.
27+
28+
.PARAMETER ManagedComputerInstanceObject
29+
Specifies a managed computer instance object from which to retrieve server
30+
protocol information. This parameter accepts pipeline input from
31+
Get-SqlDscManagedComputerInstance.
32+
33+
.EXAMPLE
34+
Get-SqlDscServerProtocol -InstanceName 'MSSQLSERVER' -ProtocolName 'TcpIp'
35+
36+
Returns TcpIp protocol information for the default SQL Server instance
37+
on the local computer.
38+
39+
.EXAMPLE
40+
Get-SqlDscServerProtocol -ServerName 'MyServer' -InstanceName 'MyInstance' -ProtocolName 'NamedPipes'
41+
42+
Returns NamedPipes protocol information for the MyInstance SQL Server
43+
instance on the MyServer computer.
44+
45+
.EXAMPLE
46+
Get-SqlDscServerProtocol -InstanceName 'MSSQLSERVER'
47+
48+
Returns all protocol information for the default SQL Server instance
49+
on the local computer.
50+
51+
.EXAMPLE
52+
Get-SqlDscManagedComputer -ServerName 'MyServer' | Get-SqlDscServerProtocol -InstanceName 'MyInstance'
53+
54+
Uses pipeline input from Get-SqlDscManagedComputer to retrieve all protocols
55+
for the specified instance.
56+
57+
.EXAMPLE
58+
Get-SqlDscManagedComputerInstance -InstanceName 'MyInstance' | Get-SqlDscServerProtocol -ProtocolName 'TcpIp'
59+
60+
Uses pipeline input from Get-SqlDscManagedComputerInstance to retrieve TcpIp
61+
protocol information.
62+
63+
.INPUTS
64+
Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer
65+
66+
A managed computer object can be piped to this command.
67+
68+
Microsoft.SqlServer.Management.Smo.Wmi.ServerInstance
69+
70+
A server instance object can be piped to this command.
71+
72+
.OUTPUTS
73+
Microsoft.SqlServer.Management.Smo.Wmi.ServerProtocol
74+
75+
Returns server protocol objects from SMO (SQL Server Management Objects).
76+
#>
77+
function Get-SqlDscServerProtocol
78+
{
79+
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('UseSyntacticallyCorrectExamples', '', Justification = 'Because the rule does not yet support parsing the code when a parameter type is not available. The ScriptAnalyzer rule UseSyntacticallyCorrectExamples will always error in the editor due to https://github.com/indented-automation/Indented.ScriptAnalyzerRules/issues/8.')]
80+
[CmdletBinding(DefaultParameterSetName = 'ByServerName')]
81+
[OutputType([Microsoft.SqlServer.Management.Smo.Wmi.ServerProtocol])]
82+
param
83+
(
84+
[Parameter(ParameterSetName = 'ByServerName')]
85+
[ValidateNotNullOrEmpty()]
86+
[System.String]
87+
$ServerName,
88+
89+
[Parameter(Mandatory = $true, ParameterSetName = 'ByServerName')]
90+
[Parameter(Mandatory = $true, ParameterSetName = 'ByManagedComputerObject')]
91+
[ValidateNotNullOrEmpty()]
92+
[System.String]
93+
$InstanceName,
94+
95+
[Parameter(ParameterSetName = 'ByServerName')]
96+
[Parameter(ParameterSetName = 'ByManagedComputerObject')]
97+
[Parameter(ParameterSetName = 'ByManagedComputerInstanceObject')]
98+
[ValidateSet('TcpIp', 'NamedPipes', 'SharedMemory')]
99+
[System.String]
100+
$ProtocolName,
101+
102+
[Parameter(Mandatory = $true, ValueFromPipeline = $true, ParameterSetName = 'ByManagedComputerObject')]
103+
[Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer]
104+
$ManagedComputerObject,
105+
106+
[Parameter(Mandatory = $true, ValueFromPipeline = $true, ParameterSetName = 'ByManagedComputerInstanceObject')]
107+
[Microsoft.SqlServer.Management.Smo.Wmi.ServerInstance]
108+
$ManagedComputerInstanceObject
109+
)
110+
111+
process
112+
{
113+
$previousErrorActionPreference = $ErrorActionPreference
114+
$ErrorActionPreference = 'Stop'
115+
116+
# Set default value for ServerName if not provided
117+
if (-not $PSBoundParameters.ContainsKey('ServerName'))
118+
{
119+
$ServerName = Get-ComputerName -ErrorAction 'Stop'
120+
}
121+
122+
if ($PSBoundParameters.ContainsKey('ProtocolName'))
123+
{
124+
Write-Verbose -Message (
125+
$script:localizedData.ServerProtocol_GetState -f $ProtocolName, $InstanceName, $ServerName
126+
)
127+
}
128+
else
129+
{
130+
Write-Verbose -Message (
131+
$script:localizedData.ServerProtocol_GetAllProtocols -f $InstanceName, $ServerName
132+
)
133+
}
134+
135+
switch ($PSCmdlet.ParameterSetName)
136+
{
137+
'ByServerName'
138+
{
139+
$serverInstance = Get-SqlDscManagedComputerInstance -ServerName $ServerName -InstanceName $InstanceName -ErrorAction 'Stop'
140+
}
141+
142+
'ByManagedComputerObject'
143+
{
144+
$serverInstance = $ManagedComputerObject | Get-SqlDscManagedComputerInstance -InstanceName $InstanceName -ErrorAction 'Stop'
145+
}
146+
147+
'ByManagedComputerInstanceObject'
148+
{
149+
$serverInstance = $ManagedComputerInstanceObject
150+
}
151+
}
152+
153+
if ($PSBoundParameters.ContainsKey('ProtocolName'))
154+
{
155+
# Get specific protocol
156+
$protocolMapping = Get-SqlDscServerProtocolName -ProtocolName $ProtocolName -ErrorAction 'Stop'
157+
158+
$serverProtocolObject = $serverInstance.ServerProtocols[$protocolMapping.ShortName]
159+
160+
$ErrorActionPreference = $previousErrorActionPreference
161+
162+
if (-not $serverProtocolObject)
163+
{
164+
$errorMessage = $script:localizedData.ServerProtocol_ProtocolNotFound -f $protocolMapping.DisplayName, $InstanceName, $serverInstance.Parent.Name
165+
$errorRecord = [System.Management.Automation.ErrorRecord]::new(
166+
[System.InvalidOperationException]::new($errorMessage),
167+
'SqlServerProtocolNotFound',
168+
[System.Management.Automation.ErrorCategory]::ObjectNotFound,
169+
$ProtocolName
170+
)
171+
$PSCmdlet.ThrowTerminatingError($errorRecord)
172+
}
173+
174+
return $serverProtocolObject
175+
}
176+
else
177+
{
178+
# Get all protocols
179+
$allProtocolMappings = Get-SqlDscServerProtocolName -All -ErrorAction 'Stop'
180+
$allServerProtocols = @()
181+
182+
foreach ($protocolMapping in $allProtocolMappings)
183+
{
184+
$serverProtocolObject = $serverInstance.ServerProtocols[$protocolMapping.ShortName]
185+
186+
if ($serverProtocolObject)
187+
{
188+
$allServerProtocols += $serverProtocolObject
189+
}
190+
}
191+
192+
$ErrorActionPreference = $previousErrorActionPreference
193+
194+
if ($allServerProtocols.Count -eq 0)
195+
{
196+
return $null
197+
}
198+
199+
return $allServerProtocols
200+
}
201+
}
202+
}

0 commit comments

Comments
 (0)