-
Notifications
You must be signed in to change notification settings - Fork 1
/
Set-ExchangeAutomaticReplies.ps1
237 lines (234 loc) · 6.18 KB
/
Set-ExchangeAutomaticReplies.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
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
[OutputType([string])]
param ([Parameter(Mandatory=$true)]
[string]$credentialName,
[Parameter(Mandatory=$true)]
[string]$directoryName,
[Parameter(Mandatory=$true)]
[string]$applicationID,
[Parameter(Mandatory=$true)]
[string]$subscriptionID,
[Parameter(Mandatory=$true)]
[string]$keyVaultName,
[Parameter(Mandatory=$true)]
[string]$certificateName,
[Parameter(Mandatory=$true)]
[string]$mailSource,
[Parameter(Mandatory=$true)]
[string]$mailTarget,
[Parameter(Mandatory=$true)]
[string]$mailBody)
# Connect to Azure
Connect-AzAccount -Identity -Subscription $subscriptionID | Out-Null
# Get certificate from Azure
$azCertSecret = Get-AzKeyVaultSecret -VaultName $keyVaultName -Name $certificateName -AsPlainText
$azCertSecretByte = [Convert]::FromBase64String($azCertSecret)
$x509Cert = [System.Security.Cryptography.X509Certificates.X509Certificate2]::new($azCertSecretByte)
# Connect to Azure AD, needs rework for certificate authentication once available
Connect-AzureAD -Credential (Get-AutomationPSCredential -Name $credentialName) | Out-Null
# Connect to Exchange Online
Connect-ExchangeOnline -Certificate $x509Cert -AppID $applicationID -Organization $directoryName -CommandName 'Get-MailboxFolderPermission','Get-MailboxFolderStatistics','Set-MailboxAutoReplyConfiguration' | Out-Null
#region: Input
if ($mailTarget.Contains('@'))
{
$checkEmail = $true
# Check manager
if ((@((Get-AzureADUser -Filter "Mail eq '$mailTarget'" | Get-AzureADUserManager).UserPrincipalName) | Select-Object -Unique) -contains $mailSource)
{
$checkSuperordinate = $true
}
else
{
$checkSuperordinate = $false
}
# Check coworker
if ((@((Get-AzureADUser -Filter "Mail eq '$mailTarget'" | Get-AzureADUserManager | Get-AzureADUserDirectReport).UserPrincipalName) | Select-Object -Unique) -contains $mailSource)
{
$checkCoordinate = $true
}
else
{
$checkCoordinate = $false
}
# Check direct reports
if ((@((Get-AzureADUser -Filter "Mail eq '$mailTarget'" | Get-AzureADUserDirectReport).UserPrincipalName) | Select-Object -Unique) -contains $mailSource)
{
$checkSubordinate = $true
}
else
{
$checkSubordinate = $false
}
# Check editors
if ((Get-AzureADUser -Filter "UserPrincipalName eq '$mailSource'").DisplayName -in ((@((Get-MailboxFolderStatistics -Identity $mailTarget -FolderScope Calendar | Where-Object{$_.FolderType -eq 'Calendar'}), (Get-MailboxFolderStatistics -Identity $mailTarget -FolderScope Inbox | Where-Object{$_.FolderType -eq 'Inbox'})) | ForEach-Object{Get-MailboxFolderPermission $_.Identity.replace('\',':\')} | Where-Object{$_.AccessRights -in @('Editor','PublishingEditor','Owner')}).User.DisplayName | Select-Object -Unique))
{
$checkPermission = $true
}
else
{
$checkPermission = $false
}
# Check results
if ($checkSuperordinate -or $checkCoordinate -or $checkSubordinate -or $checkPermission)
{
$text = "<html><body><div style='font-size: 11pt; font-family: Arial'>$mailBody</div></body></html>"
Set-MailboxAutoReplyConfiguration -Identity $mailTarget -AutoReplyState 'Enabled' -InternalMessage $text -ExternalMessage $text -ExternalAudience 'All'
}
}
else
{
$checkEmail = $false
}
#endregion
#region: Output
$output = [System.Text.StringBuilder]::new()
$output.AppendLine(@"
<div>
<style>
table, th, td {
border: none;
border-collapse: collapse;
}
th, td {
padding: 5px;
text-align: left;
vertical-align: top;
}
.green {
border-left: 4pt solid darkgreen;
padding-left: 4pt;
background-color: lightgreen
}
.yellow {
border-left: 4pt solid darkgoldenrod;
padding-left: 4pt;
background-color: lightgoldenrodyellow
}
.red {
border-left: 4pt solid darkred;
padding-left: 4pt;
background-color: lightcoral
}
</style>
<table>
<tr>
<th>Check</th>
<th>Result</th>
</tr>
<tr>
<td>Requester provided an email address</td>
"@) | Out-Null
if ($checkEmail)
{
$output.AppendLine('<td class="green">Yes</td>') | Out-Null
}
else
{
$output.AppendLine('<td class="red">No</td>') | Out-Null
}
$output.AppendLine(@"
</tr>
<tr>
<td>Requester is the manager</td>
"@) | Out-Null
if ($checkEmail)
{
if ($checkSuperordinate)
{
$output.AppendLine('<td class="green">Yes</td>') | Out-Null
}
else
{
$output.AppendLine('<td class="yellow">No</td>') | Out-Null
}
}
else
{
$output.AppendLine('<td class="yellow">Skipped</td>') | Out-Null
}
$output.AppendLine(@"
</tr>
<tr>
<td>Requester is a coworker</td>
"@) | Out-Null
if ($checkEmail)
{
if ($checkCoordinate)
{
$output.AppendLine('<td class="green">Yes</td>') | Out-Null
}
else
{
$output.AppendLine('<td class="yellow">No</td>') | Out-Null
}
}
else
{
$output.AppendLine('<td class="yellow">Skipped</td>') | Out-Null
}
$output.AppendLine(@"
</tr>
<tr>
<td>Requester is a direct report</td>
"@) | Out-Null
if ($checkEmail)
{
if ($checkSubordinate)
{
$output.AppendLine('<td class="green">Yes</td>') | Out-Null
}
else
{
$output.AppendLine('<td class="yellow">No</td>') | Out-Null
}
}
else
{
$output.AppendLine('<td class="yellow">Skipped</td>') | Out-Null
}
$output.AppendLine(@"
</tr>
<tr>
<td>Requester has inbox/calendar permission</td>
"@) | Out-Null
if ($checkEmail)
{
if ($checkPermission)
{
$output.AppendLine('<td class="green">Yes</td>') | Out-Null
}
else
{
$output.AppendLine('<td class="yellow">No</td>') | Out-Null
}
}
else
{
$output.AppendLine('<td class="yellow">Skipped</td>') | Out-Null
}
$output.AppendLine(@"
</tr>
<tr>
<td>Configure automatic replies</td>
"@) | Out-Null
if ($checkEmail)
{
if ($checkSuperordinate -or $checkCoordinate -or $checkSubordinate -or $checkPermission)
{
$output.AppendLine('<td class="green">Granted</td>') | Out-Null
}
else
{
$output.AppendLine('<td class="red">Denied</td>') | Out-Null
}
}
else
{
$output.AppendLine('<td class="yellow">Skipped</td>') | Out-Null
}
$output.AppendLine(@"
</tr>
</table>
</div>
"@) | Out-Null
$output.ToString() | Write-Output
#endregion