-
Notifications
You must be signed in to change notification settings - Fork 4
/
Janus.psm1
13117 lines (11336 loc) · 426 KB
/
Janus.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
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
# **********************************************************************************
#
# Script Name: Janus.psm1
# Version: 1.8
# Author: Dave M and Dennis K
# Date Created: June 14, 2011
# _______________________________________
#
# MODIFICATIONS:
# Date Modified: 12-20-2011
# Modified By: Dave M
# Reason for modification: Added Standard Header and Logging
# What was modified: Header and functions
#
# Date Modified: 9-28-2012
# Modified By: Dave M
# Reason for modification: Added Inheritance functions
#
# Date Modified: 11-8-2012
# Modified By: Dave M
# Reason for modification: Added AD Attribue Error checking to Get-JADEntry
#
# Date Modified: 12-28-2012
# Modified By: Dave M
# Reason for modification: Added Add-JHolidays
#
# Date Modified: 4-22-2013
# Modified By: Dave M
# Reason for modification: Added Show-JFailureAuditEvents
#
# `: This Module contains cmdlets for use at Janus.
#
# Usage:
# Import-Module Janus
#
# **********************************************************************************
$ErrorActionPreference = "SilentlyContinue"
$WarningActionPreference = "SilentlyContinue"
$error.clear()
set-alias -name cat get-content
set-alias -name cd set-location
set-alias -name cls clear-host
set-alias -name kill stop-process
set-alias -name echo write-output
set-alias -name copy copy-item
set-alias -name del remove-item
set-alias -name dir get-childitem
set-alias -name move move-item
set-alias -name set set-variable
set-alias -name type get-content
set-alias -name grep Get-JStringMatches
# Legacy Janus Module Aliases
set-alias -name Show-JMessageTracking -value Get-JMessageTracking -Scope Global
set-alias -name Add-Delegate -value Add-JEWSDelegate -Scope Global
set-alias -name Get-PF -value Show-JPublicFolderInfo -Scope Global
set-alias -name Move-ADEntry -value Move-JADEntry -Scope Global
set-alias -name Add-Contact -value Add-JContact -Scope Global
set-alias -name Set-Pass -value Set-JPassword -Scope Global
set-alias -name Move-Bulk -value Move-JMailboxesInBulk -Scope Global
set-alias -name Out-Excel -value Out-JExcel -Scope Global
set-alias -name Report-ASUser -value Show-JActiveSyncUserInfo -Scope Global
set-alias -name Add-CRDelegate -value Add-JCRDelegate -Scope Global
set-alias -name Report-OOF -value Show-JOOFSettings -Scope Global
set-alias -name SendTo-Zip -value ConvertTo-JZip -Scope Global
set-alias -name Fix-Tentative -value Update-JCalendarTentativeSettings -Scope Global
set-alias -name Execute-RemoteCMD -value Send-JRemoteCMD -Scope Global
set-alias -name Control-Service -value Repair-JService -Scope Global
set-alias -name Check-Para -value Protect-JParameter -Scope Global
set-alias -name Get-ADEntry -value Get-JADEntry -Scope Global
set-alias -name OctetToGUID -value ConvertFrom-JOctetToGUID -Scope Global
set-alias -name Set-ADEntry -value Set-JADEntry -Scope Global
set-alias -name Change-SMTPAddress -value Update-JPrimarySMTPAddress -Scope Global
function help
{
$test=@()
[string]$filter=""
[string]$cmdlets=""
$test=get-help -full $args[0]
$cmdlets=$test
if ($test.count -lt 1)
{
$filter='*' + $args[0] + '*'
$filter=$filter.replace('**','*')
write-host "Search Filter: $filter"
get-help -full $($filter)
}
elseif (($test.count -eq 2) -and($cmdlets.contains("Microsoft.Exchange.Management.PowerShell.E2010")))
{
$filter='Microsoft.Exchange.Management.PowerShell.E2010\' + $args[0]
write-host "Search Filter: $filter"
get-help -full $filter
}
else
{
get-help -full $args[0]
}
} # function help
function prompt
{
"PS " + $(get-location) + "> "
}
& {
for ($i = 0; $i -lt 26; $i++)
{
$funcname = ([System.Char]($i+65)) + ':'
$str = "function global:$funcname { set-location $funcname } "
invoke-expression $str
}
} # function prompt
function Get-JMessageTracking
{
<#
.SYNOPSIS
Searches all mailbox and Hub servers for the message specified.
.DESCRIPTION
Generates a list of all Mailbox and Transport Servers and searches
for the message requested. It returns all Events that match the
specified criteria.
.PARAMETER from
Enter the smtp address of the Sender you want to track.
.PARAMETER to
Enter the smtp address of the recipient you want to track.
NOTE: Includes To, CC and BCC fields.
.PARAMETER subject
Enter the Subject line you are searching for.
NOTE: Message tracking automatically treats the string you
enter as "*$subject*". This is a feature of message tracking
and cannot be changed.
.PARAMETER start
Enter the earliest date for the message you want to track.
NOTE: Will default to 30 days ago if not specified.
.PARAMETER end
Enter the latest date for the message you want to track.
NOTE: Will default to the moment of execution if not specified.
.PARAMETER report
This switch instructs the cmdlet to sort and display the entire
message details by date.
.EXAMPLE
Get-JMessageTracking -from [email protected] -to [email protected] -subject "Test e-mail" -start "6/8/2011 9:00:05 am" -end "6/8/2011 9:01:38 am"
Description
-----------
Returns any messages sent after 9:00:05 AM on 6/8/2011 and before
9:01:38 AM on 6/8/2011 with the subject of "Test e-mail" sent from
.NOTES
Requires the Loading of the Exchange Module.
Show-JMessageRecipients uses the same parameters, but returns
the confirmed recipients of the message.
#>
# Initialization Section
# Parameters called so that they can be passed into the script via the command line.
[CmdletBinding()]
param (
[Parameter(Position = 0, Mandatory = $false)]
[String]
$from=$null,
[Parameter(Position = 1, Mandatory = $false)]
[String]
$to=$null,
[Parameter(Position = 2, Mandatory = $false)]
[String]
$subject=$null,
[Parameter(Position = 3, Mandatory = $false)]
[system.datetime]
$start,
[Parameter(Position = 4, Mandatory = $false)]
[system.datetime]
$end,
[Parameter(Position = 4, Mandatory = $false)]
[switch]
$report=$false
)
$results=@()
$pass=0
$case=0
# Main script
if (-not ($global:ExchangeSnapIn))
{
[string]$errorstring="WARNING: Exchange module is not loaded, some cmdlets will not work correctly, exiting...`n"
$Error.add($errorstring)
write-error ($errorstring) -erroraction stop
}
# Set the Start Date if the parameter is not set
if ($start -eq $null)
{
$start=(get-date).adddays(-30)
} else
{
$start=get-date($start)
}
# Set the End Date if the parameter is not set
if ($end -eq $null)
{
$end=get-date
} else
{
$end=get-date($end)
}
if ($from -ne "")
{
$case=$case+1
}
if ($to -ne "")
{
$case=$case+2
}
if ($subject -ne "")
{
$case=$case+4
}
# $case is used to identify which get-messagetrackinglog paramters are populated
switch ($case)
{
"0"
{
# This retreives objects representing all of the hub (and edge) servers
$servers=Get-TransportServer
# This retreives objects representing all of the mailbox servers
$servers+=Get-MailboxServer
foreach ($server in $servers)
{
write-host("Processing $server...")
# $results holds all of the message events
$results+=get-messagetrackinglog -Server $server -Start $start -End $end -resultsize unlimited
$pass=$pass+1
$matches=$results.length
# something happens with $matches, we need to reduce it by the number of iterations in the Foreach loop
$matches=$matches - $pass
write-host ("$matches result(s) so far...")
}
# If you use the -report switch, formats and sorts the events
if($report)
{
write-host ("Sorting result(s)...")
$results | sort Timestamp | fl | write-output
} else
# Or else it just returns the collection of events for manipulation
{
return $results
}
}
"1"
{
$servers=Get-TransportServer
$servers+=Get-MailboxServer
foreach ($server in $servers)
{
write-host("Processing $server...")
$results+=get-messagetrackinglog -Sender $from -Server $server -Start $start -End $end -resultsize unlimited
$pass=$pass+1
$matches=$results.length
$matches=$matches - $pass
write-host ("$matches result(s) so far...")
}
if($report)
{
write-host ("Sorting result(s)...")
$results | sort Timestamp | fl | write-output
} else
{
return $results
}
}
"2"
{
$servers=Get-TransportServer
$servers+=Get-MailboxServer
foreach ($server in $servers)
{
write-host("Processing $server...")
$results+=get-messagetrackinglog -Recipient $to -Server $server -Start $start -End $end -resultsize unlimited
$pass=$pass+1
$matches=$results.length
$matches=$matches - $pass
write-host ("$matches result(s) so far...")
}
if($report)
{
write-host ("Sorting result(s)...")
$results | sort Timestamp | fl | write-output
} else
{
return $results
}
}
"3"
{
$servers=Get-TransportServer
$servers+=Get-MailboxServer
foreach ($server in $servers)
{
write-host("Processing $server...")
$results+=get-messagetrackinglog -Sender $from -Recipient $to -Server $server -Start $start -End $end -resultsize unlimited
$pass=$pass+1
$matches=$results.length
$matches=$matches - $pass
write-host ("$matches result(s) so far...")
}
if($report)
{
write-host ("Sorting result(s)...")
$results | sort Timestamp | fl | write-output
} else
{
return $results
}
}
"4"
{
$servers=Get-TransportServer
$servers+=Get-MailboxServer
foreach ($server in $servers)
{
write-host("Processing $server...")
$results+=get-messagetrackinglog -Server $server -messagesubject "$subject" -Start $start -End $end -resultsize unlimited
$pass=$pass+1
$matches=$results.length
$matches=$matches - $pass
write-host ("$matches result(s) so far...")
}
if($report)
{
write-host ("Sorting result(s)...")
$results | sort Timestamp | fl | write-output
} else
{
return $results
}
}
"5"
{
$servers=Get-TransportServer
$servers+=Get-MailboxServer
foreach ($server in $servers)
{
write-host("Processing $server...")
$results+=get-messagetrackinglog -Sender $from -Server $server -messagesubject "$subject" -Start $start -End $end -resultsize unlimited
$pass=$pass+1
$matches=$results.length
$matches=$matches - $pass
write-host ("$matches result(s) so far...")
}
if($report)
{
write-host ("Sorting result(s)...")
$results | sort Timestamp | fl | write-output
} else
{
return $results
}
}
"6"
{
$servers=Get-TransportServer
$servers+=Get-MailboxServer
foreach ($server in $servers)
{
write-host("Processing $server...")
$results+=get-messagetrackinglog -Recipient $to -Server $server -messagesubject "$subject" -Start $start -End $end -resultsize unlimited
$pass=$pass+1
$matches=$results.length
$matches=$matches - $pass
write-host ("$matches result(s) so far...")
}
if($report)
{
write-host ("Sorting result(s)...")
$results | sort Timestamp | fl | write-output
} else
{
return $results
}
}
"7"
{
$servers=Get-TransportServer
$servers+=Get-MailboxServer
foreach ($server in $servers)
{
write-host("Processing $server...")
$pass=$pass+1
$matches=$results.length
$matches=$matches - $pass
$matches=$matches - 1
write-host ("$matches result(s) so far...")
}
if($report)
{
write-host ("Sorting result(s)...")
$results | sort Timestamp | fl | write-output
} else
{
return $results
}
}
default
{
[string]$errorstring="PARAMETER ERROR, exiting...`n"
$Error.add($errorstring)
write-error ($errorstring) -erroraction stop
}
}
remove-variable servers -ErrorAction SilentlyContinue
remove-variable results -ErrorAction SilentlyContinue
remove-variable matches -ErrorAction SilentlyContinue
remove-variable pass -ErrorAction SilentlyContinue
} # function Get-JMessageTracking
function Show-JMessageRecipients
{
<#
.SYNOPSIS
Searches all mailbox and Hub servers for the recipients of the
message specified.
.DESCRIPTION
Generates a list of all Mailbox and Transport Servers and searches
for the message requested. It returns all reipients that match the
specified criteria.
.PARAMETER from
Enter the smtp address of the Sender you want to track.
.PARAMETER to
Enter the smtp address of the recipient you want to track.
NOTE: Includes To, CC and BCC fields.
.PARAMETER subject
Enter the Subject line you are searching for.
.PARAMETER start
Enter the earliest date for the message you want to track.
NOTE: Will default to one week ago if not specified.
.PARAMETER end
Enter the latest date for the message you want to track.
NOTE: Will default to the moment of execution if not specified.
.EXAMPLE
Show-MessageRecipients -from [email protected] -to [email protected] -subject "Test e-mail" -start "6-8-2011 9:00 AM" -end "6-8-2011 9:01 AM"
Description
-----------
Returns the recipients of any messages sent after 9:00:05 AM on
6/8/2011 and before 9:01:38 AM on 6/8/2011 with the subject of
"Test e-mail" sent from [email protected] to [email protected]
.NOTES
Requires the Loading of the Exchange Module.
#>
# Initialization Section
# Parameters called so that they can be passed into the script via the command line.
[CmdletBinding()]
param (
[Parameter(Position = 0, Mandatory = $false)]
[String]
$from=$null,
[Parameter(Position = 1, Mandatory = $false)]
[String]
$to=$null,
[Parameter(Position = 2, Mandatory = $false)]
[String]
$subject=$null,
[Parameter(Position = 3, Mandatory = $false)]
[system.datetime]
$start,
[Parameter(Position = 4, Mandatory = $false)]
[system.datetime]
$end)
[string[]]$rcpts=@()
$case=0
if (-not ($global:ExchangeSnapIn))
{
[string]$errorstring="WARNING: Exchange module is not loaded, some cmdlets will not work correctly, exiting...`n"
$Error.add($errorstring)
write-error ($errorstring) -erroraction stop
}
if ($start -eq $null)
{
$start=(get-date).adddays(-30)
}
if ($end -eq $null)
{
$end=get-date
}
if ($from -ne "")
{
$case=$case+1
}
if ($to -ne "")
{
$case=$case+2
}
if ($subject -ne "")
{
$case=$case+4
}
# $case is used to determine which get-messagetrackinglog parameters are specified
switch ($case)
{
"0"
{
# collects all of the transport servers (hubs and edge if present)
$servers=Get-TransportServer
# Collects all of the mailbox servers
$servers+=Get-MailboxServer
foreach ($server in $servers)
{
write-host ("Searching Logs on $server...")
# DELIVER event is the event that identifies that a message made it to the users' mailbox
$collection=get-messagetrackinglog -Server $server -EventId "DELIVER" -Start $start -End $end -resultsize unlimited
foreach ($msg in $collection)
{
# $msg.Recipients has the recipients that the DELIVER event applies to
$msgarray=$msg.Recipients
foreach ($rcpt IN $msgarray)
{
$rcpts+=$rcpt
}
}
}
}
"1"
{
$servers=Get-TransportServer
$servers+=Get-MailboxServer
foreach ($server in $servers)
{
write-host ("Searching Logs on $server...")
$collection=get-messagetrackinglog -Sender $from -Server $server -EventId "DELIVER" -Start $start -End $end -resultsize unlimited
foreach ($msg in $collection)
{
$msgarray=$msg.Recipients
foreach ($rcpt IN $msgarray)
{
$rcpts+=$rcpt
}
}
}
}
"2"
{
$servers=Get-TransportServer
$servers+=Get-MailboxServer
foreach ($server in $servers)
{
write-host ("Searching Logs on $server...")
$collection=get-messagetrackinglog -Recipient $to -Server $server -EventId "DELIVER" -Start $start -End $end -resultsize unlimited
foreach ($msg in $collection)
{
$msgarray=$msg.Recipients
foreach ($rcpt IN $msgarray)
{
$rcpts+=$rcpt
}
}
}
}
"3"
{
$servers=Get-TransportServer
$servers+=Get-MailboxServer
foreach ($server in $servers)
{
write-host ("Searching Logs on $server...")
$collection=get-messagetrackinglog -Sender $from -Recipient $to -Server $server -EventId "DELIVER" -Start $start -End $end -resultsize unlimited
foreach ($msg in $collection)
{
$msgarray=$msg.Recipients
foreach ($rcpt IN $msgarray)
{
$rcpts+=$rcpt
}
}
}
}
"4"
{
$servers=Get-TransportServer
$servers+=Get-MailboxServer
foreach ($server in $servers)
{
write-host ("Searching Logs on $server...")
$collection=get-messagetrackinglog -Server $server -EventId "DELIVER" -messagesubject "$subject" -Start $start -End $end -resultsize unlimited
foreach ($msg in $collection)
{
$msgarray=$msg.Recipients
foreach ($rcpt IN $msgarray)
{
$rcpts+=$rcpt
}
}
}
}
"5"
{
$servers=Get-TransportServer
$servers+=Get-MailboxServer
foreach ($server in $servers)
{
write-host ("Searching Logs on $server...")
$collection=get-messagetrackinglog -Sender $from -Server $server -EventId "DELIVER" -messagesubject "$subject" -Start $start -End $end -resultsize unlimited
foreach ($msg in $collection)
{
$msgarray=$msg.Recipients
foreach ($rcpt IN $msgarray)
{
$rcpts+=$rcpt
}
}
}
}
"6"
{
$servers=Get-TransportServer
$servers+=Get-MailboxServer
foreach ($server in $servers)
{
write-host ("Searching Logs on $server...")
$collection=get-messagetrackinglog -Recipient $to -Server $server -EventId "DELIVER" -messagesubject "$subject" -Start $start -End $end -resultsize unlimited
foreach ($msg in $collection)
{
$msgarray=$msg.Recipients
foreach ($rcpt IN $msgarray)
{
$rcpts+=$rcpt
}
}
}
}
"7"
{
$servers=Get-TransportServer
$servers+=Get-MailboxServer
foreach ($server in $servers)
{
write-host ("Searching Logs on $server...")
$collection=get-messagetrackinglog -Sender $from -Recipient $to -Server $server -EventId "DELIVER" -messagesubject "$subject" -Start $start -End $end -resultsize unlimited
foreach ($msg in $collection)
{
$msgarray=$msg.Recipients
foreach ($rcpt IN $msgarray)
{
$rcpts+=$rcpt
}
}
}
}
default
{
[string]$errorstring="PARAMETER ERROR, exiting...`n"
$Error.add($errorstring)
write-error ($errorstring) -erroraction stop
}
}
# De-dupes the list of recipients
$rcpts=$rcpts | Sort-Object -Unique
$rcpts | ft -auto -wrap
remove-variable servers -ErrorAction SilentlyContinue
remove-variable rcpts -ErrorAction SilentlyContinue
} # function Show-JMessageRecipients
function Add-JEWSDelegate
{
<#
.SYNOPSIS
Adds a delegate to a mailbox.
.DESCRIPTION
Adds the specified Delegate to the specified folder to a mailbox.
.PARAMETER mb
Enter the SMTP address of the mailbox being delegated
.PARAMETER viewer
Enter the SMTP address of the mailbox the will be able to view new
folders.
.PARAMETER folder
Specify the name of the Foder that the Delegate will have access to.
This will accept a comma-seperated list for multiple folders.
NOTE: This defaults to "Calendar" if it is not specified.
.PARAMETER perms
Specify the Permissions to grant from the following list:
None, Contributor, Reviewer, Author, Editor.
NOTE: This defaults to "Reviewer" if it is not specified.
.PARAMETER copy
Specify if Delegate should receive a copy of Meeting-related Messages.
.EXAMPLE
Add-JEWSDelegate -mb [email protected] -viewer [email protected] -folder Calendar
Description
-----------
Allows Randy Moore to be able to view Calendar of thr Journal Social
Networking Mailbox.
.NOTES
Requires EWS to be installed on the computer that executes this
script.
#>
[CmdletBinding()]
param (
[Parameter(Position = 0, Mandatory = $true)]
[Alias("Identity")]
[Alias("ID")]
[String]
$mb=$(Throw "You must specify a Calendar to add a delegate to (e.g., [email protected])."),
[Parameter(Position = 1, Mandatory = $true)]
[Alias("user")]
[String]
$viewer=$(Throw "You must specify a delegate to add (e.g., [email protected])."),
[Parameter(Position = 2, Mandatory = $false)]
[String[]]
[ValidateSet(
'Cal',
'Calendar',
'Contact',
'Contacts',
'Inbox',
'Note',
'Notes',
'Task',
'Tasks'
)]
$folder="Calendar",
[Parameter(Position = 3, Mandatory = $false)]
[Alias("permissions")]
[Alias("permission")]
[String]
[ValidateSet(
'Editor',
'Author',
'Reviewer',
'Contributor',
'None'
)]
$perms="Reviewer",
[Parameter(Position = 4, Mandatory = $false)]
[Alias("ReceiveCopiesOfMeetingRequests")]
[Alias("ReceiveCopiesOfMeetingMessages")]
[switch]
$copy
)
# Initialization
# Sets up access to the EWS DLL
$EWSFile=$null
if (test-path "D:\Program Files\Microsoft\Exchange\Web Services\1.1\Microsoft.Exchange.WebServices.dll") {$EWSFile="D:\Program Files\Microsoft\Exchange\Web Services\1.1\Microsoft.Exchange.WebServices.dll"}
if (test-path "C:\Program Files\Microsoft\Exchange\Web Services\1.1\Microsoft.Exchange.WebServices.dll") {$EWSFile="C:\Program Files\Microsoft\Exchange\Web Services\1.1\Microsoft.Exchange.WebServices.dll"}
if (test-path "D:\Program Files\Microsoft\Exchange\Web Services\1.2\Microsoft.Exchange.WebServices.dll") {$EWSFile="D:\Program Files\Microsoft\Exchange\Web Services\1.2\Microsoft.Exchange.WebServices.dll"}
if (test-path "C:\Program Files\Microsoft\Exchange\Web Services\1.2\Microsoft.Exchange.WebServices.dll") {$EWSFile="C:\Program Files\Microsoft\Exchange\Web Services\1.2\Microsoft.Exchange.WebServices.dll"}
$test=Get-Module -ListAvailable | where {$_.name -like "EWSMail"}
if (($test -ne $null) -and($EWSFile -ne $null)) {
$dllpath = $EWSFile
[void][Reflection.Assembly]::LoadFile($dllpath)
Add-Type -Path $EWSFile
Remove-Module EWSMail -erroraction silentlyContinue
Import-Module EWSMail -erroraction silentlyContinue
$global:EWSModule=$?
}
# Initates an object with the EWS services as methods
$service = new-object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2007_SP1)
# Filters and Functions
# switch is case sensitive, converting $folder to lower case makes the switch statement less complicated
$folder=$folder.tolower()
# $global:ExchangeSnapIn is a variable that is created and set to true in the Janus Module if the snapin is loaded
if (-not ($global:ExchangeSnapIn))
{
[string]$errorstring="WARNING: Exchange module is not loaded, some cmdlets will not work correctly, exiting...`n"
$Error.add($errorstring)
write-error ($errorstring) -erroraction stop
}
# $global:EWSModule is a variable that is created and set to true in the Janus Module if EWS is loaded
if (-not ($global:EWSModule))
{
[string]$errorstring="WARNING: EWS module is not loaded, come cmdlets will not work correctly...`nExiting...`n"
$Error.add($errorstring)
write-error ($errorstring) -erroraction stop
}
# Error checking
# does $mb exist?
$test=get-mailbox -id $mb
$successful=$?
if ($successful -eq $false)
{
[string]$errorstring="Error in Mailbox ID, cannot locate mailbox, exiting...`n"
$Error.add($errorstring)
write-error ($errorstring) -erroraction stop
} else
{
$mb=$test.PrimarySmtpAddress.tostring()
}
# does $viewer exist?
$test=get-jadentry -id $viewer -exact -pso -properties msexchrecipientdisplaytype,mail
$successful=$?
if (($successful -eq $false) -or($test.msexchrecipientdisplaytype=$null) -or($test.msexchrecipientdisplaytype=""))
{
[string]$errorstring="Error in Delegate ID, cannot locate mailbox, exiting...`n"
$Error.add($errorstring)
write-error ($errorstring) -erroraction stop
} else
{
$viewer=$test.mail
}
# Main Script
# Try allows you to catch an error. In this case, an error is generated if p-uccas03 is returned because it is a re-direct in http
try
{
$service.AutodiscoverUrl($mb)
}
# Manually assigns the url if there is an error
Catch [system.exception]
{
$URI='https://p-ucusxhc04.janus.cap/EWS/Exchange.asmx'
$service.URL = New-Object Uri($URI)
write-output ("Caught an Autodiscover URL exception, recovering...")
}
# Sets the EWS service to impersonate the mailbox you are modifying
$service.ImpersonatedUserId = new-object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress, $mb);
# Sets up a EWS Mailbox Object
$mbMailbox = new-object Microsoft.Exchange.WebServices.Data.Mailbox($mb)
# Sets up a EWS Delegate User Object
$dgUser = new-object Microsoft.Exchange.WebServices.Data.DelegateUser($viewer)
# process for Calendars
If ($folder -like "*cal*")
{
Write-Output ("Updating Calendar")
# Sets the permissions for the Delegate
$dgUser.Permissions.CalendarFolderPermissionLevel = [Microsoft.Exchange.WebServices.Data.DelegateFolderPermissionLevel]::$perms
# If the Delegate can act on the Calendar messages, they will receive a copy
if (($copy -eq $true) -and($perms -like "Editor")) {$dgUser.ReceiveCopiesOfMeetingMessages = $true}
else {$dgUser.ReceiveCopiesOfMeetingMessages = $false}
}
# Process for Inbox
If ($folder -like "*inbox*")
{
Write-Output ("Updating Inbox")
$dgUser.Permissions.InboxFolderPermissionLevel = [Microsoft.Exchange.WebServices.Data.DelegateFolderPermissionLevel]::$perms
}
Process for Tasks folder
If ($folder -like "*task*")
{
Write-Output ("Updating Tasks")
$dgUser.Permissions.TasksFolderPermissionLevel = [Microsoft.Exchange.WebServices.Data.DelegateFolderPermissionLevel]::$perms
}
# Process for Contacts folder
If ($folder -like "*contact*")
{
Write-Output ("Updating Contacts")
$dgUser.Permissions.ContactsFolderPermissionLevel = [Microsoft.Exchange.WebServices.Data.DelegateFolderPermissionLevel]::$perms
}
# Prodcess for Notes Folder
If ($folder -like "*note*")
{
Write-Output ("Updating Notes")
$dgUser.Permissions.NotesFolderPermissionLevel = [Microsoft.Exchange.WebServices.Data.DelegateFolderPermissionLevel]::$perms
}
# Don't give delegates access to private items
$dgUser.ViewPrivateItems = $false
# Initiates an empty array
$dgArray = new-object Microsoft.Exchange.WebServices.Data.DelegateUser[] 1
# Assigned the Delegate user to the first element of the array
$dgArray[0] = $dgUser
# Sets Meeting inbvites to default behavior if they have enough permissions
if (($perms -like "Editor") -and($copy)) {$service.AddDelegates($mbMailbox, [Microsoft.Exchange.WebServices.Data.MeetingRequestsDeliveryScope]::DelegatesAndSendInformationToMe, $dgArray);}
# Makes sure that delegate does not receive meeting invites if they do not have sufficient permissions to act on those invites
else {$service.AddDelegates($mbMailbox, [Microsoft.Exchange.WebServices.Data.MeetingRequestsDeliveryScope]::NoForward, $dgArray);}
remove-variable service
remove-variable dgArray
remove-variable dgUser
remove-variable mbMailbox
remove-variable ACEUser
remove-variable WindowsIdentity
} # function Add-JEWSDelegate
function Move-JMailbox
{
<#
.SYNOPSIS
Move an Exchange Mailbox.
.DESCRIPTION
Move the designated mailbox to the specified mailbox store. This
sends an e-mail when it begins and finishes. It also sends a MOM
alert if there is an error during the move. This also Logs to the
C: drive.
.PARAMETER user
Enter the Mailbox to move.
NOTE: Accepts the following data:
* GUID
* Distinguished name (DN)
* Domain\Account
* User principal name (UPN)
* LegacyExchangeDN
* SmtpAddress
* Alias
.PARAMETER store
Specify the target Mailbox Database Store.
.EXAMPLE
Move-JMailbox -user "Moore, Randy" -Store "ucmailv03\Mailbox Store 3-1"
Description
-----------
Moves Randy's mailbox to the Mailbox Store 3-1 on the ucmailv03
server,
.NOTES
Requires the Loading of the Exchange Module.
#>
# Initialization Section
# Parameters called so that they can be passed into the script via the command line.
[CmdletBinding()]
param (
[Parameter(Position = 0, Mandatory = $true)]