-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlertTester.ps1
95 lines (91 loc) · 2.66 KB
/
AlertTester.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
Param(
[Parameter(Mandatory=$true, Position=0)]
[ValidateSet("smtp","teams","slack","stride")]
[String[]]
$alert,
[String]
$smtpFrom,
[String]
$smtpTo,
[String]
$smtpServer,
[String]
$slackWebhook,
[String]
$teamsWebhook,
[string]
$strideWebhook,
[string]
$strideAuthToken
)
$alertSubject = "Alert test"
$alertBody = "This is an alert test."
switch ($alert){
smtp {
if ((!$smtpFrom) -and (!$smtpTo) -and (!$smtpServer)) {
Write-Warning "> Cannot send email, requires smtpTo, smtpFrom and smtpServer."
break
}
# $credentials = $null
Write-Verbose "> Sending Email alert to $smtpTo from $smtpFrom through $smtpServer."
Send-Mailmessage -to $smtpTo -from $smtpFrom -subject $alertSubject -BodyAsHtml $alertBody -smtpserver $smtpServer #-Credential $credentials
}
teams {
# Requires Teams Webhook.
if (!$teamsWebhook) {
Write-Warning "> Cannot send alert, requires Webhook."
break
}
Write-Verbose "> Sending alert to Teams channel."
$data = @{
'text'= "<p>" + $alertSubject + "</p><p>" + $alertBody + "</p>"
}
$request = @{
Headers = @{'accept'='application/json'}
Body = $data | ConvertTo-JSON
Method = 'POST'
URI = $teamsWebhook
}
Invoke-RestMethod @request
}
slack {
# Requires Slack webhook.
if (!$slackWebhook) {
Write-Warning "> Cannot send alert, requires Webhook."
break
}
Write-Verbose "> Sending alert to Slack channel."
$data = @{
'text'= $alertBody
'pretext' = $alertSubject
'color' = "warning"
}
$request = @{
Headers = @{'accept'='application/json'}
Body = $data | ConvertTo-JSON
Method = 'POST'
URI = $slackWebhook
}
Invoke-RestMethod @request
}
stride {
# Requires Stride Webhook.
if ((!$strideWebhook) -and (!$strideAuthToken)) {
Write-Warning "> Cannot send alert, requires Webhook and AuthToken."
break
}
Write-Verbose "> Sending alert to Stride channel."
$data = "$alertSubject `n$alertBody"
$headers =@{
'Content-Type'='text/plain'
'Authorization' = "Bearer $strideAuthToken"
}
$request = @{
Headers = $headers
Body = $data
Method = 'POST'
URI = $strideWebhook
}
Invoke-RestMethod @request
}
}