forked from timmcmic/DLConversionV2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
start-collectOffice365MailboxFolders.ps1
416 lines (303 loc) · 15.5 KB
/
start-collectOffice365MailboxFolders.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
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
function start-collectOffice365MailboxFolders
{
<#
.SYNOPSIS
This function collects all of the mailbox permissions for folders in office 365 mailboxes.
.DESCRIPTION
Trigger function.
.PARAMETER logFolder
*REQUIRED*
The location where logging for the migration should occur including all XML outputs for backups.
.PARAMETER exchangeCredential
*REQUIRED IF HYBRID MAIL FLOW ENABLED*
This is the credential utilized to establish remote powershell sessions to Exchange on-premises.
This acccount requires Exchange Organization Management rights in order to enable hybrid mail flow.
.OUTPUTS
Logs all activities and backs up all original data to the log folder directory.
Moves the distribution group from on premieses source of authority to office 365 source of authority.
.EXAMPLE
Start-collectOnPremFolderPermissions -exchangeServer Server -exchangeCredential $credential
#>
#Portions of the audit code adapted from Tony Redmon's project.
#https://github.com/12Knocksinna/Office365itpros/blob/master/ReportPermissionsFolderLevel.PS1
#Don't tell him - he can get grumpy at times.
[cmdletbinding()]
Param
(
[Parameter(Mandatory = $true)]
[string]$logFolderPath,
[Parameter(Mandatory = $false)]
[pscredential]$exchangeOnlineCredential=$NULL,
[Parameter(Mandatory = $false)]
[string]$exchangeOnlineCertificateThumbPrint="",
[Parameter(Mandatory = $false)]
[string]$exchangeOnlineOrganizationName="",
[Parameter(Mandatory = $false)]
[ValidateSet("O365Default","O365GermanyCloud","O365China","O365USGovGCCHigh","O365USGovDoD")]
[string]$exchangeOnlineEnvironmentName="O365Default",
[Parameter(Mandatory = $false)]
[string]$exchangeOnlineAppID="",
[Parameter(Mandatory = $false)]
[boolean]$retryCollection=$FALSE,
[Parameter(Mandatory = $false)]
$bringMyOwnMailboxes=$NULL
)
#Delare global variables.
$global:logFile=$NULL #This is the global variable for the calculated log file name
[string]$global:staticFolderName="\AuditData\"
#Declare function variables.
$auditMailboxes=$NULL
$auditFolders=$NULL
[array]$auditFolderNames=@()
[array]$auditFolderPermissions=@()
[int]$forCounter=0
[int]$mailboxCounter=0
[int]$totalMailboxes=0
[string]$office365MailboxFolderPermissions="office365MailboxFolderPermissions.xml"
[string]$office365MailboxList="office365MailboxList.xml"
[string]$office365MailboxProcessed="office365MailboxProcessed.xml"
[int]$auditPermissionsFound=0
new-LogFile -groupSMTPAddress Office365MailboxFolderPermissions -logFolderPath $logFolderPath
#Validate that only one method of engaging exchange online was specified.
Out-LogFile -string "Validating Exchange Online Credentials."
if (($exchangeOnlineCredential -ne $NULL) -and ($exchangeOnlineCertificateThumbPrint -ne ""))
{
Out-LogFile -string "ERROR: Only one method of cloud authentication can be specified. Use either cloud credentials or cloud certificate thumbprint." -isError:$TRUE
}
elseif (($exchangeOnlineCredential -eq $NULL) -and ($exchangeOnlineCertificateThumbPrint -eq ""))
{
out-logfile -string "ERROR: One permissions method to connect to Exchange Online must be specified." -isError:$TRUE
}
else
{
Out-LogFile -string "Only one method of Exchange Online authentication specified."
}
#Validate that all information for the certificate connection has been provieed.
if (($exchangeOnlineCertificateThumbPrint -ne "") -and ($exchangeOnlineOrganizationName -eq "") -and ($exchangeOnlineAppID -eq ""))
{
out-logfile -string "The exchange organiztion name and application ID are required when using certificate thumbprint authentication to Exchange Online." -isError:$TRUE
}
elseif (($exchangeOnlineCertificateThumbPrint -ne "") -and ($exchangeOnlineOrganizationName -ne "") -and ($exchangeOnlineAppID -eq ""))
{
out-logfile -string "The exchange application ID is required when using certificate thumbprint authentication." -isError:$TRUE
}
elseif (($exchangeOnlineCertificateThumbPrint -ne "") -and ($exchangeOnlineOrganizationName -eq "") -and ($exchangeOnlineAppID -ne ""))
{
out-logfile -string "The exchange organization name is required when using certificate thumbprint authentication." -isError:$TRUE
}
else
{
out-logfile -string "All components necessary for Exchange certificate thumbprint authentication were specified."
}
#Ensure bring your own mailboes not included with retry.
if (($bringMyOwnMailboxes -ne $NULL)-and ($retryCollection -EQ $TRUE))
{
out-logfile -string "Cannot combine bring your own mailboxes with retry collection."
out-logfile -string "If this is a retry after bringning your own mailbox - specify just retry." -isError:$TRUE -isAudit:$true
}
#Start the connection to Exchange Online.
if ($exchangeOnlineCredential -ne $NULL)
{
#User specified non-certifate authentication credentials.
try {
New-ExchangeOnlinePowershellSession -exchangeOnlineCredentials $exchangeOnlineCredential -exchangeOnlineEnvironmentName $exchangeOnlineEnvironmentName
}
catch {
out-logfile -string "Unable to create the exchange online connection using credentials."
out-logfile -string $_ -isError:$TRUE
}
}
elseif ($exchangeOnlineCertificateThumbPrint -ne "")
{
#User specified thumbprint authentication.
try {
new-ExchangeOnlinePowershellSession -exchangeOnlineCertificateThumbPrint $exchangeOnlineCertificateThumbPrint -exchangeOnlineAppId $exchangeOnlineAppID -exchangeOnlineOrganizationName $exchangeOnlineOrganizationName -exchangeOnlineEnvironmentName $exchangeOnlineEnvironmentName
}
catch {
out-logfile -string "Unable to create the exchange online connection using certificate."
out-logfile -string $_ -isError:$TRUE
}
}
#Define the log file path one time.
$logFolderPath = $logFolderPath+$global:staticFolderName
try
{
if ($retryCollection -eq $FALSE)
{
if ($bringMyOwnMailboxes -eq $NULL)
{
out-logFile -string "Obtaining all office 365 mailboxes."
$auditMailboxes = get-exomailbox -resultsize unlimited
#Exporting mailbox operations to csv - the goal here will be to allow retry.
$fileName = $office365MailboxList
$exportFile=Join-path $logFolderPath $fileName
$auditMailboxes | export-clixml -path $exportFile
}
else
{
out-logfile -string "Using the mailboxes that the administrator provided."
out-logfile -string "Following the same logic as our get so that the retry file aligns if necessary."
$auditMailboxes = $bringMyOwnMailboxes
#Exporting mailbox operations to csv - the goal here will be to allow retry.
$fileName = $office365MailboxList
$exportFile=Join-path $logFolderPath $fileName
$auditMailboxes | export-clixml -path $exportFile
}
}
elseif ($retryCollection -eq $TRUE)
{
out-logfile -string "Retry operation - importing the mailboxes from previous export."
try{
$fileName = $office365MailboxList
$importFile=Join-path $logFolderPath $fileName
$auditMailboxes = import-clixml -path $importFile
}
catch{
out-logfile -string "Retry was specified - unable to import the XML file."
out-logfile -string $_ -isError:$TRUE -isAudit:$true
}
out-logfile -string "Import the count of the last mailbox processed."
try {
$fileName = $office365MailboxProcessed
$importFile=Join-path $logFolderPath $fileName
$mailboxCounter=Import-Clixml -path $importFile
#The import represents the last mailbox processed.
#It's permissions were already exported - add 1 to start with the next mailbox in the list.
$mailboxCounter=$mailboxCounter+1
out-logfile -string ("Next mailbox to process = "+$mailboxCounter.toString())
}
catch {
out-logfile -string "Unable to read the previous mailbox processed."
out-logfile -string $_ -isError:$TRUE -isAudit:$true
}
out-logfile -string "Importing the previously exported permissions."
try {
$fileName=$office365MailboxFolderPermissions
$importFile=Join-path $logFolderPath $fileName
$auditFolderPermissions = import-clixml -Path $importFile
}
catch {
out-logfile -string "Unable to import the previously exported permissions."
}
}
}
catch
{
out-logFile -string "Unable to get mailboxes."
out-logfile -string $_ -isError:$TRUE
}
#For each mailbox - we will iterate and grab the folders for processing.
out-logfile -string "Gathering mailbox folders for assessment."
$ProgressDelta = 100/($auditMailboxes.count); $PercentComplete = 0; $MbxNumber = 0
$totalMailboxes=$auditMailboxes.count
#Here we're going to use a for loop based on count.
#This is to support a retry operation.
for ($mailboxCounter ; $mailboxCounter -lt $totalMailboxes ; $mailboxCounter++)
{
#Drop the mailbox into a working variable.
$mailbox = $auditMailboxes[$mailboxCounter]
if ($forCounter -gt 1000)
{
out-logfile -string "Sleeping for 5 seconds - powershell refresh."
start-sleep -seconds 5
$forCounter=0
}
else
{
$forCounter++
}
out-logfile -string ("Processing mailbox = "+$mailbox.primarySMTPAddress)
out-logfile -string ("Processing mailbox number: "+$mailboxCounter.toString())
$MbxNumber++
$progressString = "Mailbox Name: "+$mailbox.primarySMTPAddress+" Mailbox Number: "+$mailboxCounter
Write-Progress -Activity "Processing mailbox" -Status $progressString -PercentComplete $PercentComplete -Id 1
$PercentComplete += $ProgressDelta
try {
$auditFolders=get-exomailboxFolderStatistics -identity $mailbox.identity | where {$_.FolderType -eq "User Created" -or $_.FolderType -eq "Inbox" -or $_.FolderType -eq "SentItems" -or $_.FolderType -eq "Contacts" -or $_.FolderType -eq "Calendar"}
}
catch {
out-logfile -string "Error obtaining folder statistics."
out-logfile -string $_ -isError:$TRUE
}
#Audit folders have been obtained.
#For each folder - normalize the folder names.
$ProgressDeltaFolders = 100/($auditFolders.count); $PercentCompleteFolders = 0; $FolderNumber = 0
foreach ($folder in $auditFolders)
{
out-logFile -string ("Processing folder name ="+$folder.Identity)
out-logfile -string ("Processing folder = "+$folder.FolderId)
out-logfile -string ("Processing cotent mailbox guid = "+$folder.ContentMailboxGuid)
$folderNumber++
Write-Progress -Activity "Processing folder" -Status $folder.identity -PercentComplete $PercentCompleteFolders -id 2 -ParentId 1
$PercentCompleteFolders += $ProgressDeltaFolders
$tempFolderName=$folder.ContentMailboxGuid.tostring()+":"+$folder.FolderId.tostring()
#start-sleep -Seconds 5 #Debug sleep to watch status bar.
out-logfile -string ("Temp folder name = "+$tempFolderName)
$auditFolderNames+=$tempFolderName
}
write-progress -activity "Processing Folders" -ParentId 1 -Id 2 -Completed
#At this time the folder names have been normalized to mailbox ID and folder ID - this allows us to store them in an object later.
out-logfile -string "Obtaining any custom folder permissions that are not default or anonymous."
$ProgressDeltaAuditNames = 100/($auditFolderNames.count); $PercentCompleteAuditNames = 0; $FolderNameCount = 0
foreach ($folderName in $auditFolderNames)
{
if ($forCounter -gt 1000)
{
out-logfile -string "Sleeping for 5 seconds - powershell refresh."
start-sleep -seconds 5
$forCounter=0
}
else
{
$forCounter++
}
out-logfile -string ("Obtaining permissions on the following folder = "+$folderName)
$FolderNameCount++
Write-Progress -Activity "Processing folder" -Status $folderName -PercentComplete $PercentCompleteAuditNames -parentid 1 -Id 2
$PercentCompleteAuditNames += $ProgressDeltaAuditNames
try {
$forPermissions = Get-exomailboxFolderPermission -Identity $FolderName -ErrorAction Stop
}
catch {
out-logfile -string "Unable to obtain folder permissions."
out-logfile -string $_ -isError:$TRUE
}
#Check the permissions found to see if they meet the criteria.
$ProgressDeltaPermissions = 100/($forPermissions.count); $PercentCompletePermissions = 0; $permissionNumber = 0
foreach ($permission in $forPermissions)
{
$forUser = $Permission.User.tostring()
out-logfile -string ("Found User = "+$forUser)
$forNumberr++
Write-Progress -Activity "Processing permission" -Status $permission.identity -PercentComplete $PercentCompletePermissions -parentID 2 -id 3
$PercentCompletePermissions += $ProgressDeltaPermissions
#start-sleep -seconds 5 #Debug sleep to watch status bar.
if (($forUser -ne "Default") -and ($foruser -ne "Anonymous") -and ($foruser -notLike "NT:S-1-5-21*"))
{
out-logfile -string ("Not default or anonymous permission = "+$permission.user)
$forPermissionObject = New-Object PSObject -Property @{
identity = $permission.identity
folderName = $permission.folderName
user = $permission.user
accessRights = $permission.accessRights
}
out-logfile -string $forPermissionObject
$auditFolderPermissions+=$forPermissionObject
$auditPermissionsFound = 1
}
}
write-progress -activity 'Processing permissions' -ParentId 2 -id 3 -Completed
}
write-progress "Processing folders" -ParentId 1 -id 2 -Completed
#At this time write out the permissions.
$fileName = $office365MailboxFolderPermissions
$exportFile=Join-path $logFolderPath $fileName
$auditFolderPermissions | Export-Clixml -Path $exportFile
$fileName = $office365MailboxProcessed
$exportFile=Join-path $logFolderPath $fileName
$mailboxCounter | export-clixml -path $exportFile
#Null out all the arrays for the next run except mailboxes.
$auditFolderNames=@()
$auditFolders=@()
}
}