-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathDirectSearchMailfromExchange.ps1
76 lines (63 loc) · 2.17 KB
/
DirectSearchMailfromExchange.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
function DirectSearchMailfromExchange
{
#Requires -Version 2.0
<#
.SYNOPSIS
This script will search the mail from the Exchange server and export the results to the selected mailbox.
The script needs to be executed on the Exchange server.
Author: 3gstudent
.PARAMETER MailBox
The mail you want to search.
If you set it as 'All',it will search all the mailbox.
.PARAMETER Filter
The search filter of the mail.
.PARAMETER TargetMailbox
The mailbox of the results will be export.
.PARAMETER TargetFolder
The folder of the targetmailbox.
.PARAMETER $Version
The version of the Exhange.
It can be 2007,2010,2013 and 2016.
.EXAMPLE
PS C:\> DirectSearchMailfromExchange -MailBox "test1" -Filter "*pass*" -TargetMailbox "test2" -TargetFolder "out2" -Version 2013
or
PS C:\> DirectSearchMailfromExchange -MailBox "All" -Filter "*pass*" -TargetMailbox "test2" -TargetFolder "outAll" -Version 2013
#>
param (
[Parameter(Mandatory = $True)]
[string]$MailBox,
[Parameter(Mandatory = $True)]
[string]$Filter,
[Parameter(Mandatory = $True)]
[string]$TargetMailbox,
[Parameter(Mandatory = $True)]
[string]$TargetFolder,
[Parameter(Mandatory = $True)]
[string]$Version
)
Write-Host "[>] Start to add PSSnapin"
if ($Version -eq 2007)
{
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.Admin;
}
elseif ($Version -eq 2010)
{
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010;
}
else
{
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn;
}
Write-Host "[+] Start to search mail and export the results to the selectd mailbox"
#Searche mail and export the results to the selectd mailbox
if($MailBox -eq "all")
{
Write-Host "[!] It will search from all the mailbox,it may be a long time."
Get-Mailbox|Search-Mailbox -SearchQuery $Filter -TargetMailbox $TargetMailbox -TargetFolder $TargetFolder -LogLevel Suppress| Out-Null
}
else
{
Search-Mailbox -Identity $MailBox -SearchQuery $Filter -TargetMailbox $TargetMailbox -TargetFolder $TargetFolder -LogLevel Suppress| Out-Null
}
Write-Host "[+] All done."
}