Office365-Scoutnet-synk.psm1

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
504
505
506
507
508
#Requires -Version 5.1

[Net.ServicePointManager]::SecurityProtocol = "tls12, tls11, tls"

#region import everything we need

. $PSScriptRoot\SNSConfiguration.ps1
. $PSScriptRoot\Write-SNSLog.ps1
. $PSScriptRoot\ConvertTo-SNSJSONHash.ps1
. $PSScriptRoot\Receive-SNSApiJson.ps1
. $PSScriptRoot\Get-SNSApiGroupCustomlist.ps1
. $PSScriptRoot\Get-SNSMaillistInfo.ps1
. $PSScriptRoot\Get-SNSUserEmail.ps1
. $PSScriptRoot\Get-SNSExchangeMailListMember.ps1
. $PSScriptRoot\Get-SNSApiGroupMemberlist.ps1
. $PSScriptRoot\Invoke-SNSUppdateOffice365User.ps1
. $PSScriptRoot\Connect-SnSOffice365.ps1
. $PSScriptRoot\Disconnect-SnSOffice365.ps1

#endregion

# Configuration holder.
$script:SNSConf=[SNSConfiguration]::new()
Export-ModuleMember -Variable SNSConf

function Add-Office365UserToList
{
    [OutputType([bool], [string])]
    param (
        [Parameter(Mandatory=$True, HelpMessage="List with all Office365 users.")]
        [ValidateNotNull()]
        $allOffice365Users,

        [Parameter(Mandatory=$True, HelpMessage="Scoutnet Id for member to add.")]
        [ValidateNotNull()]
        $member,

        [Parameter(Mandatory=$True, HelpMessage="Data for the member from Scoutnet.")]
        [ValidateNotNull()]
        $MemberData,

        [Parameter(Mandatory=$True, HelpMessage="Name of group to add member in.")]
        [ValidateNotNull()]
        $distGroupName,

        [Parameter(HelpMessage="Do warn about missing Office 365 user.")]
        [switch] $doWarn
        )

    $memberSearchStr = "*$member"
    $recipient = $allOffice365Users | Where-Object {$_.CustomAttribute1 -like $memberSearchStr}

    $userAdded = $False
    $PrimarySmtpAddress = ""

    if ($recipient)
    {
        $PrimarySmtpAddress = $recipient.PrimarySmtpAddress
        $userAdded = $True
        Write-SNSLog "Added member $($recipient.DisplayName) with smtp address $($recipient.PrimarySmtpAddress) to distribution group $distGroupName"
    }
    elseif ($doWarn)
    {
        Write-SNSLog -Level "Warn" "Member $($MemberData.first_name) $($MemberData.last_name) not found in office 365. Please make sure that CustomAttribute1 contains Scoutnet Id for the user."
    }
    return $userAdded, $PrimarySmtpAddress
}


function New-SNSMailContact
{
    param (
        [ValidateNotNull()]
        $Epost,

        [ValidateNotNull()]
        $DisplayName
        )

    $contactCreated = $True
    $ExistingMailContact = Get-EXORecipient $Epost -ErrorAction "SilentlyContinue" -Verbose:$false
    if ($null -eq $ExistingMailContact)
    {
        Write-SNSLog "Creating Contact $Epost for $DisplayName"
        try
        {
            New-MailContact -Name $Epost -ExternalEmailAddress $Epost -ErrorAction "stop" -Verbose:$false

            # Set the name of the member in the company field. This is visibel in Office 365 admin console.
            Set-Contact -Identity $Epost -Company "$DisplayName" -Verbose:$false
            Set-MailContact -Identity $Epost -HiddenFromAddressListsEnabled $true -Verbose:$false
        }
        Catch
        {
            Write-SNSLog -Level "Warn" "Could not create mail contact with address $Epost. Error $_"
            $contactCreated = $False
        }
    }

    return $contactCreated
}


