forked from SeeSmitty/Powershell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Graph-SendTermFollupEmail.ps1
80 lines (69 loc) · 2.16 KB
/
Graph-SendTermFollupEmail.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
#Variables
$thumb = {"thumbprint"}
$client = {"clientID"}
$tenant = {"tenantID"}
$org = {"PrimaryDomain"}
$Certificate = Get-ChildItem Cert:\CurrentUser\My\$thumb
$GroupName = "Group Name" #Display Name of the group where termed users exist
$group = Get-MgGroup -Filter "DisplayName eq '$GroupName'" | select Id
#connection Strings
Connect-ExchangeOnline -CertificateThumbPrint $thumb -AppID $client -Organization $org
Connect-Graph -TenantId $tenant -AppId $client -Certificate $Certificate
#region functions
function Send-TermMail {
$to = $active
$from = "[email protected]"
$bcc = "[email protected]"
$subject = "Termed Employee - " + $term.DisplayName
$type = "html"
$template = Get-Content -path "C:\temp\Template.html" -raw
$params = @{
Message = @{
Subject = $subject
Body = @{
ContentType = $type
Content = $template
}
ToRecipients = @(
@{
EmailAddress = @{
Address = $to
}
}
)
BccRecipients = @(
@{
EmailAddress = @{
Address = $bcc
}
}
)
}
SaveToSentItems = "true"
}
Send-MgUserMail -UserId $from -BodyParameter $params
}
function Get-DelegateAccess {
#Function is used to get the members of the termed users group
$termedUsers = Get-MgGroupMember -GroupId $group.Id #gets the members of the Termed Users group
$delegateAccess = @()
foreach ($tu in $termedUsers) {
$tu2 = Get-MgUser -UserId $tu.id | select UserPrincipalName
$m2 = Get-EXOMailboxPermission -Identity $tu2.userPrincipalName | Where-Object {($_.User -notlike "NT*") -and ($_.AccessRights -eq "FullAccess")} | select Identity,User
$delegateAccess += $m2
}
}
function Send-FollowUp {
foreach($person in $delegateAccess){
$term = Get-EXOMailbox -Identity $person.identity | select DisplayName,userPrincipalName #former employee
$active = $person.user #manager with delgate access
Send-TermMail
}
}
#endregion functions
#gather Delegate access details
Get-DelegateAccess
#Send the follow up email to the appropriate people
Send-FollowUp
Disconnect-ExchangeOnline -Confirm:$false
Disconnect-Graph