-
Notifications
You must be signed in to change notification settings - Fork 0
/
MBExport.ps1
125 lines (97 loc) · 3.66 KB
/
MBExport.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
# Mailbox Export
# Version 1.1.0
#
# Run bulk mailbox exports as a scheduled task.
# Copyright (C) 2016 Simon Lehmann
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Script version
$ver = 1.1.0
# Destination share (format: \\MACHINENAME\Path\To\Share)
$serverShare = "\\SERVER\Export"
$scriptStart = (Get-Date)
# Get today's date
$dirDate = Get-Date -format yyyy-MM-dd_hh-mm-ss
echo $dirDate
# Add Exchange PowerShell module
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn;
function DeleteRequests($requests)
{
if ($requests.count -gt 0)
{
echo “$($requests.Count) requests to remove”
foreach ($request in $requests) {
Remove-MailboxExportRequest -Identity:$request -Confirm:$False
}
echo "Complete..."
}
else
{
echo "No requests to remove"
}
}
# ----- MAILBOX EXPORT REQUEST CLEANUP ----- #
$existingRequests = Get-MailboxExportRequest -Status Completed
DeleteRequests($existingRequests)
# ----- MAILBOX EXPORT REQUEST CREATION ----- #
# Create dated folder
New-Item $serverShare\$dirDate -type directory
# Get all mailboxes
$mailboxes = Get-Mailbox
# Export all mailboxes to PST files with names based on mailbox aliases (to use a different mailbox property replace the phrase “Alias” with its name):
$mailboxes|%{$_|New-MailboxExportRequest -FilePath $serverShare\$dirDate\$($_.Alias).pst}
# ----- MAILBOX EXPORT REQUEST REMOVAL ----- #
$allCompleted = $false
#echo $result
$pass = 0
$completed = 0
while($pass -ne 360)
{
$pass++
echo "Mailbox Removal Pass $pass"
if ($allCompleted -eq $false) {
$allCompleted = $true
# Get all mailbox export requests
$results = Get-MailboxExportRequest
foreach ($result in $results) {
# Check if current mailbox export request has completed
if ($result.Status -eq "Completed") {
# Remove current mailbox export request
Remove-MailboxExportRequest -Identity:$result -Confirm:$False
$completed++
}
else {
echo "Incomplete!"
$allCompleted = $false
}
}
echo "$completed of $($mailboxes.count) requests completed and removed..."
}
else {
echo "about to break"
break
}
# wait 15 seconds
Start-Sleep -s 10
}
echo "Broken"
$processed = $mailboxes.Count
$scriptEnd = (Get-Date)
$runTime = New-Timespan -Start $scriptStart -End $scriptEnd
$elapsedTime = “{0}:{1}:{2}” -f $runTime.Hours,$runtime.Minutes,$runTime.Seconds
echo "Elapsed Time: $elapsedTime"
# ----- SEND LOG EMAIL ----- #
$PSEmailServer = "localhost"
Send-MailMessage -From "[email protected]" -To "[email protected]" -Subject "Mailbox Export Successful" -Body "Mailbox export job completed successfully.`n`nProcessed $processed mailboxes.`n`nElapsed Time: $elapsedTime`n`nMailbox export request removal passes: $pass`n`nExchange Mailbox Exporter Version $ver"