function SNSUpdateExchangeDistributionGroups
{
    <#
    .SYNOPSIS
        Updates exchange distribution groups based on scoutnet maillists.

    .DESCRIPTION
        Fetches the distribution groups members and updates corressponding distribution groups based on the info from scoutnet.
        As all members of an distribution group must be present in exchange as user or contact, contacts will be created for external addresses.

    .INPUTS
        None. You cannot pipe objects to SNSUpdateExchangeDistributionGroups.

    .OUTPUTS
        None.

    .LINK
        https://github.com/scouternasetjanster/Office365-Scoutnet-synk

    .PARAMETER ValidationHash
        Hash value used to validate if scoutnet is updated.

    .PARAMETER Configuration
        Configuration to use. If not specified the cached configuration will be used.

    .PARAMETER ReturnMaildata
        Enables return of a Hashtable with mailaddresses for the list. Can be used together with -WhatIf to check settings.

    .PARAMETER WhatIf
        The WhatIf switch simulates the actions of the command.
        You can use this switch to view the changes that would occur without actually applying those changes.
        You don't need to specify a value with this switch.
    #>

    [CmdletBinding(HelpURI = 'https://github.com/scouternasetjanster/Office365-Scoutnet-synk',
                PositionalBinding = $False,
                SupportsShouldProcess = $True)]
    param (
        [Parameter(Mandatory=$True, HelpMessage="Hash value used to validate if Scoutnet is updated.")]
        [ValidateNotNull()]
        [string]$ValidationHash,

        [Parameter(Mandatory=$False, HelpMessage="Configuratin to use. If not specified the cached configuration will be used.")]
        $Configuration,

        [Parameter(HelpMessage="Enables return of a Hashtable with mailaddresses for the list. Can be used together with -WhatIf to check settings.")]
        [switch] $ReturnMaildata
    )

    if ($Configuration)
    {
        $Script:SNSConf = $Configuration
    }

    if (!$Script:SNSConf)
    {
        throw "No configuration specified. Please provide a configuration!"
    }

    $AddressesInMailLists = @{}

    $MailListSettings = $Script:SNSConf.MailListSettings

    # Fetch maillist info from scoutnet.
    $CustomLists, $CustomListsHash = Get-SNSMaillistInfo -CredentialCustomlists $Script:SNSConf.CredentialCustomlists -MaillistsIds $MailListSettings.values

    # Fetch all members and their mailaddresses.
    $allMailAddresses, $allMailAddressesHash = Get-SNSUserEmail -CredentialMemberlist $Script:SNSConf.CredentialMemberlist

    $MailListSettingsHash = ("{0:X8}" -f (($MailListSettings | ConvertTo-Json).GetHashCode()))

    $OutValidationHash = $ValidationHash
    $NewValidationHash = "0x{0}{1}{2}" -f ($CustomListsHash, $allMailAddressesHash, $MailListSettingsHash)

    Write-SNSLog "Saved validation hash: $ValidationHash new value $NewValidationHash"

    if ($ValidationHash -eq $NewValidationHash)
    {
        Write-SNSLog "Scoutnet is not updated. No update will be done."
    }
    else
    {
        Write-SNSLog "Scoutnet is updated. Starting to update the distribution groups."
        $otherMailListsMembers, $mailListsToProcessMembers = Get-SNSExchangeMailListMember -Maillists $MailListSettings.Keys

        $allOffice365Users = Get-EXOMailbox -RecipientTypeDetails "UserMailbox" -Properties CustomAttribute1 -Verbose:$false

        foreach ($distGroupName in $MailListSettings.keys)
        {
            $AddressesToAdd = @{}
#region Synch e-mail addresses from rule list 'scouter' using scouter_synk_option.
            $distGroupId = $MailListSettings[$distGroupName].scoutnet_list_id
            Write-SNSLog " "
            Write-SNSLog "Adding Contacts in distribution group $($distGroupName)"

            $scouter_synk_option = $MailListSettings[$distGroupName].scouter_synk_option
            $scouter_synk_option = $scouter_synk_option.ToLower()

            Write-SNSLog "Scouter synk option is '$scouter_synk_option' for distribution group $distGroupName"

            # Fetch the mail addresses to add for scouter.
            $listData = $CustomLists[$distGroupId].scouter
            foreach ($member in $listData)
            {
                if ($null -eq $member)
                {
                    Write-SNSLog -Level "Warn" "Mail list $distGroupName type 'scouter' contained a null value."
                    continue
                }
                if (-not $AllMailAddresses.ContainsKey($member))
                {
                    Write-SNSLog -Level "Warn" "Id '$member' from $distGroupName not found in list of all members."
                    continue
                }

                $MemberData = $AllMailAddresses[$member]
                $displayName = "$($MemberData.first_name) $($MemberData.last_name)"

                if ($MailListSettings[$distGroupName].ignore_user)
                {
                    if ($MailListSettings[$distGroupName].ignore_user.Contains($member))
                    {
                        Write-SNSLog "Ignoring '$($displayName)' with Id '$member' for $distGroupName."
                        continue
                    }
                }

                # Get valid addresses to add based on list setting.
                $mailaddresses = [System.Collections.ArrayList]::new()
                if ($scouter_synk_option.contains("p"))
                {
                    [void]$mailaddresses.Add($MemberData.primary_email)
                }
                elseif ($scouter_synk_option.contains("a"))
                {
                    [void]$mailaddresses.Add($MemberData.alt_email)
                }
                elseif ($scouter_synk_option.contains("f"))
                {
                    $MemberData.contacts_addresses | ForEach-Object {[void]$mailaddresses.Add($_)}
                }
                else
                {
                    $MemberData.mailaddresses | ForEach-Object {[void]$mailaddresses.Add($_)}
                }
                $mailaddresses = $mailaddresses | Sort-Object -Unique

                $AddMemberOffice365Address = $False
                $AddMemberScoutnetAddress = $True
                $AddMemberOffice365AddressTryFirst = $False # First try to add office 365 address. If that fails add the scoutnet version.

                if ($scouter_synk_option.contains("@"))
                {
                    $AddMemberScoutnetAddress = $False
                    $AddMemberOffice365Address = $True
                }
                elseif ($scouter_synk_option.contains("&"))
                {
                    $AddMemberScoutnetAddress = $True
                    $AddMemberOffice365Address = $True
                }

                if ($scouter_synk_option.contains("t"))
                {
                    $AddMemberScoutnetAddress = $False
                    $AddMemberOffice365AddressTryFirst = $True
                    $AddMemberOffice365Address = $True
                }

                if ($mailaddresses.Length -eq 0)
                {
                    Write-SNSLog -Level "Warn" "No email addresses found for $($displayName)."
                    continue
                }

                $AddMemberScoutnetAddress = $AddMemberScoutnetAddress
                if ($AddMemberOffice365Address)
                {
                    $result, $PrimarySmtpAddress = Add-Office365userToList -allOffice365Users $allOffice365Users -Member $member -MemberData $AllMailAddresses[$member] -distGroupName $distGroupName
                    if ($result)
                    {
                        if ([string]::IsNullOrWhiteSpace($PrimarySmtpAddress))
                        {
                            Write-SNSLog -Level "Warn" "Primary mailaddres for '$member' is empty. Cannot add member to list."
                        }
                        else
                        {
                            $AddressesToAdd[$PrimarySmtpAddress] = $PrimarySmtpAddress
                        }
                    }
                    else
                    {
                        if ($AddMemberOffice365AddressTryFirst)
                        {
                            $AddMemberScoutnetAddress = $True
                        }
                    }
                }

                if ($AddMemberScoutnetAddress)
                {
                    $mailaddresses | ForEach-Object {
                        if (![string]::IsNullOrWhiteSpace($_))
                        {
                            $ContactCreated = $True
                            if ($PSCmdlet.ShouldProcess($_, "New-SNSMailContact"))
                            {
                                $ContactCreated = New-SNSMailContact -Epost $_ -DisplayName $displayName
                            }

                            if ($ContactCreated)
                            {
                                $AddressesToAdd[$_] = $_
                                Write-SNSLog "Adding Contact '$_' to '$distGroupName'"
                            }
                            $mailListsToProcessMembers.Remove($_)
                        }
                    }
                }
            }
#endregion

#region Synch e-mail addresses from rule list 'ledare' using ledare_synk_option.
            $ledare_synk_option = $MailListSettings[$distGroupName].ledare_synk_option
            $ledare_synk_option = $ledare_synk_option.ToLower()

            Write-SNSLog "Ledare synk option is '$ledare_synk_option' for distribution group $distGroupName"
            # Get the settings for ledare in this list.
            $AddLedareOffice365Address = $True
            $AddLedareScoutnetAddress = $False
            $AddLedareOffice365AddressTryFirst = $False # First try to add office 365 address. If that fails add the scoutnet version.

            if ($ledare_synk_option -like "-")
            {
                $AddLedareScoutnetAddress = $True
                $AddLedareOffice365Address = $False
            }
            elseif ($ledare_synk_option -like "&")
            {
                $AddLedareScoutnetAddress = $True
            }

            if ($ledare_synk_option -like "t")
            {
                $AddLedareScoutnetAddress = $False
                $AddLedareOffice365AddressTryFirst = $True
                $AddLedareOffice365Address = $True
            }

            $listData = $CustomLists[$distGroupId].ledare
            foreach ($member in $listData)
            {
                if ($null -eq $member)
                {
                    Write-SNSLog -Level "Warn" "Maillist $distGroupName contained a null value."
                    continue
                }
                if (-not $AllMailAddresses.ContainsKey($member))
                {
                    Write-SNSLog -Level "Warn" "Id '$member' from $distGroupName not found in list of all members."
                    continue
                }

                $MemberData = $AllMailAddresses[$member]
                $displayName = "$($MemberData.first_name) $($MemberData.last_name)"

                if ($MailListSettings[$distGroupName].ignore_user)
                {
                    if ($MailListSettings[$distGroupName].ignore_user.Contains($member))
                    {
                        Write-SNSLog "Ignoring '$($displayName)' with Id '$member' for $distGroupName."
                        continue
                    }
                }

                $DoAddLedareScoutnetAddress = $AddLedareScoutnetAddress
                if ($AddLedareOffice365Address)
                {
                    $result, $PrimarySmtpAddress = Add-Office365userToList -allOffice365Users $allOffice365Users -Member $member -MemberData $MemberData -distGroupName $distGroupName
                    if ($result)
                    {
                        if ([string]::IsNullOrWhiteSpace($PrimarySmtpAddress))
                        {
                            Write-SNSLog -Level "Warn" "Primary mailaddres for '$member' is empty. Cannot add member to list."
                        }
                        else
                        {
                            $AddressesToAdd[$PrimarySmtpAddress] = $PrimarySmtpAddress
                        }
                    }
                    else
                    {
                        if ($AddLedareOffice365AddressTryFirst)
                        {
                            $DoAddLedareScoutnetAddress = $True
                        }
                    }
                }

                if ($DoAddLedareScoutnetAddress)
                {
                    if ([string]::IsNullOrWhiteSpace($MemberData.primary_email))
                    {
                        Write-SNSLog -Level "Warn" "Primary mailaddres in scoutnet for '$displayName' is empty. Cannot add member to list."
                    }
                    else
                    {
                        $ContactCreated = $True
                        if ($PSCmdlet.ShouldProcess($MemberData.primary_email, "New-SNSMailContact"))
                        {
                            $ContactCreated = New-SNSMailContact -Epost $MemberData.primary_email -DisplayName $displayName
                        }

                        if ($ContactCreated)
                        {
                            Write-SNSLog "Adding Contact '$($MemberData.primary_email)' to '$distGroupName'"
                            $AddressesToAdd[$MemberData.primary_email] = $MemberData.primary_email
                        }
                        $mailListsToProcessMembers.Remove($MemberData.primary_email)
                    }
                }
            }

#endregion


#region Add all mailaddresses listed in email_addresses for the maillist.
            foreach ($email in $MailListSettings[$distGroupName].email_addresses)
            {
                if ([string]::IsNullOrWhiteSpace($email))
                {
                    continue
                }

                $ContactCreated = $True
                if ($PSCmdlet.ShouldProcess($_, "New-SNSMailContact"))
                {
                    $ContactCreated = New-SNSMailContact -Epost $_ -DisplayName $displayName
                }

                if ($ContactCreated)
                {
                    Write-SNSLog "Adding Contact '$email' to '$distGroupName'"
                    $AddressesToAdd[$email] = $email
                }
                $mailListsToProcessMembers.Remove($email)
            }
#endregion

#region Update maillist
            $UpdateDistGroup = @{
                Identity = $distGroupName
                Members =  $AddressesToAdd.Values
                ErrorAction = "stop"
                Verbose = $false
            }
            try
            {
                if ($PSCmdlet.ShouldProcess($distGroupName, "Update-DistributionGroupMember"))
                {
                    Update-DistributionGroupMember @UpdateDistGroup -Confirm:$false
                }
            }
            catch
            {
                Write-SNSLog -Level "Error" "Update of distribution group members in $distGroupName failed! Error $_"
            }
            $AddressesInMailLists[$distGroupName] = $AddressesToAdd.Values
#endregion
        }

        Write-SNSLog " "
        Write-SNSLog "Removing old contacts"
        Write-SNSLog "Number of contacts to check for removal $($mailListsToProcessMembers.count)"

        # Delete all contacts that still is in the list of contacts to process.
        $mailListsToProcessMembers.values | ForEach-Object {
            $medlem = $_
            if ($medlem.RecipientType -eq "MailContact")
            {
                # Check this contact is member in any other maillist.
                $IsInmaillistMembers = $otherMailListsMembers[$_.Identity]
                if ($null -eq $IsInmaillistMembers)
                {
                    # Not used in any maillists. Remove the contact.
                    Write-SNSLog "Removing MailContact $($medlem.Identity)"
                    Remove-MailContact $medlem.Identity -Confirm:$false -Verbose:$false
                }
            }
        }

        Write-SNSLog " "
        Write-SNSLog "Update done new hash value is $NewValidationHash"
        $OutValidationHash = $NewValidationHash
    }

    if ($ReturnMaildata)
    {
        return $OutValidationHash, $AddressesInMailLists
    }
    else
    {
        return $OutValidationHash
    }
}