-
Notifications
You must be signed in to change notification settings - Fork 0
/
Find-MailboxDelegates.ps1
1067 lines (943 loc) · 62 KB
/
Find-MailboxDelegates.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
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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<#
.DESCRIPTION
###############Disclaimer#####################################################
The sample scripts are not supported under any Microsoft standard support
program or service. The sample scripts are provided AS IS without warranty
of any kind. Microsoft further disclaims all implied warranties including,
without limitation, any implied warranties of merchantability or of fitness for
a particular purpose. The entire risk arising out of the use or performance of
the sample scripts and documentation remains with you. In no event shall
Microsoft, its authors, or anyone else involved in the creation, production, or
delivery of the scripts be liable for any damages whatsoever (including,
without limitation, damages for loss of business profits, business interruption,
loss of business information, or other pecuniary loss) arising out of the use
of or inability to use the sample scripts or documentation, even if Microsoft
has been advised of the possibility of such damages.
###############Disclaimer#####################################################
We have developed this script because cross premises permissions are not supported with Exchange Hybrid environments: https://technet.microsoft.com/en-us/library/jj906433(v=exchg.150).aspx.
With this script you can export Exchange 2010/2013 on premises permissions, find their associated delegates, and produce a report of mailboxes with their recommended batch to minimize impact to those users.
Requirement: Active Directory Module
Steps performed by the script:
1)Collect permissions
2)Find batches based on the output permissions
3)Create Migration schedule (this is built in the format required by the Microsoft FastTrack Mail Migration team).
*For extra large environments with many mailboxes, you may consider running multiple instances of the script. For example:
1)Create multiple csv files that has different emails each. The number of csv files depends on the number of powershell sessions you will have going in parallel.
2)Spin up multiple powershell sessions and run the script pointed at different InputMailboxesCSV files
3)Merge the permissions output files from each script into one singular permissions file
4)Run one of the scripts with the -BatchUsers - this will bypass collecting permissions and jump straight into batching users using the permissinos output in the same directory as the script
=========================================
Version:
12062019: Add Permissions Only switch to skip steps 2 & 3
08232019: AccountResourceEnv switch add
05012019: Update cross domain check
06262018: Update group enumeration cross domain logic
06122018: Update group enumeration logic
Authors:
Alejandro Lopez - [email protected]
Sam Portelli - [email protected]
Contributors:
Francesco Poli - [email protected]
=========================================
.PARAMETER InputMailboxesCSV
Use this parameter to specify a list of users to collect permissions for, rather than all mailboxes.
Make sure that the CSV file provided has a header titled "PrimarySMTPAddress"
.PARAMETER ExcludeServiceAcctsCSV
In cases where you have service accounts with permissions to a large number of mailboxes, e.g. Blackberry service accounts, you can use this to exclude those accounts from the batching processing.
Provide the path to a csv file (no header needed) with each service account primarySMTPaddress on its own line.
*This will slow down processing.
.PARAMETER FullAccess
Collect Full Access permissions. Keep in mind that "Full Access" permissions are now supported in cross premises scenarios. Not including "Full Access" will speed up processing.
.PARAMETER SendOnBehalfTo
Collect SendOnBehalfTo permissions
.PARAMETER Calendar
Collect calendar permissions
.PARAMETER SendAs
Collect Send As permissions
.PARAMETER EnumerateGroups
This will enumerate groups that have permissions to mailboxes and include in the batching logic.
*This will slow down processing.
.PARAMETER ExcludeGroupsCSV
Use this to exclude groups that you don't want to enumerate. Provide the path to a csv file (no header needed) with each group name on its own line.
.PARAMETER ExchServerFQDN
Connect to a specific Exchange Server
.PARAMETER Resume
Use this to resume the script in case of a failure while running the script on a large number of users. This way you don't have to start all over.
The way this works is that it will look for the progress xml file where it keeps track of mailboxes that are pending processing.
Make sure not to use in conjunction with the InputMailboxesCSV switch.
.PARAMETER BatchUsers
Use this if you want to skip collecting permissions and only run Step 2 and Step 3.
Make sure you have the permissions output file in the same directory (Find-MailboxDelegates-Permissions.csv).
.PARAMETER BatchUsersOnly
Use this if you want to skip collecting permissions (step1) and creating a migration schedule (step 3). This won't require an active exchange session, but make sure you have the permissions output file in the same directory (Find-MailboxDelegates-Permissions.csv).
.PARAMETER GetPermissionsOnly
Use this switch to run Step 1 only (Get Permissions). This will skip steps 2&3 which does the spider web batching and creates a migration schedule.
.PARAMETER AccountResourceEnv
Switch to run the script taking into account an Account/Resource environment
.EXAMPLE
#Export only SendOnBehalfTo and Send As permissions and Enumerate Groups for all mailboxes.
.\Find-MailboxDelegates.ps1 -SendOnBehalfTo -SendAs -EnumerateGroups
.EXAMPLE
#Export only Full Access and Send As permissions and Enumerate Groups for the provided user list. Make sure to use "PrimarySMTPAddress" as header.
.\Find-MailboxDelegates.ps1 -InputMailboxesCSV "C:\Users\administrator\Desktop\userlist.csv" -FullAccess -SendAs -EnumerateGroups
.EXAMPLE
#Resume the script after a script interruption and failure to pick up on where it left off. Make sure to include the same switches as before EXCEPT the InputMailboxesCSV otherwise it'll yell at you
.\Find-MailboxDelegates.ps1 -FullAccess -SendAs -EnumerateGroups -Resume
.EXAMPLE
#Export all permissions and enumerate groups for all mailboxes
.\Find-MailboxDelegates.ps1 -FullAccess -SendOnBehalfTo -SendAs -Calendar -EnumerateGroups
.EXAMPLE
#Export all permissions but don't enumerate groups for all mailboxes
.\Find-MailboxDelegates.ps1 -FullAccess -SendOnBehalfTo -SendAs -Calendar
.EXAMPLE
#Export all permissions and exclude service accounts for all mailboxes
.\Find-MailboxDelegates.ps1 -FullAccess -SendOnBehalfTo -SendAs -Calendar -ExcludeServiceAcctsCSV "c:\serviceaccts.csv"
.EXAMPLE
#Export all permissions and exclude service accounts for all mailboxes
.\Find-MailboxDelegates.ps1 -FullAccess -SendOnBehalfTo -SendAs -Calendar -ExcludeServiceAcctsCSV "c:\serviceaccts.csv" -ExcludeGroupsCSV "c:\groups.csv"
.EXAMPLE
#Skip collect permissions (assumes you already have a permissions output file) and only run Step 2 to batch users
.\Find-MailboxDelegates.ps1 -BatchUsersOnly
.EXAMPLE
#Skip collect permissions (assumes you already have a permissions output file) and only run Step 2 and 3 to batch users and creation migration schedule file
.\Find-MailboxDelegates.ps1 -BatchUsers
#>
param(
[string]$InputMailboxesCSV,
[switch]$FullAccess,
[switch]$SendOnBehalfTo,
[switch]$Calendar,
[switch]$SendAs,
[switch]$EnumerateGroups,
[string]$ExcludeServiceAcctsCSV,
[string]$ExcludeGroupsCSV,
[string]$ExchServerFQDN,
[switch]$Resume,
[switch]$BatchUsers,
[switch]$BatchUsersOnly,
[switch]$GetPermissionsOnly,
[switch]$AccountResourceEnv
)
Begin{
try{
#region functions
Function Write-LogEntry {
param(
[string] $LogName ,
[string] $LogEntryText,
[string] $ForegroundColor
)
if ($LogName -NotLike $Null) {
# log the date and time in the text file along with the data passed
"$([DateTime]::Now.ToShortDateString()) $([DateTime]::Now.ToShortTimeString()) : $LogEntryText" | Out-File -FilePath $LogName -append;
if ($ForeGroundColor -NotLike $null) {
# for testing i pass the ForegroundColor parameter to act as a switch to also write to the shell console
write-host $LogEntryText -ForegroundColor $ForeGroundColor
}
}
}
<# Get-RecipientCustom
Perform a fail safe Get-Recipient to handle resource forest model where the users permissions are assigned to domain\user in the account
forest, that is not resolvable in the Resource domain. Upon get-recipient failure it will try to find a mailbox with the linkedMasterAccount
associate, and use it as reference for batch grouping
#>
Function Get-RecipientCustom{
param(
[string]$recipient
)
$error.Clear()
try
{
$del=Get-Recipient -Identity $recipient -ErrorAction stop
}
catch
{
if ( $error[0].Exception.ToString() -like "*The operation couldn't be performed because object*couldn't be found on*")
{
$external = $Script:mailboxesLookup | where {$_.linkedmasteraccount -eq $recipient}
if ($external) {
$del = Get-Recipient -Identity $external.identity.tostring() -ErrorAction silentlyContinue
if ($del)
{
$error.Clear()
return $del
}
else
{
Write-LogEntry -LogName:$Script:LogFile -LogEntryText "Found external linked account $recipient associated to $($external.identity) mailbox, but cannot get the Recipient property"
return $null
}
}
Else
{
Write-LogEntry -LogName:$Script:LogFile -LogEntryText "Unable to find Mailbox with LinkedMasterAccount associated to $recipient"
return $null
}
}
}
return $del
}
Function Get-GroupCustom{
param(
[string]$Identity
)
$error.Clear()
try
{
#$del=Get-Group -Identity $group -ErrorAction stop
$group = Get-Group -identity $Identity -ErrorAction SilentlyContinue
}
catch
{
if ( $error[0].Exception.ToString() -like "*The operation couldn't be performed because object*couldn't be found on*")
{
$domain = $Identity.split("\")[0]
$remoteDC = Get-ADDomainController -discover -domain $domain
try{
$group = get-group -identity $Identity -DomainController $remoteDC.hostname -erroraction silentlyContinue
return $group
}
catch{
return $null
}
}
}
return $group
}
Function Get-Permissions(){
param(
[string]$UserEmail,
[bool]$gatherfullaccess,
[bool]$gatherSendOnBehalfTo,
[bool]$gathercalendar,
[bool]$gathersendas,
[bool]$EnumerateGroups,
[string[]]$ExcludedGroups,
[string[]]$ExcludedServiceAccts
)
try{
#Variables
Write-LogEntry -LogName:$Script:LogFile -LogEntryText "Get Permissions for: $($UserEmail)"
$CollectPermissions = New-Object System.Collections.Generic.List[System.Object]
$Error.Clear()
$Mailbox = Get-mailbox $UserEmail -EA SilentlyContinue
If(!$Mailbox){
throw "Problem getting mailbox for $($UserEmail) : $($error)"
}
$globalCatalog = ([System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest().GlobalCatalogs | Select-Object -First 1 -ExpandProperty Name) + ":3268"
#Enumerate Groups/Send As - moving this part outside of the function for faster processing
<#
If(($EnumerateGroups -eq $true) -or ($gathersendas -eq $true)){
$dse = [ADSI]"LDAP://Rootdse"
$ext = [ADSI]("LDAP://CN=Extended-Rights," + $dse.ConfigurationNamingContext)
$dn = [ADSI]"LDAP://$($dse.DefaultNamingContext)"
$dsLookFor = new-object System.DirectoryServices.DirectorySearcher($dn)
$permission = "Send As"
$right = $ext.psbase.Children | ? { $_.DisplayName -eq $permission }
}
#>
If($gathercalendar -eq $true){
$Error.Clear()
$CalendarPermission = Get-MailboxFolderPermission -Identity ($Mailbox.alias + ':\Calendar') -WarningAction SilentlyContinue -ErrorAction SilentlyContinue | ?{$_.User -notlike "Anonymous" -and $_.User -notlike "Default"} | Select User, AccessRights
if (!$CalendarPermission){
$Calendar = (($Mailbox.PrimarySmtpAddress.ToString())+ ":\" + (Get-MailboxFolderStatistics -Identity $Mailbox.DistinguishedName -WarningAction SilentlyContinue -ErrorAction SilentlyContinue | where-object {$_.FolderType -eq "Calendar"} | Select-Object -First 1).Name)
$CalendarPermission = Get-MailboxFolderPermission -Identity $Calendar -WarningAction SilentlyContinue -ErrorAction SilentlyContinue | ?{$_.User -notlike "Anonymous" -and $_.User -notlike "Default"} | Select User, AccessRights
}
If($CalendarPermission){
Foreach($perm in $CalendarPermission){
#$ifGroup = Get-Group -identity $perm.user.tostring() -ErrorAction SilentlyContinue
$ifGroup = Get-GroupCustom -identity $perm.user.tostring() -ErrorAction SilentlyContinue
If($ifGroup){
If($EnumerateGroups -eq $true){
If(-not ($excludedGroups -contains $ifGroup.Name)){
$groupDomainName = $ifgroup.identity.tostring().split("/")[0]
Write-LogEntry -LogName:$Script:LogFile -LogEntryText "Found permission : Calendar : Enumerate Group $($ifGroup.distinguishedName) Domain Name: $groupDomainName"
#$lstUsr = Get-AdGroup -identity $ifGroup.Name -Server $groupDomainName | Get-ADGroupMember -Recursive | Get-ADUser -Properties Mail
$lstUsr = Get-ADGroupMember -identity $ifGroup.distinguishedName -server $groupDomainName -Recursive | Get-ADUser -Properties Mail | ?{$_.Mail}
foreach ($usrTmp in $lstUsr) {
$usrTmpEmail = $usrTmp.Mail
If($ExcludedServiceAccts){
if(-not ($ExcludedServiceAccts -contains $usrTmpEmail -or $ExcludedServiceAccts -contains $mailbox.primarySMTPAddress.ToString())){
Write-LogEntry -LogName:$Script:LogFile -LogEntryText "Found permission : CalendarFolder : $($usrTmpEmail)"
$CollectPermissions.add([pscustomobject]@{Mailbox = $Mailbox.PrimarySMTPAddress; User = $usrTmpEmail; AccessRights = "Calendar Folder"})
}
}
Else{
Write-LogEntry -LogName:$Script:LogFile -LogEntryText "Found permission : CalendarFolder : $($usrTmpEmail)"
$CollectPermissions.add([pscustomobject]@{Mailbox = $Mailbox.PrimarySMTPAddress; User = $usrTmpEmail; AccessRights = "Calendar Folder"})
}
}
}
}
}
Else{
If($perm.user.adrecipient.primarysmtpaddress -ne $null){
$delegate = Get-RecipientCustom $perm.user.adrecipient.primarysmtpaddress.tostring().replace(":\Calendar","")
If($mailbox.primarySMTPAddress -and $delegate.primarySMTPAddress){
If(-not ($mailbox.primarySMTPAddress.ToString() -eq $delegate.primarySMTPAddress.ToString())){
If($ExcludedServiceAccts){
if(-not ($ExcludedServiceAccts -contains $delegate.primarySMTPAddress.tostring() -or $ExcludedServiceAccts -contains $mailbox.primarySMTPAddress.ToString())){
Write-LogEntry -LogName:$Script:LogFile -LogEntryText "Found permission : CalendarFolder : $($delegate.primarySMTPAddress.ToString())"
$CollectPermissions.add([pscustomobject]@{Mailbox = $Mailbox.PrimarySMTPAddress; User = $delegate.primarySMTPAddress.ToString(); AccessRights = "Calendar Folder"})
}
}
Else{
Write-LogEntry -LogName:$Script:LogFile -LogEntryText "Found permission : CalendarFolder : $($delegate.primarySMTPAddress.ToString())"
$CollectPermissions.add([pscustomobject]@{Mailbox = $Mailbox.PrimarySMTPAddress; User = $delegate.primarySMTPAddress.ToString(); AccessRights = "Calendar Folder"})
}
}
}
}
}
}
}
If($Error){
Write-LogEntry -LogName:$Script:LogFile -LogEntryText "MBX=$($Mailbox.PrimarySMTPAddress) PERM=CalendarFolder ERROR=$($error[0].ToString()) POSITION=$($error[0].InvocationInfo.PositionMessage)"
}
}
If($gatherfullaccess -eq $true){
$Error.Clear()
$FullAccessPermissions = Get-MailboxPermission -Identity ($Mailbox.PrimarySMTPAddress).tostring() | ? {($_.AccessRights -like “*FullAccess*”) -and ($_.IsInherited -eq $false) -and ($_.User -notlike “NT AUTHORITY\SELF”) -and ($_.User -notlike "S-1-5*") -and ($_.User -notlike $Mailbox.PrimarySMTPAddress)}
If($FullAccessPermissions){
Foreach($perm in $FullAccessPermissions){
#$ifGroup = Get-Group -identity $perm.user.tostring() -ErrorAction SilentlyContinue
$ifGroup = Get-GroupCustom -identity $perm.user.tostring() -ErrorAction SilentlyContinue
If($ifGroup){
If($EnumerateGroups -eq $true){
If(-not ($excludedGroups -contains $ifGroup.Name)){
$groupDomainName = $ifgroup.identity.tostring().split("/")[0]
Write-LogEntry -LogName:$Script:LogFile -LogEntryText "Found permission : FullAccess : Enumerate Group $($ifGroup.distinguishedName) Domain Name: $groupDomainName"
#$lstUsr = Get-AdGroup -identity $ifGroup.distinguishedName | Get-ADGroupMember -Recursive | Get-ADUser -Properties Mail $globalCatalog
$lstUsr = Get-ADGroupMember -identity $ifGroup.distinguishedName -server $groupDomainName -Recursive | Get-ADUser -Properties Mail | ?{$_.Mail}
foreach ($usrTmp in $lstUsr) {
$usrTmpEmail = $usrTmp.Mail
If($ExcludedServiceAccts){
if(-not ($ExcludedServiceAccts -contains $usrTmpEmail -or $ExcludedServiceAccts -contains $mailbox.primarySMTPAddress.ToString())){
Write-LogEntry -LogName:$Script:LogFile -LogEntryText "Found permission : FullAccess : $($usrTmpEmail)"
$CollectPermissions.add([pscustomobject]@{Mailbox = $Mailbox.PrimarySMTPAddress; User = $usrTmpEmail; AccessRights = "Full Access"})
}
}
Else{
Write-LogEntry -LogName:$Script:LogFile -LogEntryText "Found permission : FullAccess : $($usrTmpEmail)"
$CollectPermissions.add([pscustomobject]@{Mailbox = $Mailbox.PrimarySMTPAddress; User = $usrTmpEmail; AccessRights = "Full Access"})
}
}
}
}
}
Else{
#$delegate = Get-Recipient -Identity $perm.user.tostring() -ErrorAction SilentlyContinue
$delegate = Get-RecipientCustom $perm.user.tostring()
If($mailbox.primarySMTPAddress -and $delegate.primarySMTPAddress){
If(-not ($mailbox.primarySMTPAddress.ToString() -eq $delegate.primarySMTPAddress.ToString())){
If($ExcludedServiceAccts){
if(-not ($ExcludedServiceAccts -contains $delegate.primarySMTPAddress.tostring() -or $ExcludedServiceAccts -contains $mailbox.primarySMTPAddress.ToString())){
Write-LogEntry -LogName:$Script:LogFile -LogEntryText "Found permission : FullAccess : $($delegate.primarySMTPAddress.ToString())"
$CollectPermissions.add([pscustomobject]@{Mailbox = $Mailbox.PrimarySMTPAddress; User = $delegate.primarySMTPAddress.ToString(); AccessRights = "Full Access"})
}
}
Else{
Write-LogEntry -LogName:$Script:LogFile -LogEntryText "Found permission : FullAccess : $($delegate.primarySMTPAddress.ToString())"
$CollectPermissions.add([pscustomobject]@{Mailbox = $Mailbox.PrimarySMTPAddress; User = $delegate.primarySMTPAddress.ToString(); AccessRights = "Full Access"})
}
}
}
}
}
}
If($Error){
Write-LogEntry -LogName:$Script:LogFile -LogEntryText "MBX=$($Mailbox.PrimarySMTPAddress) PERM=FullAccess ERROR=$($error[0].ToString()) POSITION=$($error[0].InvocationInfo.PositionMessage)"
}
}
If($gathersendas -eq $true){
$Error.Clear()
#$SendAsPermissions = Get-ADPermission $Mailbox.DistinguishedName | ?{($_.ExtendedRights -like "*send-as*") -and ($_.IsInherited -eq $false) -and -not ($_.User -like "NT AUTHORITY\SELF") }
$SendAsPermissions = New-Object System.Collections.Generic.List[System.Object]
$userDN = [ADSI]("LDAP://$($mailbox.DistinguishedName)")
$userDN.psbase.ObjectSecurity.Access | ? { ($_.ObjectType -eq [GUID]$right.RightsGuid.Value) -and ($_.IsInherited -eq $false) } | select -ExpandProperty identityreference | %{
If(-not ($_ -like "NT AUTHORITY\SELF")){
$SendAsPermissions.add($_)
}
}
If($SendAsPermissions){
Foreach($perm in $SendAsPermissions){
#$ifGroup = Get-Group -identity $perm.tostring() -ErrorAction SilentlyContinue
$ifGroup = Get-GroupCustom -identity $perm.tostring() -ErrorAction SilentlyContinue
If($ifGroup){
If($EnumerateGroups -eq $true){
If(-not ($ExcludedGroups -contains $ifGroup.Name)){
$groupDomainName = $ifgroup.identity.tostring().split("/")[0]
Write-LogEntry -LogName:$Script:LogFile -LogEntryText "Found permission : SendAs : Enumerate Group $($ifGroup.distinguishedName) Domain Name: $groupDomainName"
#$lstUsr = Get-AdGroup -identity $ifGroup.distinguishedName -Server $groupDomainName | Get-ADGroupMember -Recursive | Get-ADUser -Properties Mail -server $globalCatalog
$lstUsr = Get-ADGroupMember -identity $ifGroup.distinguishedName -server $groupDomainName -Recursive | Get-ADUser -Properties Mail | ?{$_.Mail}
foreach ($usrTmp in $lstUsr) {
$usrTmpEmail = $usrTmp.Mail
If($ExcludedServiceAccts){
if(-not ($ExcludedServiceAccts -contains $usrTmpEmail -or $ExcludedServiceAccts -contains $mailbox.primarySMTPAddress.ToString())){
Write-LogEntry -LogName:$Script:LogFile -LogEntryText "Found permission : SendAs : $($usrTmpEmail)"
$CollectPermissions.add([pscustomobject]@{Mailbox = $Mailbox.PrimarySMTPAddress; User = $usrTmpEmail; AccessRights = "Send As"})
}
}
Else{
Write-LogEntry -LogName:$Script:LogFile -LogEntryText "Found permission : SendAs : $($usrTmpEmail)"
$CollectPermissions.add([pscustomobject]@{Mailbox = $Mailbox.PrimarySMTPAddress; User = $usrTmpEmail; AccessRights = "Send As"})
}
}
}
}
}
Else{
#$delegate = Get-Recipient -Identity $perm.tostring() -ErrorAction SilentlyContinue
$delegate = Get-RecipientCustom $perm.tostring()
If($mailbox.primarySMTPAddress -and $delegate.primarySMTPAddress){
If(-not ($mailbox.primarySMTPAddress.ToString() -eq $delegate.primarySMTPAddress.ToString())){
If($ExcludedServiceAccts){
if(-not ($ExcludedServiceAccts -contains $delegate.primarySMTPAddress.tostring() -or $ExcludedServiceAccts -contains $mailbox.primarySMTPAddress.ToString())){
Write-LogEntry -LogName:$Script:LogFile -LogEntryText "Found permission : SendAs : $($delegate.primarySMTPAddress.ToString())"
$CollectPermissions.add([pscustomobject]@{Mailbox = $Mailbox.PrimarySMTPAddress; User = $delegate.primarySMTPAddress.ToString(); AccessRights = "Send As"})
}
}
Else{
Write-LogEntry -LogName:$Script:LogFile -LogEntryText "Found permission : SendAs : $($delegate.primarySMTPAddress.ToString())"
$CollectPermissions.add([pscustomobject]@{Mailbox = $Mailbox.PrimarySMTPAddress; User = $delegate.primarySMTPAddress.ToString(); AccessRights = "Send As"})
}
}
}
}
}
}
If($Error){
Write-LogEntry -LogName:$Script:LogFile -LogEntryText "MBX=$($Mailbox.PrimarySMTPAddress) PERM=SendAs ERROR=$($error[0].ToString()) POSITION=$($error[0].InvocationInfo.PositionMessage)"
}
}
If($gatherSendOnBehalfTo -eq $true){
$Error.Clear()
$GrantSendOnBehalfToPermissions = $Mailbox.grantsendonbehalfto.ToArray()
If($GrantSendOnBehalfToPermissions){
Foreach($perm in $GrantSendOnBehalfToPermissions){
#$delegate = Get-Recipient -Identity $perm.tostring() -ErrorAction SilentlyContinue
$delegate = Get-RecipientCustom $perm.tostring()
If($mailbox.primarySMTPAddress -and $delegate.primarySMTPAddress){
If(-not ($mailbox.primarySMTPAddress.ToString() -eq $delegate.primarySMTPAddress.ToString())){
If($ExcludedServiceAccts){
if(-not ($ExcludedServiceAccts -contains $delegate.primarySMTPAddress.tostring() -or $ExcludedServiceAccts -contains $mailbox.primarySMTPAddress.ToString())){
Write-LogEntry -LogName:$Script:LogFile -LogEntryText "Found permission : SendOnBehalfTo : $($delegate.primarySMTPAddress.ToString())"
$CollectPermissions.add([pscustomobject]@{Mailbox = $Mailbox.PrimarySMTPAddress; User = $delegate.primarySMTPAddress.ToString(); AccessRights = "SendOnBehalfTo"})
}
}
Else{
Write-LogEntry -LogName:$Script:LogFile -LogEntryText "Found permission : SendOnBehalfTo : $($delegate.primarySMTPAddress.ToString())"
$CollectPermissions.add([pscustomobject]@{Mailbox = $Mailbox.PrimarySMTPAddress; User = $delegate.primarySMTPAddress.ToString(); AccessRights = "SendOnBehalfTo"})
}
}
}
}
}
If($Error){
Write-LogEntry -LogName:$Script:LogFile -LogEntryText "MBX=$($Mailbox.PrimarySMTPAddress) PERM=SendOnBehalfTo ERROR=$($error[0].ToString()) POSITION=$($error[0].InvocationInfo.PositionMessage)"
}
}
If($CollectPermissions.Count -eq 0){
#write progress to xml file
$updateXML = [System.Xml.XmlDocument](Get-Content $ProgressXMLFile)
$node = $updateXML.Mailboxes.Mailbox | ?{$_.Name -eq $Mailbox.PrimarySMTPAddress}
If($node -ne $null){
$node.Progress = "Completed"
}
$updateXML.save($ProgressXMLFile)
$CollectPermissions.add([pscustomobject]@{Mailbox = $Mailbox.PrimarySMTPAddress; User = "None"; AccessRights = "None"})
Return $CollectPermissions
}
else{
#write progress to xml file
$updateXML = [System.Xml.XmlDocument](Get-Content $ProgressXMLFile)
$node = $updateXML.Mailboxes.Mailbox | ?{$_.Name -eq $Mailbox.PrimarySMTPAddress}
If($node -ne $null){
$node.Progress = "Completed"
}
$updateXML.save($ProgressXMLFile)
Return $CollectPermissions
}
}
catch{
$updateXML = [System.Xml.XmlDocument](Get-Content $ProgressXMLFile)
$node = $updateXML.Mailboxes.Mailbox | ?{$_.Name -eq $UserEmail}
If($node -ne $null){
$node.Progress = "Failed"
}
$updateXML.save($ProgressXMLFile)
Write-LogEntry -LogName:$Script:LogFile -LogEntryText "MBX=$($UserEmail) ERROR=$($_.exception.message) POSITION=$($_.InvocationInfo.Line) $($_.InvocationInfo.PositionMessage)"
}
}
Function ConnectTo-Exchange ($ExchServerFQDN) {
#Connect to Exchange
if (Test-Path $env:ExchangeInstallPath\bin\RemoteExchange.ps1){
. $env:ExchangeInstallPath\bin\RemoteExchange.ps1 | out-null
If(!$ExchServerFQDN){
Connect-ExchangeServer -auto -AllowClobber | Out-Null
}
Else{
Connect-ExchangeServer -serverfqdn $ExchServerFQDN -AllowClobber | Out-Null
}
}
else{
Write-LogEntry -LogName:$LogFile -LogEntryText "Exchange Server management tools are not installed on this computer." -ForegroundColor Red
EXIT
}
#Method #2 to connect using remote powershell
<#
If($ExchServerFQDN){
try{
""
#If want to save creds without having to enter password into Get-Credential every time
#$password = "Password" | ConvertTo-SecureString -asPlainText -Force
#$username = "[email protected]"
#$Creds = New-Object System.Management.Automation.PSCredential($username,$password)
#$ExchServerFQDN = "$env:computername.$env:userdnsdomain"
$session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://$ExchServerFQDN/PowerShell/ -Authentication Kerberos -WarningAction 'SilentlyContinue' -ErrorAction SilentlyContinue
If(!$session){
$Creds = Get-Credential -Message "Unable to connect using current credentials. Enter account credentials that has permissions to connect to Exchange"
$session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://$ExchServerFQDN/PowerShell/ -Authentication Kerberos -Credential $Creds -WarningAction 'SilentlyContinue'
If(!$session){
throw
}
}
$Connect = Import-Module (Import-PSSession $Session -AllowClobber -WarningAction 'SilentlyContinue' -DisableNameChecking) -Global -WarningAction 'SilentlyContinue'
}
catch{
throw "Unable to establish a session with the Exchange Server: $($ExchServerFQDN)"
exit
}
}
Else{
#check if a session already exists
$error.clear()
get-command get-mailbox -ErrorAction SilentlyContinue | out-null
If($error){
try{
""
#If want to save creds without having to enter password into Get-Credential every time
#$password = "Password" | ConvertTo-SecureString -asPlainText -Force
#$username = "[email protected]"
#$Creds = New-Object System.Management.Automation.PSCredential($username,$password)
$ExchServerFQDN = "$env:computername.$env:userdnsdomain"
$session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://$ExchServerFQDN/PowerShell/ -Authentication Kerberos -WarningAction 'SilentlyContinue' -ErrorAction SilentlyContinue
If(!$session){
$ExchServerFQDN = Read-host "Type in the FQDN of the Exchange Server to connect to"
$Creds = Get-Credential -Message "Enter credentials to connect to exchange on premises"
$session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://$ExchServerFQDN/PowerShell/ -Authentication Kerberos -Credential $Creds -WarningAction 'SilentlyContinue'
If(!$session){
throw
}
}
$Connect = Import-Module (Import-PSSession $Session -AllowClobber -WarningAction 'SilentlyContinue' -DisableNameChecking) -Global -WarningAction 'SilentlyContinue'
}
catch{
throw "Unable to establish a session with the Exchange Server: $($ExchServerFQDN)"
exit
}
}
}
#>
}
Function Create-Batches(){
param(
[string]$InputPermissionsFile
)
#Variables
If(-not (Test-Path $InputPermissionsFile)){
Write-LogEntry -LogName:$Script:LogFile -LogEntryText "$($InputPermissionsFile) file not found. Check the log file for more info: $LogFile" -ForegroundColor Red
exit
}
If((get-childitem $InputPermissionsFile).length -eq 0 ){
Write-LogEntry -LogName:$Script:LogFile -LogEntryText "The permissions file is empty. Check the log file for more info: $LogFile" -ForegroundColor Red
exit
}
Write-LogEntry -LogName:$Script:LogFile -LogEntryText "Run function: Create-Batches" -ForegroundColor White
$data = import-csv $InputPermissionsFile
$hashData = $data | Group Mailbox -AsHashTable -AsString
$hashDataByDelegate = $data | Group user -AsHashTable -AsString
$usersWithNoDependents = New-Object System.Collections.ArrayList
$batch = @{}
$batchNum = 0
$hashDataSize = $hashData.Count
$yyyyMMdd = Get-Date -Format 'yyyyMMdd'
try{
Write-LogEntry -LogName:$Script:LogFile -LogEntryText "Build ArrayList for users with no dependents"
If($hashDataByDelegate["None"].count -gt 0){
$hashDataByDelegate["None"] | %{$_.Mailbox} | %{[void]$usersWithNoDependents.Add($_)}
}
Write-LogEntry -LogName:$Script:LogFile -LogEntryText "Identify users with no permissions on them, nor them have perms on another"
If($usersWithNoDependents.count -gt 0){
$($usersWithNoDependents) | %{
if($hashDataByDelegate.ContainsKey($_)){
$usersWithNoDependents.Remove($_)
}
}
Write-LogEntry -LogName:$Script:LogFile -LogEntryText "Remove users with no dependents from hash"
$usersWithNoDependents | %{$hashData.Remove($_)}
#Clean out hashData of users in hash data with no delegates, otherwise they'll get batched
Write-LogEntry -LogName:$Script:LogFile -LogEntryText "Clean out hashData of users in hash with no delegates"
foreach($key in $($hashData.keys)){
if(($hashData[$key] | select -expandproperty user ) -eq "None"){
$hashData.Remove($key)
}
}
}
#Execute batch functions
If(($hashData.count -ne 0) -or ($usersWithNoDependents.count -ne 0)){
Write-LogEntry -LogName:$Script:LogFile -LogEntryText "Run function: Find-Links" -ForegroundColor White
while($hashData.count -ne 0){Find-Links $hashData | out-null}
Write-LogEntry -LogName:$Script:LogFile -LogEntryText "Run function: Create-BatchFile" -ForegroundColor White
Create-BatchFile $batch $usersWithNoDependents
}
}
catch {
Write-LogEntry -LogName:$Script:LogFile -LogEntryText "Error: $_"
}
}
Function Find-Links($hashData){
try{
$nextInHash = $hashData.Keys | select -first 1
$batch.Add($nextInHash,$hashData[$nextInHash])
Do{
$checkForMatches = $false
foreach($key in $($hashData.keys)){
Write-Progress -Activity "Step 2 of 3: Analyze Delegates" -status "Items remaining: $($hashData.Count)" -percentComplete (($hashDataSize-$hashData.Count) / $hashDataSize*100)
#Checks
$usersHashData = $($hashData[$key]) | %{$_.mailbox}
$usersBatch = $($batch[$nextInHash]) | %{$_.mailbox}
$delegatesHashData = $($hashData[$key]) | %{$_.user}
$delegatesBatch = $($batch[$nextInHash]) | %{$_.user}
$ifMatchesHashUserToBatchUser = [bool]($usersHashData | ?{$usersBatch -contains $_})
$ifMatchesHashDelegToBatchDeleg = [bool]($delegatesHashData | ?{$delegatesBatch -contains $_})
$ifMatchesHashUserToBatchDelegate = [bool]($usersHashData | ?{$delegatesBatch -contains $_})
$ifMatchesHashDelegToBatchUser = [bool]($delegatesHashData | ?{$usersBatch -contains $_})
If($ifMatchesHashDelegToBatchDeleg -OR $ifMatchesHashDelegToBatchUser -OR $ifMatchesHashUserToBatchUser -OR $ifMatchesHashUserToBatchDelegate){
if(($key -ne $nextInHash)){
$batch[$nextInHash] += $hashData[$key]
$checkForMatches = $true
}
$hashData.Remove($key)
}
}
} Until ($checkForMatches -eq $false)
return $hashData
}
catch{
Write-LogEntry -LogName:$Script:LogFile -LogEntryText "Error: $_" -ForegroundColor Red
}
}
Function Create-BatchFile($batchResults,$usersWithNoDepsResults){
try{
"Batch,User" > $Script:BatchesFile
foreach($key in $batchResults.keys){
$batchNum++
$batchName = "BATCH-$batchNum"
$output = New-Object System.Collections.ArrayList
$($batch[$key]) | %{$output.add($_.mailbox) | out-null}
$($batch[$key]) | %{$output.add($_.user) | out-null}
$output | select -Unique | % {
"$batchName"+","+$_ >> $Script:BatchesFile
}
}
If($usersWithNoDepsResults.count -gt 0){
$batchNum++
foreach($user in $usersWithNoDepsResults){
#$batchName = "BATCH-$batchNum"
$batchName = "BATCH-NoDependencies"
"$batchName"+","+$user >> $Script:BatchesFile
}
}
}
catch{
Write-LogEntry -LogName:$Script:LogFile -LogEntryText "Error: $_" -ForegroundColor Red
}
}
Function Create-MigrationSchedule(){
param(
[string]$InputBatchesFile
)
try{
If(-not (Test-Path $InputBatchesFile)){
throw [System.IO.FileNotFoundException] "$($InputBatchesFile) file not found."
}
$usersFromBatch = import-csv $InputBatchesFile
"Migration Date(MM/dd/yyyy),Migration Window,Migration Group,PrimarySMTPAddress,SuggestedBatch,MailboxSize(MB),Notes" > $Script:MigrationScheduleFile
$userInfo = New-Object System.Text.StringBuilder
Write-LogEntry -LogName:$Script:LogFile -LogEntryText "Number of users in the migration schedule: $($usersFromBatch.Count)" -ForegroundColor White
$usersFromBatchCounter = 0
foreach($item in $usersFromBatch){
$usersFromBatchCounter++
$usersFromBatchRemaining = $usersFromBatch.count - $usersFromBatchCounter
Write-Progress -Activity "Step 3 of 3: Creating migration schedule" -status "Items remaining: $($usersFromBatchRemaining)" -percentComplete (($usersFromBatchCounter / $usersFromBatch.count)*100)
#Check if using UseImportCSVFile and if yes, check if the user was part of that file, otherwise mark
$isUserPartOfInitialCSVFile = ""
If($Script:InputMailboxesCSV -ne ""){
If(-not ($Script:ListOfMailboxes.PrimarySMTPAddress -contains $item.user)){
$isUserPartOfInitialCSVFile = "User was not part of initial csv file"
}
}
$user = get-user $item.user #-erroraction SilentlyContinue
If(![string]::IsNullOrEmpty($user.WindowsEmailAddress)){
If($user.recipienttype -eq "UserMailbox"){
$mbStats = Get-MailboxStatistics $user.WindowsEmailAddress.tostring() | select totalitemsize
If($mbStats.totalitemsize.value)
{
#if connecting through remote pshell, and not using Exo server shell, the data comes as
#TypeName: Deserialized.Microsoft.Exchange.Data.ByteQuantifiedSize
if ( ($mbStats.TotalItemSize.Value.GetType()).name.ToString() -eq "ByteQuantifiedSize")
{
$mailboxSize = $mbStats.totalitemsize.value.ToMb()
}
else
{
$mailboxSize = $mbStats.TotalItemSize.Value.ToString().split("(")[1].split(" ")[0].replace(",","")/1024/1024
}
}
Else{
$mailboxSize = 0
}
$userInfo.AppendLine(",,,$($user.WindowsEmailAddress),$($item.Batch),$($mailboxSize),$isUserPartOfInitialCSVFile") | Out-Null
}
}
Else{ #there was an error either getting the user from Get-User or the user doesn't have an email address
$userInfo.AppendLine(",,,$($item.user),$($item.Batch),n/a,,User not found or doesn't have an email address") | Out-Null
}
}
$userInfo.ToString().TrimEnd() >> $Script:MigrationScheduleFile
}
catch{
Write-LogEntry -LogName:$Script:LogFile -LogEntryText "Error: $($_) at $($_.InvocationInfo.ScriptLineNumber)" -ForegroundColor Red
}
}
Function CleanUp-PreviousRun() {
try{
If(test-path $PermsOutputFile){Remove-item -path $PermsOutputFile}
If(test-path $BatchesFile){Remove-item -path $BatchesFile}
If(test-path $MigrationScheduleFile){Remove-item -path $MigrationScheduleFile}
#$ProgressXMLFile doesn't have to be wiped since this is recreated every time
Write-LogEntry -LogName:$LogFile -LogEntryText "Successfully cleaned up previous run results."
}
catch{
Write-LogEntry -LogName:$LogFile -LogEntryText "Unable to clean up csv outputs from previous run. This needs to be done to avoid mixed results. ERROR=$($_) " -ForegroundColor Red
exit
}
}
#endregion functions
#Script Variables
$elapsed = [System.Diagnostics.Stopwatch]::StartNew()
$scriptPath = $PSScriptRoot
$yyyyMMdd = Get-Date -Format 'yyyyMMdd'
$LogFile = "$scriptPath\Find-MailboxDelegates-$yyyyMMdd.log"
$PermsOutputFile = "$scriptPath\Find-MailboxDelegates-Permissions.csv"
$BatchesFile = "$scriptPath\Find-MailboxDelegates-Batches.csv"
$MigrationScheduleFile = "$scriptPath\Find-MailboxDelegates-Schedule.csv"
$ProgressXMLFile = "$scriptPath\Find-MailboxDelegates-Progress.xml"
$Version = "12062019"
$computer = $env:COMPUTERNAME
$user = $env:USERNAME
$WarningPreference = "SilentlyContinue"
$ErrorActionPreference = "SilentlyContinue"
""
Write-LogEntry -LogName:$LogFile -LogEntryText "User: $user Computer: $computer ScriptVersion: $Version PowershellVersion: $($PSVersionTable.PSVersion.Major)" -foregroundcolor Yellow
""
Write-LogEntry -LogName:$LogFile -LogEntryText "Script parameters passed: $($PSBoundParameters.GetEnumerator())"
""
Write-LogEntry -LogName:$LogFile -LogEntryText "Pre-flight Check" -ForegroundColor Green
#Requirement is Powershell V3 in order to use PSCustomObjets which are data structures
If($PSVersionTable.PSVersion.Major -lt 3){
throw "Powershell V3+ is required. If you're running from Exchange Shell, it may be defaulting to PS2.0. Run 'powershell -version 3' and re-run the script."
}
#Run only the batch users step if the switch BatchUsersOnly switch has been added
If($BatchUsersOnly -and $GetPermissionsOnly){
Throw "Can't have both 'BatchUsersOnly' and 'GetPermissionsOnly' switches. Please use one or the other. "
exit
}
ElseIf($BatchUsersOnly){
Write-LogEntry -LogName:$LogFile -LogEntryText "Running only Step #2: Batch Users..." -ForegroundColor Yellow
Create-Batches -InputPermissionsFile $PermsOutputFile
exit
}
#Check switches provided are acceptable
If($BatchUsers -and ($FullAccess -or $SendOnBehalfTo -or $Calendar -or $SendAs -or $InputMailboxesCSV -or $EnumerateGroups -or $ExcludeServiceAccts -or $ExcludeGroups -or $Resume -or $AccountResourceEnv)){
throw "BatchUsers can't be combined with these other switches."
}
If(!$FullAccess -and !$SendOnBehalfTo -and !$Calendar -and !$SendAs -and !$BatchUsers -and !$BatchUsersOnly){
throw "Include the switches for the permissions you want to query on. Check the read me file for more details."
}
$testSession = get-command get-mailbox -ErrorAction SilentlyContinue
If(!$testSession){
Write-LogEntry -LogName:$LogFile -LogEntryText "Didn't find an active exchange session. Initiating..." -ForegroundColor Gray
ConnectTo-Exchange $ExchServerFQDN | Out-Null
""
}
$exchserversession = get-pssession | ?{$_.configurationname -eq "Microsoft.Exchange"} | select -expandproperty computername
$exchangeversion = get-exchangeserver $exchserversession | select -expandproperty AdminDisplayVersion
Write-LogEntry -LogName:$LogFile -LogEntryText "ExchangeServerName: $($exchserversession) ExchangeServerVersion: $($exchangeversion)"
""
#Open connection to AD - this will be used to enumerate groups and collect Send As permissions
$checkADModule = get-module -listavailable activedirectory
If($checkADModule -eq $null){
throw "Please install the Active Direcotry Module: https://docs.microsoft.com/en-us/previous-versions/windows/it-pro/windows-server-2008-R2-and-2008/dd378937(v=ws.10) "
}
Import-Module -Name ActiveDirectory
If(($EnumerateGroups -eq $true) -or ($SendAs -eq $true)){
$dse = [ADSI]"LDAP://Rootdse"
$ext = [ADSI]("LDAP://CN=Extended-Rights," + $dse.ConfigurationNamingContext)
$dn = [ADSI]"LDAP://$($dse.DefaultNamingContext)"
$dsLookFor = new-object System.DirectoryServices.DirectorySearcher($dn)
$permission = "Send As"
$right = $ext.psbase.Children | ? { $_.DisplayName -eq $permission }
}
#Check if re-running the script without resume. Clean outputs from previous run to prevent data corruption
If((!$Resume) -and (test-path $PermsOutputFile) -and (!$BatchUsers) -and (!$BatchUsersOnly)){
Write-LogEntry -LogName:$LogFile -LogEntryText "Clean up previous run to avoid mixed results" -ForegroundColor Yellow
CleanUp-PreviousRun
}
#Set scope to find objects in other domains
Set-AdServerSettings -ViewEntireForest $True
#Used for Acccount/Resource models
If($AccountResourceEnv){
Write-LogEntry -LogName:$LogFile -LogEntryText "Creating Mailboxes lookup table for Account/Resource Environment" -ForegroundColor Gray
$Script:mailboxesLookup = Get-Mailbox -ResultSize Unlimited
}
#Get Mailboxes
If($Resume){
If(!$InputMailboxesCSV){
If(test-path $ProgressXMLFile){
$xmlDoc = [System.Xml.XmlDocument](Get-Content $ProgressXMLFile)
$ListOfMailboxes = $xmlDoc.mailboxes.mailbox | ?{$_.Progress -eq "Pending"} | select @{N="PrimarySMTPAddress";E={$_.name}} #-expandproperty name
}
else{
throw "Unable to resume due to missing progress file: $($ProgressXMLFile)"
exit
}
}
Else{
throw "Can't have both 'Resume' and 'InputMailboxesCSV' at the same time. Choose 'Resume' if you want to pick up on where you left off from a previous run."
exit
}
}
ElseIf(!$Batchusers){
If($InputMailboxesCSV -ne ""){
$ListOfMailboxes = Import-Csv $InputMailboxesCSV
if($ListOfMailboxes.PrimarySMTPAddress -eq $null){
throw "Make sure the input csv file header is: PrimarySMTPAddress"
exit
}
#write to xml for progress tracking
[xml]$xmlDoc = New-Object System.Xml.XmlDocument
$dec = $xmlDoc.CreateXmlDeclaration("1.0","UTF-8",$null)
$xmlDoc.AppendChild($dec) | Out-Null
$root = $xmlDoc.CreateNode("element","Mailboxes",$null)
foreach($entry in $ListOfMailboxes.PrimarySMTPAddress){
$mbx = $xmlDoc.CreateNode("element","Mailbox",$null)
$mbx.SetAttribute("Name",$entry)
$mbx.SetAttribute("Progress","Pending")
$root.AppendChild($mbx) | Out-Null
}
#add root to the document
$xmlDoc.AppendChild($root) | Out-Null
#save file
$xmlDoc.save($ProgressXMLFile)
}
Else{
If($mailboxesLookup){
$ListOfMailboxes = $mailboxesLookup | select PrimarySMTPAddress
}
Else{
$ListOfMailboxes = Get-Mailbox -ResultSize Unlimited | select PrimarySMTPAddress
}
#write to xml for progress tracking
[xml]$xmlDoc = New-Object System.Xml.XmlDocument
$dec = $xmlDoc.CreateXmlDeclaration("1.0","UTF-8",$null)
$xmlDoc.AppendChild($dec) | Out-Null
$root = $xmlDoc.CreateNode("element","Mailboxes",$null)
foreach($entry in $ListOfMailboxes.PrimarySMTPAddress){
$mbx = $xmlDoc.CreateNode("element","Mailbox",$null)
$mbx.SetAttribute("Name",$entry)
$mbx.SetAttribute("Progress","Pending")
$root.AppendChild($mbx) | Out-Null
}
#add root to the document
$xmlDoc.AppendChild($root) | Out-Null
#save file
$xmlDoc.save($ProgressXMLFile)
}
}
#Get excluded groups
If($ExcludeGroupsCSV){
If(test-path $ExcludeGroupsCSV){
$ExcludeGroups = get-content $ExcludeGroupsCSV
}
Else{
throw "Unable to find the CSV file for excluded groups. Confirm this is the right directory: $($ExcludeGroupsCSV)"
exit
}
}