This repository has been archived by the owner on May 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
New-SCOMHtmlNotificationChannel.ps1
503 lines (445 loc) · 18.9 KB
/
New-SCOMHtmlNotificationChannel.ps1
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
<#
.SYNOPSIS
Creates SCOM notification channels with HTML formatting enabled.
.DESCRIPTION
This script will create SCOM notification channels with HTML formatting options enabled. This allows for significantly richer notification templates.
Channels can either be created from scratch or clone their settings from an existing channel (and will therefore use its predefined endpoints).
By default notification bodies will include appropriate links to the SCOM Web Console, but by specifying the -SquaredUpURL parameter links will use Squared Up instead.
Please ensure that a management group connection exists prior to running the script - either run this from the Operations Manager shell or run New-SCOMManagementGroupConnection (remembering to import the OperationsManager module if on PowerShell v2).
.PARAMETER SquaredUpURL
The root URL of the Squared Up web console. If a protocol is not specified, http:// will be prepended. If this value is not specified, link will use the SCOM Web console instead.
.PARAMETER BaseSmtpChannel
Specifies the Id or Displayname of an existing Notification Channel from which to clone settings. You can get a list of IDs by running Get-SCOMNotificationChannel | select -ExpandProperty Action | fl DisplayName,Id
.PARAMETER SMTPServerFQDN
The FQDN of the SMTP relay you want to send notification emails to.
.PARAMETER SMTPFromAddress
The email address notification emails will be sent from, and the reply-to address.
.PARAMETER SMTPServerPort
The port number your SMTP relay is listening on. Defaults to 25 if unspecified.
.PARAMETER SMTPRetryMins
The number of minutes between retries should the SMTP server not respond.
.PARAMETER SMTPAuthentication
The authentication mechanism used by the SMTP relay. Valid values are 'Anonymous' (the default if unspecified) or 'Ntlm'.
.PARAMETER HighImportance
Controls whether channels are created with the High Importance mail flag. Default value is false.
.PARAMETER PlainText
Controls whether channels with a Plaintext format are created rather than HTML. Default value is false.
.EXAMPLE
C:\PS> .\New-SCOMHtmlNotificationChannel.ps1 -SquaredUpURL 'SQUP.contoso.com/SquaredUpv3' -SMTPServerFQDN "mail.contoso.com" -SMTPFromAddress "[email protected]" -HighImportance
Creates High Importance HTML channels in SCOM using the specified SMTP relay settings.
.EXAMPLE
C:\PS> .\New-SCOMHtmlNotificationChannel.ps1 -SquaredUpURL "SQUP.contoso.com/SquaredUpv3" -BaseSmtpChannelGuid 2149e02e-6bb2-661a-1535-17d1dba162ab -PlainText
Creates Plaintext channels with normal importance, taking all SMTP settings from an existing SMTP notification channel.
.INPUTS
None
.OUTPUTS
Microsoft.EnterpriseManagement.Administration.SmtpNotificationAction
.NOTES
Copyright 2017 Squared Up Limited, All Rights Reserved.
.LINK
https://www.squaredup.com
.LINK
https://github.com/squaredup
#>
[CmdletBinding(
SupportsShouldProcess=$true,
DefaultParameterSetName="New")]
Param(
[Parameter(
ParameterSetName = 'Clone',
Mandatory = $false
)]
[Parameter(
ParameterSetName = 'New',
Mandatory = $false
)]
[string]$SquaredUpURL,
[Parameter(
ParameterSetName = 'Clone',
Mandatory = $true
)]
[ValidateNotNullOrEmpty()]
[string]$BaseSmtpChannel,
[Parameter(
ParameterSetName = 'New',
Mandatory = $true
)]
[ValidateNotNullOrEmpty()]
[string]$SMTPServerFQDN,
[Parameter(
ParameterSetName = 'New',
Mandatory = $true
)]
[ValidateNotNullOrEmpty()]
[string]$SMTPFromAddress,
[Parameter(
ParameterSetName = 'New',
Mandatory = $false
)]
[ValidateRange(0,65535)]
[int]$SMTPServerPort = 25,
[Parameter(
ParameterSetName = 'New',
Mandatory = $false
)]
[ValidateRange(1,2147483647)]
[int]$SMTPRetryMins = 5,
[Parameter(
ParameterSetName = 'New',
Mandatory = $false
)]
[ValidateSet('Anonymous', 'Ntlm')]
[string]$SMTPAuthentication = 'Anonymous',
[Parameter(
ParameterSetName = 'New',
Mandatory = $false
)]
[Parameter(
ParameterSetName = 'Clone',
Mandatory = $false
)]
[Switch]$HighImportance,
[Parameter(
ParameterSetName = 'New',
Mandatory = $false
)]
[Parameter(
ParameterSetName = 'Clone',
Mandatory = $false
)]
[Switch]$PlainText
)
function Get-NotificationActionBody {
[CmdletBinding()]
[OutputType([String])]
Param(
[Switch]$Html,
[string]$SquaredUpURL
)
if ([string]::IsNullOrEmpty($SquaredUpURL)) {
Write-Verbose -Message 'Links in the notification body will use the SCOM Web Console' -Verbose:$VerbosePreference
$AlertUrl = '$Target/Property[Type="Notification!Microsoft.SystemCenter.AlertNotificationSubscriptionServer"]/WebConsoleUrl$?DisplayMode=Pivot&AlertID=$UrlEncodeData/Context/DataItem/AlertId$'
$ObjectUrl = '$Target/Property[Type="Notification!Microsoft.SystemCenter.AlertNotificationSubscriptionServer"]/WebConsoleUrl$?DisplayMode=Pivot&ViewType=DiagramView&PmoID=$UrlEncodeData/Context/DataItem/ManagedEntity$'
}
else {
Write-Verbose -Message "Links in the notification body will use the Squared Up server '$SquaredUpURL'" -Verbose:$VerbosePreference
$AlertUrl = $SquaredUpURL + '/drilldown/scomalert?id=$UrlEncodeData/Context/DataItem/AlertId$'
$ObjectUrl = $SquaredUpURL + '/drilldown/scomobject?id=$UrlEncodeData/Context/DataItem/ManagedEntity$'
}
$Severity = '$Data/Context/DataItem/Severity$'
$CreatedByMonitor = '$Data/Context/DataItem/CreatedByMonitor$'
$ResolutionStateName = '$Data/Context/DataItem/ResolutionStateName$'
$AlertName = '$Data/Context/DataItem/AlertName$'
$AlertDescription = '$Data/Context/DataItem/AlertDescription$'
$ManagedEntityDisplayName = '$Data/Context/DataItem/ManagedEntityDisplayName$'
$ManagedEntityPath = '$Data/Context/DataItem/ManagedEntityPath$'
$NotificationSubId = '$MPElement$'
if ($Html)
{
return [string]::Join("`n", @(
"<!DOCTYPE html>",
"<html><head>",
"<!-- (c) Squared Up Ltd 2017 -->",
"<style>div span{display:none;text-decoration:line-through;}.y{display:inline;text-decoration:none;}.s-$Severity{display:inline;text-decoration:none;}.m-$CreatedByMonitor{display:inline;text-decoration:none;}.t{font-weight:bold;}.b{background:#f1f1f1;}.b-2-New{background:#FF3E3E;}.b-1-New{background:#FDC700;}.b-0-New{background:#5F9ECA;}</style>",
"</head>",
"<body style='font-size:0.8em;font-family:arial;line-height:1.5em;color:#444444;'>",
"<div>",
"<div class='b'><div class='b-$Severity-$ResolutionStateName'> </div></div>",
"<div class='t'><span class='y'>$ResolutionStateName | </span><span class='s-2'>Critical</span><span class='s-1'>Warning</span><span class='s-0'>Info</span> | <span class='m-true'>Monitor</span><span class='m-false'>Rule</span></div>",
"<h2 style='margin-bottom:0;line-height:1em;'>$AlertName</h2>",
"on<br />",
"<b>$ManagedEntityDisplayName</b><br/>",
"<b>$ManagedEntityPath</b><br/>",
"<br />",
"<i>$AlertDescription</i><br />",
"<br/>",
"<a href='$AlertUrl'>view alert</a> | <a href='$ObjectUrl'>view object</a><br />",
"Notification Subscription ID: $NotificationSubId",
"</div>",
"</body>",
"</html>"
))
}
else
{
return [string]::Join("`n", @(
"$ResolutionStateName | Severity:$Severity | Monitor:$CreatedByMonitor",
"",
"$AlertName",
"on:",
"$ManagedEntityDisplayName",
"$ManagedEntityPath",
"",
"$AlertDescription",
"",
"View alert:",
"$AlertUrl",
"",
"View object:",
"$ObjectUrl",
"",
"Notification Subscription ID:",
"$NotificationSubId"
))
}
}
function Get-NotificationActionDisplayName {
[CmdletBinding()]
[OutputType([String])]
Param(
[Switch]$Html,
[Switch]$HighImportance,
[Switch]$SquaredUp
)
$emailFormat = "Plain text"
$importance = "Normal importance"
$console = "SCOM Web Console"
if ($Html) {
$emailFormat = "HTML"
}
if ($HighImportance) {
$importance = "High importance"
}
if ($SquaredUp) {
$console = "Squared Up Console"
}
return "$emailFormat Notifications - $console - $importance"
}
function Get-NotificationActionSubject {
[CmdletBinding()]
[OutputType([String])]
Param()
return 'Alert ($Data/Context/DataItem/ResolutionStateName$): $Data/Context/DataItem/AlertName$'
}
function Get-NotificationActionDescription {
[CmdletBinding()]
[OutputType([String])]
Param(
[Parameter(Mandatory = $false)]
[string]$BaseDisplayName
)
if ($PSBoundParameters.ContainsKey('BaseDisplayName')) {
return "This is a modified copy of the '$BaseDisplayName' channel. Any changes to the connection details of the original channel will be used automatically by this channel."
} else {
return "Created on $([Datetime]::Now) by $(Get-SCOMConnectedUser)"
}
}
function New-SmtpChannel {
[CmdletBinding(SupportsShouldProcess=$true)]
Param(
[string]$SquaredUpURL,
$ChannelSettings,
[switch]$PlainText,
[switch]$HighImportance
)
$action = New-Object -typename 'Microsoft.EnterpriseManagement.Administration.SmtpNotificationAction' -ArgumentList "SMTPHTMLNotificationChannel$([guid]::NewGuid().Guid.replace('-','_'))"
#Configure
$action.Subject = Get-NotificationActionSubject
$action.Body = Get-NotificationActionBody -Html:(!$PlainText) -SquaredUpURL $SquaredUpURL
$action.Description = $ChannelSettings.Description
$action.DisplayName = Get-NotificationActionDisplayName -Html:(!$PlainText) -HighImportance:$HighImportance -SquaredUp:(![string]::IsNullOrEmpty($SquaredUpURL))
$action.BodyEncoding = $ChannelSettings.BodyEncoding;
$action.Endpoint = $ChannelSettings.Endpoint;
$action.From = $ChannelSettings.From;
$action.IsBodyHtml = !$PlainText;
$action.ReplyTo = $ChannelSettings.ReplyTo;
$action.SubjectEncoding = $ChannelSettings.SubjectEncoding;
if ($HighImportance) {
$action.Headers.Add((New-Object -Typename Microsoft.EnterpriseManagement.Administration.SmtpNotificationActionHeader -ArgumentList "Importance", "High"))
$action.Headers.Add((New-Object -Typename Microsoft.EnterpriseManagement.Administration.SmtpNotificationActionHeader -ArgumentList "X-Priority", "1" ))
$action.Headers.Add((New-Object -Typename Microsoft.EnterpriseManagement.Administration.SmtpNotificationActionHeader -ArgumentList "X-MSMail-Priority", "High" ))
}
#Save
if ($null -eq $ChannelSettings.EndPoint.Id -and $pscmdlet.ShouldProcess("$($ChannelSettings.EndPoint.PrimaryServer.Address) on port $($ChannelSettings.EndPoint.PrimaryServer.PortNumber) using $($ChannelSettings.EndPoint.PrimaryServer.AuthenticationType) authentication","Create SMTP Endpoint"))
{
Write-Verbose -Message "Creating SMTP Endpoint '$($ChannelSettings.EndPoint.PrimaryServer.Address)' on port $($ChannelSettings.EndPoint.PrimaryServer.PortNumber) using $($ChannelSettings.EndPoint.PrimaryServer.AuthenticationType) authentication" -Verbose:$VerbosePreference
Save-SCOMNotificationEndpoint -NotificationEndpoint $ChannelSettings.EndPoint
}
if ($pscmdlet.ShouldProcess($action.DisplayName,"Create SMTP Notification channel"))
{
Write-Verbose -Message "Creating SMTP notification channel '$($action.DisplayName)'" -Verbose:$VerbosePreference
Save-SCOMNotificationAction -NotificationAction $action
return $action
}
}
function New-SmtpEndpoint {
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseShouldProcessForStateChangingFunctions", "")]
[CmdletBinding()]
[OutputType([Microsoft.EnterpriseManagement.Administration.SmtpNotificationEndpoint])]
Param(
[string]$Name,
[int]$SMTPRetryMins,
[string]$SMTPServerFQDN,
[int]$SMTPServerPort,
[string]$SMTPAuthentication,
[string]$Description
)
$primaryServer = New-Object -TypeName 'Microsoft.EnterpriseManagement.Administration.SmtpServer' -ArgumentList $SMTPServerFQDN
$primaryServer.AuthenticationType = $SMTPAuthentication
$primaryServer.PortNumber = $SMTPServerPort
$endpoint = New-Object -TypeName 'Microsoft.EnterpriseManagement.Administration.SmtpNotificationEndpoint' -ArgumentList "SMTPEndpoint$([guid]::NewGuid().Guid.replace('-','_'))",'Smtp',$primaryServer
$endpoint.PrimaryServerSwitchBackIntervalSeconds = $SMTPRetryMins * 60
$endpoint.DisplayName = "SMTPEndpoint for $Name"
$endpoint.Description = $Description
return $endpoint
}
function Convert-SquaredUpUrl {
[CmdletBinding()]
[OutputType([string])]
Param(
[string]$Url
)
$Url = $Url.TrimEnd('/')
if ($Url -notmatch '^https?://') {
$Url = "http://$Url"
Write-Verbose -Message "Set Squared Up URL to '$Url'" -Verbose:$VerbosePreference
}
if ([System.Uri]::IsWellFormedUriString($Url, [System.UriKind]::Absolute) -eq $false) {
Throw "'$Url' is an invalid URL."
}
return $Url
}
function New-ChannelSettings {
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseShouldProcessForStateChangingFunctions", "")]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseSingularNouns", "")]
[OutputType([PSCustomObject])]
[CmdletBinding()]
Param(
[string]$Description,
[int]$SMTPRetryMins,
[string]$SMTPServerFQDN,
[int]$SMTPServerPort,
[string]$SMTPAuthentication,
[string]$SMTPFromAddress
)
$endpointParams = Get-ParametersFromHashtable -Function "New-SmtpEndpoint" $PSBoundParameters
return [PSCustomObject]@{
"Description" = $description;
"BodyEncoding" = "utf-8";
"Endpoint" = New-SmtpEndpoint -Name "Advanced Notifications" @endpointParams
"From" = $SMTPFromAddress;
"ReplyTo" = $SMTPFromAddress;
"SubjectEncoding" = "utf-8";
}
}
function Copy-ChannelSettings {
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseSingularNouns", "")]
[OutputType([PSCustomObject])]
[CmdletBinding()]
Param(
[string]$BaseSmtpChannel
)
$baseSmtpAction = Get-SCOMNotificationAction -DisplayNameOrId $BaseSmtpChannel
Write-Verbose "Using '$($baseSmtpAction.DisplayName)' as a template"
# Create settings object and return
return [PSCustomObject]@{
"Description" = Get-NotificationActionDescription -BaseDisplayName $baseSmtpAction.DisplayName;
"BodyEncoding" = $baseSmtpAction.BodyEncoding;
"Endpoint" = $baseSmtpAction.Endpoint;
"From" = $baseSmtpAction.From;
"ReplyTo" = $baseSmtpAction.ReplyTo;
"SubjectEncoding" = $baseSmtpAction.SubjectEncoding;
}
}
function Get-ParametersFromHashtable {
[CmdletBinding()]
[OutputType([System.Collections.Hashtable])]
Param(
[string]$Function,
[HashTable]$Parameters
)
$finalParameters = @{}
# Filter out parameters that the target function doesn't accept
foreach ($param in (get-command $Function).Parameters.Keys) {
if ($Parameters.ContainsKey($param)) {
$finalParameters[$param] = $Parameters[$param]
}
}
return $finalParameters
}
function Get-SCOMConnectedUser {
[CmdletBinding()]
[OutputType([string])]
Param()
$connectionSettings = (Get-SCOMManagementGroup -ErrorAction Stop).ConnectionSettings
return "$($onnectionSettings.Domain)\$($connectionSettings.UserName)"
}
function Save-SCOMNotificationAction {
[CmdletBinding()]
Param(
[Microsoft.EnterpriseManagement.Administration.NotificationAction]$NotificationAction
)
if ($null -eq $NotificationAction.Id) {
(Get-SCOMManagementGroup -ErrorAction Stop).InsertNotificationAction($NotificationAction)
} else {
$NotificationAction.Update()
}
}
function Save-SCOMNotificationEndpoint {
[CmdletBinding()]
Param(
[Microsoft.EnterpriseManagement.Administration.NotificationEndpoint]$NotificationEndpoint
)
if ($null -eq $NotificationEndpoint.Id) {
(Get-SCOMManagementGroup -ErrorAction Stop).InsertNotificationEndpoint($NotificationEndpoint)
} else {
$NotificationEndpoint.Update()
}
}
function Get-SCOMNotificationAction {
[CmdletBinding()]
[OutputType([Microsoft.EnterpriseManagement.Administration.NotificationAction])]
Param(
[string]$DisplayNameOrId
)
# Test if we have a GUID or a displayname specified for the base channel
if ($DisplayNameOrId -match '^[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12}$') {
$guid = [guid]$DisplayNameOrId
return (Get-SCOMManagementGroup -ErrorAction Stop).GetNotificationAction($guid)
}
else {
$action = (Get-SCOMNotificationChannel -DisplayName $DisplayNameOrId).Action
# Ensure that we found an appropriate channel to clone
if ($null -eq $action)
{
Throw "The notification channel '$DisplayNameOrId' could not be found."
}
return $action
}
}
# Bind default parameter values as if user had specified them
foreach ($param in $MyInvocation.MyCommand.Parameters.Keys) {
$value = Get-Variable $param -ValueOnly -ErrorAction SilentlyContinue
if ($value -and !$PSBoundParameters.ContainsKey($param)) {
$PSBoundParameters[$param] = $value
}
}
# Normalise SquaredUpURL
if ($PSBoundParameters.ContainsKey("SquaredUpURL")) {
$SquaredUpURL = Convert-SquaredUpUrl -Url $SquaredUpURL
}
# Main block
try{
# Construct Channel Settings
$channelSettings = $null
switch ($PSCmdlet.ParameterSetName) {
"New" {
# Create a new endpoint and use user specified channel settings.
$params = Get-ParametersFromHashtable -Function "New-ChannelSettings" -Parameters $PSBoundParameters
$channelSettings = New-ChannelSettings -Description (Get-NotificationActionDescription) @params
}
"Clone" {
# Copy an existing channel settings and use existing endpoint
$channelSettings = Copy-ChannelSettings -BaseSmtpChannel $BaseSmtpChannel
}
default {
Throw "Unable to determine source of SMTP channel settings"
}
}
# Create Channel
New-SmtpChannel -SquaredUpURL $SquaredUpURL -ChannelSettings $channelSettings -HighImportance:$HighImportance -PlainText:$PlainText
}
catch
{
throw
}