forked from dsccommunity/SqlServerDsc
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path020.SqlAgentAlert.ps1
More file actions
285 lines (227 loc) · 9.63 KB
/
020.SqlAgentAlert.ps1
File metadata and controls
285 lines (227 loc) · 9.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
<#
.SYNOPSIS
The `SqlAgentAlert` DSC resource is used to create, modify, or remove
_SQL Server Agent_ alerts.
.DESCRIPTION
The `SqlAgentAlert` DSC resource is used to create, modify, or remove
_SQL Server Agent_ alerts.
The built-in parameter **PSDscRunAsCredential** can be used to run the resource
as another user. The resource will then authenticate to the _SQL Server_
instance as that user. It also possible to instead use impersonation by the
parameter **Credential**.
## Requirements
* Target machine must be running Windows Server 2012 or later.
* Target machine must be running SQL Server Database Engine 2012 or later.
* Target machine must have access to the SQLPS PowerShell module or the SqlServer
PowerShell module.
## Known issues
All issues are not listed here, see [here for all open issues](https://github.com/dsccommunity/SqlServerDsc/issues?q=is%3Aissue+is%3Aopen+in%3Atitle+SqlAgentAlert).
### Property **Reasons** does not work with **PSDscRunAsCredential**
When using the built-in parameter **PSDscRunAsCredential** the read-only
property **Reasons** will return empty values for the properties **Code**
and **Phrase**. The built-in property **PSDscRunAsCredential** does not work
together with class-based resources that using advanced type like the parameter
**Reasons** have.
### Using **Credential** property
SQL Authentication and Group Managed Service Accounts is not supported as
impersonation credentials. Currently only Windows Integrated Security is
supported to use as credentials.
For Windows Authentication the username must either be provided with the User
Principal Name (UPN), e.g. `username@domain.local` or if using non-domain
(for example a local Windows Server account) account the username must be
provided without the NetBIOS name, e.g. `username`. Using the NetBIOS name, e.g
using the format `DOMAIN\username` will not work.
See more information in [Credential Overview](https://github.com/dsccommunity/SqlServerDsc/wiki/CredentialOverview).
.PARAMETER Name
The name of the _SQL Server Agent_ alert.
.PARAMETER Ensure
Specifies if the _SQL Server Agent_ alert should be present or absent.
Default value is `'Present'`.
.PARAMETER Severity
The severity of the _SQL Server Agent_ alert. Valid range is 0 to 25.
Cannot be used together with **MessageId**.
.PARAMETER MessageId
The message id of the _SQL Server Agent_ alert. Valid range is 0 to 2147483647.
Cannot be used together with **Severity**.
.EXAMPLE
Invoke-DscResource -ModuleName SqlServerDsc -Name SqlAgentAlert -Method Get -Property @{
InstanceName = 'MSSQLSERVER'
Name = 'Alert1'
}
This example shows how to get the current state of the _SQL Server Agent_
alert named **Alert1**.
.EXAMPLE
Invoke-DscResource -ModuleName SqlServerDsc -Name SqlAgentAlert -Method Test -Property @{
InstanceName = 'MSSQLSERVER'
Name = 'Alert1'
Ensure = 'Present'
Severity = 16
}
This example shows how to test if the _SQL Server Agent_ alert named
**Alert1** is in the desired state.
.EXAMPLE
Invoke-DscResource -ModuleName SqlServerDsc -Name SqlAgentAlert -Method Set -Property @{
InstanceName = 'MSSQLSERVER'
Name = 'Alert1'
Ensure = 'Present'
Severity = 16
}
This example shows how to set the desired state for the _SQL Server Agent_
alert named **Alert1** with severity level 16.
.EXAMPLE
Invoke-DscResource -ModuleName SqlServerDsc -Name SqlAgentAlert -Method Set -Property @{
InstanceName = 'MSSQLSERVER'
Name = 'Alert1'
Ensure = 'Present'
MessageId = 50001
}
This example shows how to set the desired state for the _SQL Server Agent_
alert named **Alert1** with message ID 50001.
.EXAMPLE
Invoke-DscResource -ModuleName SqlServerDsc -Name SqlAgentAlert -Method Set -Property @{
InstanceName = 'MSSQLSERVER'
Name = 'Alert1'
Ensure = 'Absent'
}
This example shows how to remove the _SQL Server Agent_ alert named
**Alert1**.
#>
[DscResource(RunAsCredential = 'Optional')]
class SqlAgentAlert : SqlResourceBase
{
[DscProperty(Key)]
[System.String]
$Name
[DscProperty()]
[ValidateSet('Present', 'Absent')]
[System.String]
$Ensure = 'Present'
[DscProperty()]
[ValidateRange(0, 25)]
[Nullable[System.Int32]]
$Severity
[DscProperty()]
[ValidateRange(0, 2147483647)]
[Nullable[System.Int32]]
$MessageId
SqlAgentAlert () : base ()
{
# Property names that cannot be enforced
$this.ExcludeDscProperties = @(
'InstanceName',
'ServerName',
'Credential'
)
}
[SqlAgentAlert] Get()
{
# Call base implementation to get current state
$currentState = ([ResourceBase] $this).Get()
return $currentState
}
[System.Boolean] Test()
{
# Call base implementation to test current state
$inDesiredState = ([ResourceBase] $this).Test()
return $inDesiredState
}
[void] Set()
{
# Call base implementation to set desired state
([ResourceBase] $this).Set()
}
hidden [void] AssertProperties([System.Collections.Hashtable] $properties)
{
# Validate that both Severity and MessageId are not specified
Assert-BoundParameter -BoundParameterList $properties -MutuallyExclusiveList1 @('Severity') -MutuallyExclusiveList2 @('MessageId')
}
hidden [System.Collections.Hashtable] GetCurrentState([System.Collections.Hashtable] $properties)
{
$serverObject = $this.GetServerObject()
Write-Verbose -Message ($this.localizedData.SqlAgentAlert_GettingCurrentState -f $this.Name, $this.InstanceName)
$currentState = @{
InstanceName = $this.InstanceName
ServerName = $this.ServerName
Name = $this.Name
Ensure = 'Absent'
}
$alertObject = $serverObject | Get-SqlDscAgentAlert -Name $this.Name -ErrorAction 'SilentlyContinue'
if ($alertObject)
{
Write-Verbose -Message ($this.localizedData.SqlAgentAlert_AlertExists -f $this.Name)
$currentState.Ensure = 'Present'
# Get the current severity and message ID
if ($alertObject.Severity -gt 0)
{
$currentState.Severity = $alertObject.Severity
}
if ($alertObject.MessageId -gt 0)
{
$currentState.MessageId = $alertObject.MessageId
}
}
else
{
Write-Verbose -Message ($this.localizedData.SqlAgentAlert_AlertDoesNotExist -f $this.Name)
}
return $currentState
}
hidden [void] Modify([System.Collections.Hashtable] $properties)
{
$serverObject = $this.GetServerObject()
if ($this.Ensure -eq 'Present')
{
$alertObject = $serverObject | Get-SqlDscAgentAlert -Name $this.Name -ErrorAction 'SilentlyContinue'
if ($null -eq $alertObject)
{
Write-Verbose -Message ($this.localizedData.SqlAgentAlert_CreatingAlert -f $this.Name)
$newAlertParameters = @{
ServerObject = $serverObject
Name = $this.Name
ErrorAction = 'Stop'
}
if ($properties.ContainsKey('Severity'))
{
$newAlertParameters.Severity = $properties.Severity
}
if ($properties.ContainsKey('MessageId'))
{
$newAlertParameters.MessageId = $properties.MessageId
}
$null = New-SqlDscAgentAlert @newAlertParameters
}
else
{
Write-Verbose -Message ($this.localizedData.SqlAgentAlert_UpdatingAlert -f $this.Name)
$setAlertParameters = @{
AlertObject = $alertObject
ErrorAction = 'Stop'
}
$needsUpdate = $false
if ($properties.ContainsKey('Severity') -and $alertObject.Severity -ne $properties.Severity)
{
$setAlertParameters.Severity = $properties.Severity
$needsUpdate = $true
}
if ($properties.ContainsKey('MessageId') -and $alertObject.MessageId -ne $properties.MessageId)
{
$setAlertParameters.MessageId = $properties.MessageId
$needsUpdate = $true
}
if ($needsUpdate)
{
$null = Set-SqlDscAgentAlert @setAlertParameters
}
else
{
Write-Verbose -Message ($this.localizedData.SqlAgentAlert_NoChangesNeeded -f $this.Name)
}
}
}
else # Ensure = 'Absent'
{
Write-Verbose -Message ($this.localizedData.SqlAgentAlert_RemovingAlert -f $this.Name)
$null = $serverObject | Remove-SqlDscAgentAlert -Name $this.Name -Force -ErrorAction 'Stop'
}
}
}