-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSend-ScriptMessage.ps1
More file actions
336 lines (294 loc) · 13.4 KB
/
Send-ScriptMessage.ps1
File metadata and controls
336 lines (294 loc) · 13.4 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
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
function Send-ScriptMessage
{
<#
.LINK
https://github.com/Sekers/ScriptMessage/wiki
.SYNOPSIS
Sends a message using the specified messaging service.
.DESCRIPTION
Sends a message using the specified messaging service.
Note: If necessary, you will be asked to authenticate to the messaging service.
.PARAMETER Service
Specify the messaging service(s) to send the message from. You can specify more than one service to send the same message for redundancy or other purposes.
.PARAMETER Type
Optionally specify the type(s) of message(s) (mail, chat, etc.) to use when sending the message. Defaults to the 'Mail' service type. You can specify more than one message type to send the same message for redundancy or other purposes.
.PARAMETER ServiceType
Alternatively, you can specify both the messaging service(s) and message type(s) in a single parameter. You can specify more than one service/type combination to send the same message for redundancy or other purposes.
.PARAMETER From
The messaging address you are sending from. Alternatively, provide an object with the 'Address' property value set to the messaging address and, optionally, include a 'Name' property and corresponding value.
.PARAMETER ReplyTo
The messaging address(es) you want recipients to reply to. Alternatively, provide an object (or array of objects) with the 'Address' property value set to the messaging address you want recipients to reply to and, optionally, include a 'Name' property and corresponding value.
.PARAMETER To
An array of addresses to send the message to. Alternatively, provide an object (or array of objects) with the 'Address' property value set to the messaging address you want to send to and, optionally, include a 'Name' property and corresponding value.
Must have at least one of 'To', 'CC', or 'BCC' set, depending on the messaging service used.
.PARAMETER CC
An array of addresses to carbon copy (CC) the message to. Alternatively, provide an object (or array of objects) with the 'Address' property value set to the messaging address you want to send to and, optionally, include a 'Name' property and corresponding value.
Must have at least one of 'To', 'CC', or 'BCC' set, depending on the messaging service used.
.PARAMETER BCC
An array of addresses to blind carbon copy (BCC) the message to. Alternatively, provide an object (or array of objects) with the 'Address' property value set to the messaging address you want to send to and, optionally, include a 'Name' property and corresponding value.
Must have at least one of 'To', 'CC', or 'BCC' set, depending on the messaging service used.
.PARAMETER SaveToSentItems
Use this parameter to ask the messaging service to save the sent message to a 'Sent Items' location, if supported by the service.
Defaults to '$true'.
.PARAMETER Subject
Specify the message subject, if supported by the messaging service.
.PARAMETER Body
An object with the 'Content' property value set to message you want to send. Optionally, include a 'ContentType' property and corresponding value ('Text' or 'HTML').
'ContentType' defaults to 'Text'.
.PARAMETER Attachment
Specify any file attachments, if supported by the messaging service.
You can submit an array of any of the following (you can mix types in the array):
- Content (Hashtable). Submit with a 'Name' key value set to the filename you want and a 'Content' key value as the data (encoded as a byte stream).
- File path (String)
- Directory path (String)
.PARAMETER SenderId
Specify the account used to send the message request. This might be different than the 'From' parameter in the case of "Send As', "Send on Behalf", delegated mailboxes, etc.
If not specified, defaults to the address inside of the 'From' parameter.
.PARAMETER MailType # TODO: Implement OneOnOne emailing.
Override the default 'MailType' specified in the configuration file for the messaging service being used. Options are 'OneOnOne' or 'Group'.
.PARAMETER ChatType
Override the default 'ChatType' specified in the configuration file for the messaging service being used. Options are 'OneOnOne' or 'Group'.
.PARAMETER IncludeBCCInGroupChat
Specify whether to include BCC recipients in a group chat message.
.EXAMPLE
$MessageArguments = @{
From = '[email protected]'
To = '[email protected]'
CC = @()
Subject = "Test Message"
Body = @{
Content = "This is a test message.`n`nThank you!"
}
}
Send-ScriptMessage -Service MicrosoftGraph -Type 'Mail' @MessageArguments
.EXAMPLE
$MessageArguments = @{
From = '[email protected]'
To = @('[email protected]')
CC = @( "[email protected]", "[email protected]")
Subject = "Test Message"
Body = @{
Content = "This is a test message.`n`nThank you!"
}
}
Send-ScriptMessage -Service MicrosoftGraph -Type 'Mail', 'Chat' @MessageArguments
.EXAMPLE
$MessageArguments = @{
From = @{
Name = 'John Doe'
Address = '[email protected]'
}
ReplyTo= @{
Name = "Lisa Maloney"
Address = "[email protected]"
}
To = @('[email protected]')
CC = @( "[email protected]", "[email protected]")
SaveToSentItems = $true
Subject = "Test Message"
Body = @{
ContentType = 'Text'
Content = "This is a test message.`n`nThank you!"
}
Attachment = @('C:\StuffToSend\', 'C:\Documents\AnotherFile.pdf')
SenderId = '[email protected]'
}
Send-ScriptMessage -Service MicrosoftGraph @MessageArguments
.EXAMPLE
# Attachments From Variable - Option 1: PS Desktop or Core
$Content1 = [System.IO.File]::ReadAllBytes('C:\Users\John\Downloads\MyPDF.pdf')
$Content2 = [System.IO.File]::ReadAllBytes('C:\Users\John\Downloads\AMovie.mp4')
# Attachments From Variable - Option 2: PS Desktop Only
# $Content1 = Get-Content -Encoding Byte -Raw -Path 'C:\Users\John\Downloads\MyPDF.pdf'
# $Content2 = Get-Content -Encoding Byte -Raw -Path 'C:\Users\John\Downloads\AMovie.mp4'
# Attachments From Variable - Option 3: PS Core Only
# $Content1 = Get-Content -AsByteStream -Raw -Path 'C:\Users\John\Downloads\MyPDF.pdf'
# $Content2 = Get-Content -AsByteStream -Raw -Path 'C:\Users\John\Downloads\AMovie.mp4'
$Attachment = @(
@{
Name = 'MyPDF.pdf'
Content = $Content1
},
@{
Name = 'funnyballgame.mp4'
Content = $Content2
}
}
$MessageArguments = @{
From = @{
Address = '[email protected]'
}
To = @('[email protected]')
Subject = "Test Message"
Body = @{
Content = "This is a test message.`n`nThank you!"
}
Attachment = $Attachment
}
#>
[CmdletBinding()]
param(
[Parameter(
ParameterSetName = 'ServiceAndTypeSeparate',
Mandatory = $true,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true)]
[MessagingService[]]$Service,
[Parameter(
ParameterSetName = 'ServiceAndTypeSeparate',
Mandatory = $false,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true)]
[MessageType[]]$Type = 'Mail',
[Parameter(
ParameterSetName = 'ServiceAndTypeCombined',
Mandatory = $true,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true)]
[MessageServiceType[]]$ServiceType,
[Parameter(
Mandatory = $true,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true)]
[pscustomobject]$From,
[Parameter(
Mandatory = $false,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true)]
[AllowEmptyString()]
[pscustomobject]$ReplyTo,
[Parameter(
Mandatory = $false,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true)]
[AllowEmptyString()]
[pscustomobject]$To,
[Parameter(
Mandatory = $false,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true)]
[AllowEmptyString()]
[pscustomobject]$CC,
[Parameter(
Mandatory = $false,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true)]
[AllowEmptyString()]
[pscustomobject]$BCC,
[Parameter(
Mandatory = $false,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true)]
[bool]$SaveToSentItems = $true,
[Parameter(
Mandatory = $true,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true)]
[string]$Subject,
[Parameter(
Mandatory = $true,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true)]
[pscustomobject]$Body,
[Parameter(
Mandatory = $false,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true)]
[array]$Attachment, # Array of Content (bytes), File paths, and/or Directory paths,
[Parameter(
Mandatory = $false,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true)]
[string]$SenderId,
# TODO: Implement MailType (similar to ChatType so we can do 1:1 emailing)
[Parameter(
Mandatory = $false,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true)]
[MailType]$MailType,
[Parameter(
Mandatory = $false,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true)]
[ChatType]$ChatType,
[Parameter(
Mandatory = $false,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true)]
[ValidateSet($null, $true, $false)]
[Object]$IncludeBCCInGroupChat # Is an object so it can be set to $null
)
# Set the necessary configuration variables.
$ScriptMessageConfig = Get-ScriptMessageConfig
# Make sure that at least one of, To, CC, or BCC is provided.
if ([string]::IsNullOrEmpty($To) -and [string]::IsNullOrEmpty($CC) -and [string]::IsNullOrEmpty($BCC))
{
throw 'Please provide at least one parameter value for any of the following: To, CC, or BCC'
}
# Convert recipient types into properly formatted PSObject.
$From = ConvertTo-ScriptMessageRecipientObject -Recipient $From # Note that From is NOT an array. There should only be one.
[array]$ReplyTo = ConvertTo-ScriptMessageRecipientObject -Recipient $ReplyTo
[array]$To = ConvertTo-ScriptMessageRecipientObject -Recipient $To
[array]$CC = ConvertTo-ScriptMessageRecipientObject -Recipient $CC
[array]$BCC = ConvertTo-ScriptMessageRecipientObject -Recipient $BCC
# Convert body into properly formatted PSObject.
$Body = ConvertTo-ScriptMessageBodyObject -Body $Body
# Convert attachments into properly formatted PSObject.
$Attachment = ConvertTo-ScriptMessageAttachmentObject -Attachment $Attachment
if ($null -ne $Service) # If ServiceAndTypeSeparate
{
# Remove message service & message type duplicates.
$Service = $Service | Select-Object -Unique
$Type = $Type | Select-Object -Unique
# Create the ServiceType class object\hash.
[MessageServiceType]$ServiceType = @{
Service = $Service
Type = $Type
}
}
foreach ($serviceTypeObj in $ServiceType) # TODO: Catch errors once we have multiple services so if one fails the other(s) can still be processed.
{
# Set the connection parameters.
$ConnectionParameters = @{
ServiceConfig = $ScriptMessageConfig.$($serviceTypeObj.Service)
}
# Set default values if not specified by a parameter.
if (-not $ChatType)
{
[ChatType]$ChatType = $ConnectionParameters.ServiceConfig.ChatType
}
if (-not $IncludeBCCInGroupChat)
{
[bool]$IncludeBCCInGroupChat = $ConnectionParameters.ServiceConfig.IncludeBCCInGroupChat
}
# Connect to the messaging service, if necessary (e.g., API service).
Connect-ScriptMessage -Service $($serviceTypeObj.Service) -ErrorAction Stop
switch ($($serviceTypeObj.Service))
{
'MicrosoftGraph' {
$SendMessageParameters = [ordered]@{
From = $From
ReplyTo = $ReplyTo
To = $To
CC = $CC
BCC = $BCC
SaveToSentItems = $SaveToSentItems
Subject = $Subject
Body = $Body
Attachment = $Attachment
SenderId = $SenderId
Type = $serviceTypeObj.Type
ChatType = $ChatType
IncludeBCCInGroupChat = $IncludeBCCInGroupChat
}
Send-ScriptMessage_MicrosoftGraph @SendMessageParameters
# Disconnect from Microsoft Graph API, if enabled in config.
if ($ConnectionParameters.ServiceConfig.MgDisconnectWhenDone)
{
$null = Disconnect-MgGraph -ErrorAction SilentlyContinue
}
}
Default {throw "Invalid `'Service`' value."}
}
}
}