forked from dsccommunity/SqlServerDsc
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSet-SqlDscAgentAlert.ps1
More file actions
232 lines (189 loc) · 9.12 KB
/
Set-SqlDscAgentAlert.ps1
File metadata and controls
232 lines (189 loc) · 9.12 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
<#
.SYNOPSIS
Updates a SQL Agent Alert.
.DESCRIPTION
This command updates an existing SQL Agent Alert on a SQL Server Database Engine
instance.
.PARAMETER ServerObject
Specifies current server connection object.
.PARAMETER AlertObject
Specifies an alert object to update.
.PARAMETER Name
Specifies the name of the SQL Agent Alert to update.
.PARAMETER Severity
Specifies the severity level for the SQL Agent Alert. Valid range is 0 to 25.
Cannot be used together with MessageId.
.PARAMETER MessageId
Specifies the message ID for the SQL Agent Alert. Valid range is 0 to 2147483647.
Cannot be used together with Severity.
.PARAMETER PassThru
If specified, the updated alert object will be returned.
.PARAMETER Refresh
Specifies that the alert object should be refreshed before updating. This
is helpful when alerts could have been modified outside of the **ServerObject**,
for example through T-SQL.
.PARAMETER Force
Specifies that the alert should be updated without prompting for confirmation.
.INPUTS
Microsoft.SqlServer.Management.Smo.Server
SQL Server Database Engine instance object.
Microsoft.SqlServer.Management.Smo.Agent.Alert
SQL Agent Alert object to update.
.OUTPUTS
Microsoft.SqlServer.Management.Smo.Agent.Alert
Returned when parameter **PassThru** is specified.
None
No output is returned unless **PassThru** is specified.
.EXAMPLE
$serverObject = Connect-SqlDscDatabaseEngine -InstanceName 'MyInstance'
Set-SqlDscAgentAlert -ServerObject $serverObject -Name 'MyAlert' -Severity 16
Updates the SQL Agent Alert named 'MyAlert' to severity level 16.
.EXAMPLE
$serverObject = Connect-SqlDscDatabaseEngine -InstanceName 'MyInstance'
$alertObject = $serverObject | Get-SqlDscAgentAlert -Name 'MyAlert'
$alertObject | Set-SqlDscAgentAlert -MessageId 50001
Updates the SQL Agent Alert using pipeline input with alert object.
.EXAMPLE
$serverObject = Connect-SqlDscDatabaseEngine -InstanceName 'MyInstance'
$updatedAlert = $serverObject | Set-SqlDscAgentAlert -Name 'MyAlert' -Severity 16 -PassThru
Updates the alert and returns the updated object.
.EXAMPLE
$serverObject = Connect-SqlDscDatabaseEngine -InstanceName 'MyInstance'
Set-SqlDscAgentAlert -ServerObject $serverObject -Name 'MyAlert' -Severity 16 -Force
Updates the SQL Agent Alert named 'MyAlert' to severity level 16 without prompting for confirmation.
#>
function Set-SqlDscAgentAlert
{
[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.')]
[CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'High')]
[OutputType([Microsoft.SqlServer.Management.Smo.Agent.Alert])]
param
(
[Parameter(ParameterSetName = 'ServerObject', Mandatory = $true, ValueFromPipeline = $true)]
[Microsoft.SqlServer.Management.Smo.Server]
$ServerObject,
[Parameter(ParameterSetName = 'AlertObject', Mandatory = $true, ValueFromPipeline = $true)]
[Microsoft.SqlServer.Management.Smo.Agent.Alert]
$AlertObject,
[Parameter(ParameterSetName = 'ServerObject', Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[System.String]
$Name,
[Parameter()]
[ValidateRange(0, 25)]
[System.Int32]
$Severity,
[Parameter()]
[ValidateRange(0, 2147483647)]
[System.Int32]
$MessageId,
[Parameter()]
[System.Management.Automation.SwitchParameter]
$PassThru,
[Parameter(ParameterSetName = 'ServerObject')]
[System.Management.Automation.SwitchParameter]
$Refresh,
[Parameter()]
[System.Management.Automation.SwitchParameter]
$Force
)
# cSpell: ignore SSAA
process
{
if ($Force.IsPresent -and -not $Confirm)
{
$ConfirmPreference = 'None'
}
# Validate that both Severity and MessageId are not specified
Assert-BoundParameter -BoundParameterList $PSBoundParameters -MutuallyExclusiveList1 @('Severity') -MutuallyExclusiveList2 @('MessageId')
if ($PSCmdlet.ParameterSetName -eq 'ServerObject')
{
if ($Refresh.IsPresent)
{
Write-Verbose -Message ($script:localizedData.Set_SqlDscAgentAlert_RefreshingServerObject)
$ServerObject.JobServer.Alerts.Refresh()
}
$alertObjectToUpdate = Get-AgentAlertObject -ServerObject $ServerObject -Name $Name
if ($null -eq $alertObjectToUpdate)
{
$errorMessage = $script:localizedData.Set_SqlDscAgentAlert_AlertNotFound -f $Name
$PSCmdlet.ThrowTerminatingError(
[System.Management.Automation.ErrorRecord]::new(
[System.Management.Automation.ItemNotFoundException]::new($errorMessage),
'SSAA0002', # cspell: disable-line
[System.Management.Automation.ErrorCategory]::ObjectNotFound,
$Name
)
)
}
}
else
{
$alertObjectToUpdate = $AlertObject
}
$verboseDescriptionMessage = $script:localizedData.Set_SqlDscAgentAlert_UpdateShouldProcessVerboseDescription -f $alertObjectToUpdate.Name, $alertObjectToUpdate.Parent.Parent.InstanceName
$verboseWarningMessage = $script:localizedData.Set_SqlDscAgentAlert_UpdateShouldProcessVerboseWarning -f $alertObjectToUpdate.Name
$captionMessage = $script:localizedData.Set_SqlDscAgentAlert_UpdateShouldProcessCaption
if ($PSCmdlet.ShouldProcess($verboseDescriptionMessage, $verboseWarningMessage, $captionMessage))
{
try
{
Write-Verbose -Message ($script:localizedData.Set_SqlDscAgentAlert_UpdatingAlert -f $alertObjectToUpdate.Name)
$hasChanges = $false
if ($PSBoundParameters.ContainsKey('Severity'))
{
if ($alertObjectToUpdate.Severity -ne $Severity)
{
Write-Verbose -Message ($script:localizedData.Set_SqlDscAgentAlert_SettingSeverity -f $Severity, $alertObjectToUpdate.Name)
$alertObjectToUpdate.Severity = $Severity
$alertObjectToUpdate.MessageId = 0 # Must set any conflicting properties to 0
$hasChanges = $true
}
else
{
Write-Verbose -Message ($script:localizedData.Set_SqlDscAgentAlert_SeverityAlreadyCorrect -f $Severity, $alertObjectToUpdate.Name)
}
}
if ($PSBoundParameters.ContainsKey('MessageId'))
{
if ($alertObjectToUpdate.MessageId -ne $MessageId)
{
Write-Verbose -Message ($script:localizedData.Set_SqlDscAgentAlert_SettingMessageId -f $MessageId, $alertObjectToUpdate.Name)
$alertObjectToUpdate.MessageId = $MessageId
$alertObjectToUpdate.Severity = 0 # Must set any conflicting properties to 0
$hasChanges = $true
}
else
{
Write-Verbose -Message ($script:localizedData.Set_SqlDscAgentAlert_MessageIdAlreadyCorrect -f $MessageId, $alertObjectToUpdate.Name)
}
}
if ($hasChanges)
{
$alertObjectToUpdate.Alter()
Write-Verbose -Message ($script:localizedData.Set_SqlDscAgentAlert_AlertUpdated -f $alertObjectToUpdate.Name)
}
else
{
Write-Verbose -Message ($script:localizedData.Set_SqlDscAgentAlert_NoChangesNeeded -f $alertObjectToUpdate.Name)
}
if ($PassThru.IsPresent)
{
return $alertObjectToUpdate
}
}
catch
{
$errorMessage = $script:localizedData.Set_SqlDscAgentAlert_UpdateFailed -f $alertObjectToUpdate.Name
$PSCmdlet.ThrowTerminatingError(
[System.Management.Automation.ErrorRecord]::new(
[System.InvalidOperationException]::new($errorMessage, $_.Exception),
'SSAA0008', # cspell: disable-line
[System.Management.Automation.ErrorCategory]::InvalidOperation,
$alertObjectToUpdate
)
)
}
}
}
}