Skip to content

Commit 6488425

Browse files
authored
Add new public commands for managing SQL Agent Alerts (#2149)
1 parent 8eaf645 commit 6488425

31 files changed

+3160
-31
lines changed

.github/instructions/SqlServerDsc-guidelines.instructions.md

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ applyTo: "**"
77

88
## Naming
99
- Public commands: `{Verb}-SqlDsc{Noun}` format
10+
- Private function: `{Verb}-{Noun}` format
1011

1112
## Resources
1213
- Database Engine resources: inherit `SqlResourceBase`
@@ -24,8 +25,15 @@ applyTo: "**"
2425

2526
## Test Requirements
2627
- Unit tests: Add `$env:SqlServerDscCI = $true` in `BeforeAll`, remove in `AfterAll`
27-
- Integration tests: Use `Disconnect-SqlDscDatabaseEngine` after `Connect-SqlDscDatabaseEngine`
28-
- Test config: tests/Integration/Commands/README.md and tests/Integration/Resources/README.md
29-
- Integration test script files must be added to a group
30-
within the test stage in ./azure-pipelines.yml.
31-
- Choose the appropriate group number based on the required dependencies
28+
- Integration tests:
29+
- If requiring SQL Server DB, start the Windows service in `BeforeAll`, stop it in `AfterAll`.
30+
- Use `Connect-SqlDscDatabaseEngine` for SQL Server DB session, and always with correct CI credentials
31+
- Use `Disconnect-SqlDscDatabaseEngine` after `Connect-SqlDscDatabaseEngine`
32+
- Test config: tests/Integration/Commands/README.md and tests/Integration/Resources/README.md
33+
- Integration test script files must be added to a group within the test stage in ./azure-pipelines.yml.
34+
- Choose the appropriate group number based on the required dependencies
35+
36+
## Unit tests
37+
- When unit test uses SMO types, ensure they are properly stubbed in SMO.cs
38+
- Load stub types from SMO.cs in unit test files, e.g. `Add-Type -Path "$PSScriptRoot/../Stubs/SMO.cs"`
39+
- After changing SMO stub types, run tests in a new PowerShell session for changes to take effect.

.github/instructions/dsc-community-style-guidelines-class-resource.instructions.md

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,60 +8,73 @@ applyTo: "source/[cC]lasses/**/*.ps1"
88
**Applies to:** Classes with `[DscResource(...)]` decoration only.
99

1010
## Requirements
11-
- File: `source/Classes/{ResourceName}.ps1`
11+
- File: `source/Classes/020.{ResourceName}.ps1`
1212
- Decoration: `[DscResource(RunAsCredential = 'Optional')]` (replace with `'Mandatory'` if required)
1313
- Inheritance: Must inherit `ResourceBase` (part of module DscResource.Base)
1414
- `$this.localizedData` hashtable auto-populated by `ResourceBase` from localization file
1515

16+
## Required constructor
17+
18+
```powershell
19+
MyResourceName () : base ($PSScriptRoot)
20+
{
21+
# Property names where state cannot be enforced, e.g Ensure
22+
$this.ExcludeDscProperties = @()
23+
}
24+
```
25+
1626
## Required Method Pattern
1727

1828
```powershell
1929
[MyResourceName] Get()
2030
{
31+
# Call base implementation to get current state
2132
$currentState = ([ResourceBase] $this).Get()
2233
23-
# If needed, post-processing based on returned current state before returning to user
34+
# If needed, post-processing on current state that can not be handled by GetCurrentState()
2435
2536
return $currentState
2637
}
2738
2839
[System.Boolean] Test()
2940
{
41+
# Call base implementation to test current state
3042
$inDesiredState = ([ResourceBase] $this).Test()
3143
32-
# If needed, post-processing based on returned test result before returning to user
44+
# If needed, post-processing on test result that can not be handled by base Test()
3345
3446
return $inDesiredState
3547
}
3648
3749
[void] Set()
3850
{
51+
# Call base implementation to set desired state
3952
([ResourceBase] $this).Set()
4053
41-
# If needed, additional state changes that could not be handled by Modify()
54+
# If needed, additional state changes that can not be handled by Modify()
4255
}
4356
4457
hidden [System.Collections.Hashtable] GetCurrentState([System.Collections.Hashtable] $properties)
4558
{
46-
# Return current state as hashtable
47-
# Variable $properties contains the key properties (key-value pairs).
59+
# Always return current state as hashtable, $properties contains key properties
4860
}
4961
5062
hidden [void] Modify([System.Collections.Hashtable] $properties)
5163
{
52-
# Set desired state for non-compliant properties only
53-
# Variable $properties contains the properties (key-value pairs) that are not in desired state.
64+
# Always set desired state, $properties contain those that must change state
5465
}
66+
```
5567

68+
## Optional Method Pattern
69+
70+
```powershell
5671
hidden [void] AssertProperties([System.Collections.Hashtable] $properties)
5772
{
58-
# Validate user-provided properties
59-
# Variable $properties contains properties user assigned values.
73+
# Validate user-provided properties, $properties contains user assigned values
6074
}
6175
6276
hidden [void] NormalizeProperties([System.Collections.Hashtable] $properties)
6377
{
64-
# Normalize user-provided properties
65-
# Variable $properties contains properties user assigned values.
78+
# Normalize user-provided properties, $properties contains user assigned values
6679
}
6780
```

.github/instructions/dsc-community-style-guidelines-integration-tests.instructions.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ applyTo: "tests/[iI]ntegration/**/*.[iI]ntegration.[tT]ests.ps1"
1414
- Avoid `ExpectedMessage` for `Should -Throw` assertions
1515
- Only run integration tests in CI unless explicitly instructed.
1616
- Call commands with `-Force` parameter where applicable (avoids prompting).
17+
- Use `-ErrorAction Stop` on commands so failures surface immediately
1718

1819
## Required Setup Block
1920

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@ applyTo: "source/**/*.ps1"
1414
- Commands/functions: `source/en-US/SqlServerDsc.strings.psd1`
1515
- Class resources: `source/en-US/{ResourceClassName}.strings.psd1`
1616

17-
## Key Naming
18-
- Format: `FunctionName_Description` (underscore separators)
19-
- Example: `Get_SqlDscDatabase_ConnectingToDatabase`
17+
## Key Naming Patterns
18+
- Format: `Verb_FunctionName_Action` (underscore separators), e.g. `Get_SqlDscDatabase_ConnectingToDatabase`
2019

2120
## String Format
2221
```powershell

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ applyTo: "**/*.md"
1010
- Use '1.' for all items in ordered lists (1/1/1 numbering style)
1111
- Disable `MD013` rule by adding a comment for tables/code blocks exceeding 80 characters
1212
- Empty lines required before/after code blocks and headings (except before line 1)
13+
- Escape backslashes in file paths only (not in code blocks)
1314
- Code blocks must specify language identifiers
1415

1516
## Text Formatting

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ applyTo: "**/*.[Tt]ests.ps1"
1212
- One `Describe` block per file matching the tested entity name
1313
- Test code only inside `Describe` blocks
1414
- Assertions only in `It` blocks
15-
- Never test `Write-Verbose`, `Write-Debug`, or parameter binding behavior
15+
- Never test verbose messages, debug messages or parameter binding behavior
1616
- Pass all mandatory parameters to avoid prompts
1717

1818
## Structure & Scope
@@ -25,11 +25,15 @@ applyTo: "**/*.[Tt]ests.ps1"
2525

2626
## Syntax Rules
2727
- PascalCase: `Describe`, `Context`, `It`, `Should`, `BeforeAll`, `BeforeEach`, `AfterAll`, `AfterEach`
28-
- `It` descriptions start with 'Should'
2928
- `Context` descriptions start with 'When'
29+
- `It` descriptions start with 'Should', must not contain 'when'
3030
- Mock variables prefix: 'mock'
3131
- Prefer `-BeTrue`/`-BeFalse` over `-Be $true`/`-Be $false`
3232
- No `Should -Not -Throw` - invoke commands directly
33+
- Never add an empty `-MockWith` block
34+
- Omit `-MockWith` when returning `$null`
35+
- Set `$PSDefaultParameterValues` for `Mock:ModuleName`, `Should:ModuleName`, `InModuleScope:ModuleName`
36+
- Omit `-ModuleName` parameter on Pester commands
3337

3438
## File Organization
3539
- Class resources: `tests/Unit/Classes/{Name}.Tests.ps1`
@@ -42,7 +46,7 @@ applyTo: "**/*.[Tt]ests.ps1"
4246
- Keep scope close to usage context
4347

4448
## Best Practices
45-
- Assign unused return objects to `$null`
49+
- Inside `It` blocks, assign unused return objects to `$null` (unless part of pipeline)
4650
- Tested entity must be called from within the `It` blocks
4751
- Keep results and assertions in same `It` block
4852
- Cover all scenarios and code paths

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

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ applyTo: "**/*.ps?(m|d)1"
1515
- Classes: PascalCase
1616
- Include scope for script/global/environment variables: `$script:`, `$global:`, `$env:`
1717

18+
## File naming
19+
20+
- Class files: `###.ClassName.ps1` format (e.g. `001.SqlReason.ps1`, `004.StartupParameters.ps1`)
21+
1822
## Formatting
1923

2024
### Indentation & Spacing
@@ -71,8 +75,10 @@ applyTo: "**/*.ps?(m|d)1"
7175
- Avoid `Write-Output` (use `return` instead)
7276
- Avoid `ConvertTo-SecureString -AsPlainText` in production code
7377
- Don't redefine reserved parameters (Verbose, Debug, etc.)
74-
- Include a `Force` parameter for functions that support `$PSCmdlet.ShouldContinue` or that use `$PSCmdlet.ShouldProcess`
78+
- Include a `Force` parameter for functions that uses `$PSCmdlet.ShouldContinue` or `$PSCmdlet.ShouldProcess`
7579
- For state-changing functions, use `SupportsShouldProcess`
80+
- Place ShouldProcess check immediately before each state-change
81+
- `$PSCmdlet.ShouldProcess` must use required pattern
7682
- Use `$PSCmdlet.ThrowTerminatingError()` for terminating errors, use relevant error category
7783
- Use `Write-Error` for non-terminating errors, use relevant error category
7884
- Use `Write-Warning` for warnings
@@ -81,6 +87,32 @@ applyTo: "**/*.ps?(m|d)1"
8187
- Use `Write-Information` for informational messages.
8288
- Never use backtick as line continuation in production code.
8389

90+
## ShouldProcess Required Pattern
91+
92+
- Ensure `$descriptionMessage` explains what will happen
93+
- Ensure `$confirmationMessage` succinctly asks for confirmation
94+
- Keep `$captionMessage` short and descriptive (no trailing `.`)
95+
96+
```powershell
97+
$descriptionMessage = $script:localizedData.FunctionName_Action_ShouldProcessDescription -f $param1, $param2
98+
$confirmationMessage = $script:localizedData.FunctionName_Action_ShouldProcessConfirmation -f $param1
99+
$captionMessage = $script:localizedData.FunctionName_Action_ShouldProcessCaption
100+
101+
if ($PSCmdlet.ShouldProcess($descriptionMessage, $confirmationMessage, $captionMessage))
102+
{
103+
# state changing code
104+
}
105+
```
106+
107+
## Force Parameter Pattern
108+
109+
```powershell
110+
if ($Force.IsPresent -and -not $Confirm)
111+
{
112+
$ConfirmPreference = 'None'
113+
}
114+
```
115+
84116
### Structure
85117

86118
```powershell
@@ -107,7 +139,7 @@ applyTo: "**/*.ps?(m|d)1"
107139
function Get-Something
108140
{
109141
[CmdletBinding()]
110-
[OutputType([String])]
142+
[OutputType([System.String])]
111143
param
112144
(
113145
[Parameter(Mandatory = $true)]
@@ -128,7 +160,7 @@ function Get-Something
128160
- Include `[CmdletBinding()]` on every function
129161
- Parameter block at top
130162
- Parameter block: `param ()` if empty, else opening/closing parentheses on own lines
131-
- `[OutputType()]` for functions with output
163+
- `[OutputType({return type})]` for functions with output, no output use `[OutputType()]`
132164
- All parameters use `[Parameter()]` attribute, mandatory parameters use `[Parameter(Mandatory = $true)]`
133165
- Parameter attributes on separate lines
134166
- Parameter type on line above parameter name

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ applyTo: "**"
2525
## Requirements
2626
- Follow guidelines over existing code patterns
2727
- Always update CHANGELOG.md Unreleased section
28-
- Localize all strings using string keys
28+
- Localize all strings using string keys; remove any orphaned string keys
2929
- Check DscResource.Common before creating private functions
3030
- Separate reusable logic into private functions
3131
- Add unit tests for all commands/functions/resources

.vscode/settings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@
110110
"/@prkelly/g",
111111
"/@glennsarti/g",
112112
"/tempdb/g",
113-
"/sqlcmd\\.exe/g"
113+
"/\\b[A-Za-z]{2,}\\d{4}\\b/g"
114114
],
115115
"[markdown]": {
116116
"files.trimTrailingWhitespace": true,

CHANGELOG.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3434
Throws a terminating error if the specified principal does not exist as a login.
3535
- Supports pipeline input and provides detailed error messages with localization.
3636
- Uses `Test-SqlDscIsLogin` command for login validation following module patterns.
37-
- Added `Get-SqlDscLogin`, `Get-SqlDscRole`, `New-SqlDscLogin`, `New-SqlDscRole`, `Remove-SqlDscRole`, and `Remove-SqlDscLogin` commands for retrieving and managing SQL Server logins and roles with support for refresh, pipeline input, and ShouldProcess.
37+
- Added `Get-SqlDscLogin`, `Get-SqlDscRole`, `New-SqlDscLogin`, `New-SqlDscRole`,
38+
`Remove-SqlDscRole`, and `Remove-SqlDscLogin` commands for retrieving and managing
39+
SQL Server logins and roles with support for refresh, pipeline input, and ShouldProcess.
40+
- Added `Get-SqlDscAgentAlert`, `New-SqlDscAgentAlert`,
41+
`Set-SqlDscAgentAlert`, `Remove-SqlDscAgentAlert`, and `Test-SqlDscAgentAlert`
42+
to manage SQL Agent alerts on a Database Engine instance.
3843

3944
### Changed
4045

@@ -77,7 +82,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7782
- DSC community style guidelines
7883
- Added requirement to follow guidelines over existing code patterns.
7984
- Improved markdown, pester, powershell, and changelog instructions.
80-
- Fixed `Ingore` that seems in edge-cases fail.
85+
- Fixed `Ignore` that seems in edge-cases fail.
86+
- Improved markdown and changelog instructions.
8187

8288
## [17.1.0] - 2025-05-22
8389

0 commit comments

Comments
 (0)