Skip to content

Commit d296853

Browse files
authored
SqlServerDsc: Add public command Get-SqlDscConfigurationOption (#1935)
- SqlServerDsc - Added new public command: - `Get-SqlDscConfigurationOption` - Returns the available configuration options that can be used with the DSC resource _SqlConfiguration_.
1 parent 7d58bf5 commit d296853

File tree

8 files changed

+348
-4
lines changed

8 files changed

+348
-4
lines changed

.vscode/settings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@
7070
"GetxPDTVariable",
7171
"Dbcc",
7272
"creplace",
73-
"dbatools"
73+
"dbatools",
74+
"fastbuild"
7475
],
7576
"cSpell.ignorePaths": [
7677
".git"

CHANGELOG.md

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

66
## [Unreleased]
77

8+
### Added
9+
10+
- SqlServerDsc
11+
- Added a new build task `fastbuild` that can be used during development
12+
process when there are no need to generate documentation.
13+
- Added new public command:
14+
- `Get-SqlDscConfigurationOption` - Returns the available configuration
15+
options that can be used with the DSC resource _SqlConfiguration_.
16+
817
### Changed
918

1019
- SqlServerDsc

build.yaml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,16 @@ BuildWorkflow:
1515
- Generate_Conceptual_Help
1616
- Generate_Wiki_Content
1717

18+
fastbuild:
19+
- Clean
20+
- Build_Module_ModuleBuilder
21+
- Build_NestedModules_ModuleBuilder
22+
1823
pack:
1924
- build
20-
- package_module_nupkg
25+
- package_module_nupkg # cSpell: disable-line
2126

22-
hqrmtest:
27+
hqrmtest: # cSpell: disable-line
2328
- Invoke_HQRM_Tests_Stop_On_Fail
2429

2530
test:
@@ -146,7 +151,7 @@ Resolve-Dependency:
146151
GitHubConfig:
147152
GitHubFilesToAdd:
148153
- 'CHANGELOG.md'
149-
GitHubConfigUserName: dscbot
154+
GitHubConfigUserName: dscbot # cSpell: disable-line
150155
GitHubConfigUserEmail: [email protected]
151156
UpdateChangelogOnPrerelease: false
152157

source/DSCResources/DSC_SqlConfiguration/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@
33
The `SqlConfiguration` DSC resource manages the [SQL Server Configuration Options](https://msdn.microsoft.com/en-us/library/ms189631.aspx)
44
on a SQL Server instance.
55

6+
To list the available configuration option names run:
7+
8+
```powershell
9+
$serverObject = Connect-SqlDscDatabaseEngine -InstanceName 'SQL2022'
10+
$serverObject | Get-SqlDscConfigurationOption | ft
11+
```
12+
613
## Requirements
714

815
* Target machine must be running Windows Server 2012 or later.
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<#
2+
.SYNOPSIS
3+
Get server configuration option.
4+
5+
.DESCRIPTION
6+
This command gets the available configuration options from a SQL Server Database Engine instance.
7+
8+
.PARAMETER ServerObject
9+
Specifies current server connection object.
10+
11+
.PARAMETER Name
12+
Specifies the name of the configuration option to get.
13+
14+
.PARAMETER Refresh
15+
Specifies that the **ServerObject**'s configuration property should be
16+
refreshed before trying get the available configuration options. This is
17+
helpful when run values or configuration values have been modified outside
18+
of the specified **ServerObject**.
19+
20+
.EXAMPLE
21+
$serverObject = Connect-SqlDscDatabaseEngine -InstanceName 'MyInstance'
22+
$sqlServerObject | Get-SqlDscConfigurationOption
23+
24+
Get all the available configuration options.
25+
26+
.EXAMPLE
27+
$serverObject = Connect-SqlDscDatabaseEngine -InstanceName 'MyInstance'
28+
$sqlServerObject | Get-SqlDscConfigurationOption -Name '*threshold*'
29+
30+
Get the configuration options that contains the word **threshold**.
31+
32+
.OUTPUTS
33+
`[Microsoft.SqlServer.Management.Smo.ConfigProperty[]]`
34+
#>
35+
function Get-SqlDscConfigurationOption
36+
{
37+
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseOutputTypeCorrectly', '', Justification = 'Because the rule does not understands that the command returns [System.String[]] when using , (comma) in the return statement')]
38+
[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.')]
39+
[OutputType([Microsoft.SqlServer.Management.Smo.ConfigProperty[]])]
40+
[CmdletBinding()]
41+
param
42+
(
43+
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
44+
[Microsoft.SqlServer.Management.Smo.Server]
45+
$ServerObject,
46+
47+
[Parameter()]
48+
[System.String]
49+
$Name,
50+
51+
[Parameter()]
52+
[System.Management.Automation.SwitchParameter]
53+
$Refresh
54+
)
55+
56+
process
57+
{
58+
if ($Refresh.IsPresent)
59+
{
60+
# Make sure the configuration option values are up-to-date.
61+
$serverObject.Configuration.Refresh()
62+
}
63+
64+
if ($PSBoundParameters.ContainsKey('Name'))
65+
{
66+
$configurationOption = $serverObject.Configuration.Properties |
67+
Where-Object -FilterScript {
68+
$_.DisplayName -like $Name
69+
}
70+
71+
if (-not $configurationOption)
72+
{
73+
$missingConfigurationOptionMessage = $script:localizedData.ConfigurationOption_Get_Missing -f $Name
74+
75+
$writeErrorParameters = @{
76+
Message = $missingConfigurationOptionMessage
77+
Category = 'InvalidOperation'
78+
ErrorId = 'GSDCO0001' # cspell: disable-line
79+
TargetObject = $Name
80+
}
81+
82+
Write-Error @writeErrorParameters
83+
}
84+
}
85+
else
86+
{
87+
$configurationOption = $serverObject.Configuration.Properties.ForEach({ $_ })
88+
}
89+
90+
return , [Microsoft.SqlServer.Management.Smo.ConfigProperty[]] (
91+
$configurationOption |
92+
Sort-Object -Property 'DisplayName'
93+
)
94+
}
95+
}

source/en-US/SqlServerDsc.strings.psd1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,4 +178,7 @@ ConvertFrom-StringData @'
178178
179179
## Get-FileVersionInformation
180180
FileVersionInformation_Get_FilePathIsNotFile = The specified path is not a file.
181+
182+
## Get-SqlDscConfigurationOption
183+
ConfigurationOption_Get_Missing = There is no configuration option with the name '{0}'.
181184
'@
Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '')]
2+
param ()
3+
4+
BeforeDiscovery {
5+
try
6+
{
7+
if (-not (Get-Module -Name 'DscResource.Test'))
8+
{
9+
# Assumes dependencies has been resolved, so if this module is not available, run 'noop' task.
10+
if (-not (Get-Module -Name 'DscResource.Test' -ListAvailable))
11+
{
12+
# Redirect all streams to $null, except the error stream (stream 2)
13+
& "$PSScriptRoot/../../build.ps1" -Tasks 'noop' 2>&1 4>&1 5>&1 6>&1 > $null
14+
}
15+
16+
# If the dependencies has not been resolved, this will throw an error.
17+
Import-Module -Name 'DscResource.Test' -Force -ErrorAction 'Stop'
18+
}
19+
}
20+
catch [System.IO.FileNotFoundException]
21+
{
22+
throw 'DscResource.Test module dependency not found. Please run ".\build.ps1 -ResolveDependency -Tasks build" first.'
23+
}
24+
}
25+
26+
BeforeAll {
27+
$script:dscModuleName = 'SqlServerDsc'
28+
29+
$env:SqlServerDscCI = $true
30+
31+
Import-Module -Name $script:dscModuleName
32+
33+
# Loading mocked classes
34+
Add-Type -Path (Join-Path -Path (Join-Path -Path $PSScriptRoot -ChildPath '../Stubs') -ChildPath 'SMO.cs')
35+
36+
$PSDefaultParameterValues['InModuleScope:ModuleName'] = $script:dscModuleName
37+
$PSDefaultParameterValues['Mock:ModuleName'] = $script:dscModuleName
38+
$PSDefaultParameterValues['Should:ModuleName'] = $script:dscModuleName
39+
}
40+
41+
AfterAll {
42+
$PSDefaultParameterValues.Remove('InModuleScope:ModuleName')
43+
$PSDefaultParameterValues.Remove('Mock:ModuleName')
44+
$PSDefaultParameterValues.Remove('Should:ModuleName')
45+
46+
# Unload the module being tested so that it doesn't impact any other tests.
47+
Get-Module -Name $script:dscModuleName -All | Remove-Module -Force
48+
49+
Remove-Item -Path 'env:SqlServerDscCI'
50+
}
51+
52+
Describe 'Get-SqlDscConfigurationOption' -Tag 'Public' {
53+
It 'Should have the correct parameters in parameter set <MockParameterSetName>' -ForEach @(
54+
@{
55+
MockParameterSetName = '__AllParameterSets'
56+
MockExpectedParameters = '[-ServerObject] <Server> [[-Name] <string>] [-Refresh] [<CommonParameters>]'
57+
}
58+
) {
59+
$result = (Get-Command -Name 'Get-SqlDscConfigurationOption').ParameterSets |
60+
Where-Object -FilterScript {
61+
$_.Name -eq $mockParameterSetName
62+
} |
63+
Select-Object -Property @(
64+
@{
65+
Name = 'ParameterSetName'
66+
Expression = { $_.Name }
67+
},
68+
@{
69+
Name = 'ParameterListAsString'
70+
Expression = { $_.ToString() }
71+
}
72+
)
73+
74+
$result.ParameterSetName | Should -Be $MockParameterSetName
75+
$result.ParameterListAsString | Should -Be $MockExpectedParameters
76+
}
77+
78+
Context 'When the specified configuration option exist' {
79+
BeforeAll {
80+
$mockServerObject = New-Object -TypeName 'Microsoft.SqlServer.Management.Smo.Server' |
81+
Add-Member -MemberType 'ScriptProperty' -Name 'Configuration' -Value {
82+
return @{
83+
Properties = @()
84+
}
85+
} -PassThru -Force
86+
87+
$mockDefaultParameters = @{
88+
ServerObject = $mockServerObject
89+
Name = 'Unknown Option Name'
90+
}
91+
}
92+
93+
Context 'When specifying to throw on error' {
94+
BeforeAll {
95+
$mockErrorMessage = InModuleScope -ScriptBlock {
96+
$script:localizedData.ConfigurationOption_Get_Missing
97+
}
98+
}
99+
100+
It 'Should throw the correct error' {
101+
{ Get-SqlDscConfigurationOption @mockDefaultParameters -ErrorAction 'Stop' } |
102+
Should -Throw -ExpectedMessage ($mockErrorMessage -f 'Unknown Option Name')
103+
}
104+
}
105+
106+
Context 'When ignoring the error' {
107+
It 'Should not throw an exception and return $null' {
108+
Get-SqlDscConfigurationOption @mockDefaultParameters -ErrorAction 'SilentlyContinue' |
109+
Should -BeNullOrEmpty
110+
}
111+
}
112+
}
113+
114+
Context 'When getting a specific configuration option' {
115+
BeforeAll {
116+
$mockServerObject = New-Object -TypeName 'Microsoft.SqlServer.Management.Smo.Server' |
117+
Add-Member -MemberType 'ScriptProperty' -Name 'Configuration' -Value {
118+
$configOption1 = [Microsoft.SqlServer.Management.Smo.ConfigProperty]::CreateTypeInstance()
119+
$configOption1.DisplayName = 'blocked process threshold (s)'
120+
121+
return @{
122+
Properties = @($configOption1)
123+
}
124+
} -PassThru -Force
125+
}
126+
127+
It 'Should return the correct values' {
128+
$mockDefaultParameters = @{
129+
ServerObject = $mockServerObject
130+
Name = 'blocked process threshold (s)'
131+
}
132+
133+
$result = Get-SqlDscConfigurationOption @mockDefaultParameters
134+
135+
$result | Should -BeOfType 'Microsoft.SqlServer.Management.Smo.ConfigProperty'
136+
137+
$result.DisplayName | Should -Be 'blocked process threshold (s)'
138+
}
139+
140+
Context 'When passing parameter ServerObject over the pipeline' {
141+
It 'Should return the correct values' {
142+
$result = $mockServerObject | Get-SqlDscConfigurationOption -Name 'blocked process threshold (s)'
143+
144+
$result | Should -BeOfType 'Microsoft.SqlServer.Management.Smo.ConfigProperty'
145+
146+
$result.DisplayName | Should -Be 'blocked process threshold (s)'
147+
}
148+
}
149+
}
150+
151+
Context 'When getting all available configuration options' {
152+
BeforeAll {
153+
$mockServerObject = New-Object -TypeName 'Microsoft.SqlServer.Management.Smo.Server' |
154+
Add-Member -MemberType 'ScriptProperty' -Name 'Configuration' -Value {
155+
$configOption1 = [Microsoft.SqlServer.Management.Smo.ConfigProperty]::CreateTypeInstance()
156+
$configOption1.DisplayName = 'blocked process threshold (s)'
157+
158+
$configOption2 = [Microsoft.SqlServer.Management.Smo.ConfigProperty]::CreateTypeInstance()
159+
$configOption2.DisplayName = 'show advanced options'
160+
161+
return @{
162+
Properties = @($configOption1, $configOption2)
163+
}
164+
} -PassThru -Force
165+
}
166+
167+
It 'Should return the correct values' {
168+
$result = Get-SqlDscConfigurationOption -ServerObject $mockServerObject
169+
170+
$result | Should -BeOfType 'Microsoft.SqlServer.Management.Smo.ConfigProperty'
171+
172+
$result.DisplayName | Should -Contain 'show advanced options'
173+
$result.DisplayName | Should -Contain 'blocked process threshold (s)'
174+
}
175+
176+
Context 'When passing parameter ServerObject over the pipeline' {
177+
It 'Should return the correct values' {
178+
$result = $mockServerObject | Get-SqlDscConfigurationOption
179+
180+
$result | Should -BeOfType 'Microsoft.SqlServer.Management.Smo.ConfigProperty'
181+
182+
$result.DisplayName | Should -Contain 'show advanced options'
183+
$result.DisplayName | Should -Contain 'blocked process threshold (s)'
184+
}
185+
}
186+
}
187+
}

0 commit comments

Comments
 (0)