-
Notifications
You must be signed in to change notification settings - Fork 0
/
notification.ps1
141 lines (125 loc) · 6.92 KB
/
notification.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
#####################################################
# HelloID-Conn-Prov-Notification-MessageBird
#
# Version: 1.1.0
#####################################################
# Initialize default values
$success = $false
# Enable TLS1.2
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor [System.Net.SecurityProtocolType]::Tls12
# Set debug logging
switch ($($actionContext.Configuration.IsDebug)) {
$true { $VerbosePreference = 'Continue' }
$false { $VerbosePreference = 'SilentlyContinue' }
}
try {
if ($($actionContext.TemplateConfiguration.scriptFlow) -eq "SMS") {
# Execute script flow SMS
# Set authorizationheaders
$headers = [System.Collections.Generic.Dictionary[string, string]]::new()
$headers.Add("Authorization", "AccessKey $($actionContext.Configuration.accesskey)")
$sendMessageBody = @{
recipients = $actionContext.TemplateConfiguration.recipients
originator = $actionContext.Configuration.originator
body = $actionContext.TemplateConfiguration.body
}
# Optional, define date and time of the message
if (![String]::IsNullOrEmpty($actionContext.TemplateConfiguration.time)) {
# Define the date and time
$currentDate = Get-date
$time = $actionContext.TemplateConfiguration.time
# Create a DateTime object of current date and specified time
$dateTimeString = $currentDate.toString("yyyy-MM-dd") + " $time"
$scheduledDatetime = [datetime]$dateTimeString
# Convert DateTime to RFC3339 format (Y-m-d\TH:i:sP)
$scheduledDatetimeRFC = $scheduledDatetime.ToString("yyyy-MM-dd\THH:mm:sszzz", [System.Globalization.CultureInfo]::InvariantCulture)
$sendMessageBody += @{ scheduledDatetime = $scheduledDatetimeRFC }
}
$body = $sendMessageBody | ConvertTo-Json
$splatParams = @{
Uri = $actionContext.Configuration.baseUri
Headers = $headers
Method = 'POST'
Body = ([System.Text.Encoding]::UTF8.GetBytes($body))
ContentType = "application/json"
ErrorAction = "Stop"
}
if (-Not($actionContext.DryRun -eq $true)) {
if ("scheduledDatetime" -in $sendMessageBody.Keys) {
Write-Verbose "Scheduling MessageBird notification [$($actionContext.TemplateConfiguration.scriptFlow)] for [$($personContext.Person.DisplayName)] to [$($sendMessageBody.recipients)] at [$($sendMessageBody.scheduledDatetime)]"
}
else {
Write-Verbose "Sending MessageBird notification [$($actionContext.TemplateConfiguration.scriptFlow)] for [$($personContext.Person.DisplayName)] to [$($sendMessageBody.recipients)]"
}
$response = Invoke-RestMethod @splatParams
if ("scheduledDatetime" -in $sendMessageBody.Keys) {
$outputContext.AuditLogs.Add([PSCustomObject]@{
Message = "Successfully scheduled MessageBird notification [$($actionContext.TemplateConfiguration.scriptFlow)] for [$($personContext.Person.DisplayName)] to [$($sendMessageBody.recipients)] at [$($sendMessageBody.scheduledDatetime)]"
IsError = $false
})
}
else {
$outputContext.AuditLogs.Add([PSCustomObject]@{
Message = "Successfully sent MessageBird notification [$($actionContext.TemplateConfiguration.scriptFlow)] for [$($personContext.Person.DisplayName)] to [$($sendMessageBody.recipients)]"
IsError = $false
})
}
}
else {
# Log what would happen during enforcement
if ("scheduledDatetime" -in $sendMessageBody.Keys) {
Write-Warning "DryRun: Would schedule MessageBird notification [$($actionContext.TemplateConfiguration.scriptFlow)] for [$($personContext.Person.DisplayName)] to [$($sendMessageBody.recipients)] at [$($sendMessageBody.scheduledDatetime)]"
}
else {
Write-Warning "DryRun: Would Send MessageBird notification [$($actionContext.TemplateConfiguration.scriptFlow)] for [$($personContext.Person.DisplayName)] to [$($sendMessageBody.recipients)]"
}
Write-Verbose "Uri: $($splatParams.Uri)"
Write-Verbose "Body: $($sendMessageBody)"
}
}
else {
throw "Incorrect scriptFlow"
}
}
catch {
$ex = $PSItem
Write-Verbose "Error: $($ex.Exception.Message)"
Write-Verbose "Uri: $($splatParams.Uri)"
Write-Verbose "Body: $($sendMessageBody)"
Write-Verbose "SplatParams: $($splatParams | ConvertTo-Json)"
switch ($ex.Exception.Message) {
'Incorrect scriptFlow' {
$errorMessage = "Incorrect scriptFlow [$($actionContext.TemplateConfiguration.scriptFlow)]"
}
default {
if ($($ex.Exception.GetType().FullName -eq 'Microsoft.PowerShell.Commands.HttpResponseException') -or
$($ex.Exception.GetType().FullName -eq 'System.Net.WebException')) {
if ("scheduledDatetime" -in $sendMessageBody.Keys) {
$errorMessage = "Error scheduling MessageBird notification [$($actionContext.TemplateConfiguration.scriptFlow)] for [$($personContext.Person.DisplayName)] to [$($sendMessageBody.recipients)] at [$($sendMessageBody.scheduledDatetime)]. Error: $($ex.ErrorDetails.Message)"
}
else {
$errorMessage = "Error sending MessageBird notification [$($actionContext.TemplateConfiguration.scriptFlow)] for [$($personContext.Person.DisplayName)] to [$($sendMessageBody.recipients)]. Error: $($ex.ErrorDetails.Message)"
}
}
else {
if ("scheduledDatetime" -in $sendMessageBody.Keys) {
$errorMessage = "Error scheduling MessageBird notification [$($actionContext.TemplateConfiguration.scriptFlow)] for [$($personContext.Person.DisplayName)] to [$($sendMessageBody.recipients)] at [$($sendMessageBody.scheduledDatetime)]. Error: $($ex.Exception.Message) $($ex.ScriptStackTrace)"
}
else {
$errorMessage = "Error sending MessageBird notification [$($actionContext.TemplateConfiguration.scriptFlow)] for [$($personContext.Person.DisplayName)] to [$($sendMessageBody.recipients)]. Error: $($ex.Exception.Message) $($ex.ScriptStackTrace)"
}
}
}
}
$outputContext.AuditLogs.Add([PSCustomObject]@{
Message = $errorMessage
IsError = $true
})
}
finally {
# Check if auditLogs contains errors, if no errors are found, set success to true
if (-NOT($outputContext.AuditLogs.isError -contains $true)) {
$success = $true
}
$outputContext.Success = $success
}