-
Notifications
You must be signed in to change notification settings - Fork 0
/
export-disabledmailbox.ps1
304 lines (211 loc) · 12.6 KB
/
export-disabledmailbox.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
# Export and remove from Exchange old mailboxes
# Check script body and change it for you task
# Exchange account with export rights
$exchange_admin = 'exchange-admin@BGELOV.ru'
# Exchange server
$exchange_server = "EXCHANGE-SERVER"
# Domain name in some formats
$domain_name = "BGELOV\\"
$domain_name_simple = "BGELOV"
# Work directory
$path = 'D:\automation'
# Path for export
$export_path = '\\server\pst'
# Database name with dismiss users
$DisabledMdb_Name = "DisabledMDB"
$mailboxTrue = ""
$mailboxFalse = ""
$DisabledMDB = ''
if (!(Test-Path "$path\dismiss_users.txt")) {
# Write your algorithm for getting dismissed users
Write-Host "=== Get users who were fired more than a certain date ===" -ForegroundColor Yellow
try
{
# You can using database or other way for getting dismissed users
}
catch [System.Data.Odbc.OdbcException]
{
$_.Exception
$_.Exception.Message
$_.Exception.ItemName
}
"" > "$path\dismiss_users.txt"
}
# Connect to Exchange
$UserCredential = Get-Credential $exchange_admi
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "http://$exchange_server/PowerShell/" -Authentication Kerberos -Credential $UserCredential
Import-PSSession $Session
# Check mailboxes for dismissed users
if (!((Test-Path "$path\mailboxTrue.txt") -and (Test-Path "$path\mailboxFalse.txt"))) {
Write-Host "=== Check mailboxes for dicmissed users ===" -ForegroundColor Yellow
$list = gc "$path\dismiss_users.txt"
foreach ($l in $list) {
if (get-mailbox $l -ErrorAction SilentlyContinue) { $mailboxTrue += "$l`n" } else { $mailboxFalse += "$l`n" }
}
$mailboxTrue > "$path\mailboxTrue.txt"
$mailboxFalse > "$path\mailboxFalse.txt"
}
# Getting users in DisabledMDB
Write-Host "=== Getting users in DisabledMDB ===" -ForegroundColor Yellow
if (Test-Path "$path\mailboxTrue.txt") {
$mailboxTrue = gc "$path\mailboxTrue.txt"
foreach ($mbx in $mailboxTrue) {
if ($mbx) {
Write-Host "$mbx"
$nss = (get-mailbox $mbx).database
Write-Host "$nss"
if ("$nss" -eq 'DisabledMDB') { $DisabledMDB += "$mbx`n" }
}
}
$DisabledMDB > "$path\DisabledMDB.txt"
}
# Export to .pst
Write-Host "=== Starting export to .pst ===" -ForegroundColor Yellow
$result_file = "$path\not_export.txt"
$result_file_OK = "$path\export.txt"
# OU if you need some exclude rules
$OU_contacts = 'OU=Contacts,DC=bgelov,DC=ru'
$OU_contacts2 = 'bgelov.ru/Contacts'
$OU_contacts3 = 'bgelov.ru/NewUsers'
$OU_disableU = 'bgelov.ru/Archive'
$OU_subworkers2 = 'bgelov.ru/SubUsers'
if (Test-Path $result_file) { Remove-Item -Path $result_file -Force }
if (Test-Path $result_file_OK) { Remove-Item -Path $result_file_OK -Force }
# Disabled users from DisabledMDB.txt
$user = gc "$path\$DisabledMdb_Name.txt"
$curDate = (Get-Date).AddMonths(-3)
$Date3Month = Get-Date $curDate -f "yyyy-MM-dd HH:mm:ss"
function check-sendas ($u)
{
$login = $login_enabled = $null
$flag = $false
$u_mbx_SendAs = (Get-Mailbox -identity $u | Get-ADPermission | where {($_.ExtendedRights -like "*Send-As*") -and ($_.IsInherited -eq $false) -and -not (($_.User -like "NT AUTHORITY\SELF") -or ($_.User -like "$domain_name_simple\Message Processors") -or ($_.User -like "S-1-5-21-1069761793-*"))}).user
if ($u_mbx_SendAs) {
foreach ($u_mbx_S in $u_mbx_SendAs) {
$login = $u_mbx_S
$login = $login -replace $domain_name,""
$login_enabled = (Get-ADUser $login -Properties Enabled | select Enabled).Enabled
if ($login_enabled -eq $false) { } else { $flag = $true }
$login = $login_enabled = $null
}
}
if ($flag -eq $true) { return $u_mbx_SendAs } else { return $null }
}
function check-fullaccess ($u)
{
$login = $login_enabled = $null
$flag = $false
$u_mbx_perm = (Get-MailboxPermission $u | where {($_.AccessRights -eq "FullAccess") -and ($_.IsInherited -eq $False) -and -not (($_.User -like "NT AUTHORITY\SYSTEM") -or ($_.User -like "NT AUTHORITY\SELF") -or ($_.User -like "$domain_name_simple\ExchangeAdministrator") -or ($_.User -like "$domain_name_simple\Administrator") -or ($_.User -like "$domain_name_simple\Domain Admins") -or ($_.User -like "$domain_name_simple\Enterprise Admins"))}).user
if ($u_mbx_perm) {
foreach ($u_mbx_p in $u_mbx_perm) {
$login = $u_mbx_p
$login = $login -replace $domain_name,""
$login_enabled = (Get-ADUser $login -Properties Enabled | select Enabled).Enabled
if ($login_enabled -eq $false) { Remove-MailboxPermission -Identity $u -User $login -AccessRights FullAccess -InheritanceType All -Confirm:$false } else { $flag = $true }
$login = $login_enabled = $null
}
$u_mbx_perm = (Get-MailboxPermission $u | where {($_.AccessRights -eq "FullAccess") -and ($_.IsInherited -eq $False) -and -not (($_.User -like "NT AUTHORITY\SYSTEM") -or ($_.User -like "NT AUTHORITY\SELF") -or ($_.User -like "$domain_name_simple\ExchangeAdministrator") -or ($_.User -like "$domain_name_simple\Administrator") -or ($_.User -like "$domain_name_simple\Domain Admins") -or ($_.User -like "$domain_name_simple\Enterprise Admins"))}).user
}
if ($flag -eq $true) { return $u_mbx_perm } else { return $null }
}
foreach ($u in $user) {
$u_mbx = $u_acc = $u_mbx_frwrd = $u_mbx_frwrd_DN = $u_mbx_SendAs = $u_mbx_perm = $null
# Exclude users
if (!(($u -eq 'TESTuser') -or ($u -eq 'TESTuser2') -or ($u -eq 'TESTuser3') -or ($u -eq ''))) {
# Check user mailbox
if ($u_mbx = get-mailbox $u -ErrorAction SilentlyContinue | select SamAccountName, ForwardingAddress) {
# Check enabled or disabled account
$u_acc = Get-ADUser $u -Properties Enabled, extensionAttribute6 | select Enabled, PasswordLastSet, extensionAttribute6
#if (!($u_acc.Enabled)) {
# Checkmeddage count
$message_count = get-mailbox $u -ErrorAction SilentlyContinue | Get-MailboxStatistics | select itemcount -ExpandProperty itemcount
$message_count
if (!($u_acc.Enabled)) {
# Check users with specific attribute and less count of messages in mailbox
#if ((!($u_acc.Enabled) -and ($u_acc.extensionAttributeX -eq 1) -and ($message_count -le 50))) {
#if ((!($u_acc.Enabled) -and ($u_acc.extensionAttributeX -eq 1) -and ($message_count -le 50))) {
# Check date when user changed password
if (!($u_acc.PasswordLastSet -gt $Date3Month)) {
# Check last login
#if ((Get-MailboxStatistics $u | select LastLogonTime).LastLogonTime -lt $Date3Month) {
# Check message forwarfing. If forwarding to contact or dismissed user, then continue
$u_mbx_frwrd = $u_mbx.ForwardingAddress
$u_mbx_frwrd_DN = $u_mbx_frwrd.Parent.DistinguishedName
if ((!($u_mbx_frwrd)) -or ($u_mbx_frwrd_DN -like "*$OU_contacts") -or ($u_mbx_frwrd -like "$OU_contacts2*") -or ($u_mbx_frwrd -like "$OU_contacts3*") -or ($u_mbx_frwrd -like "$OU_disableU*") -or ($u_mbx_frwrd -like "$OU_subworkers2*")) {
# Check SendAs rights
#$u_mbx_SendAs = check-sendas $u
#if (!$u_mbx_SendAs) {
# Check FullAccess rights on mailbox
$u_mbx_perm = check-fullaccess $u
if (!$u_mbx_perm) {
# Check if this mailbox already with export process...
$u_status = (Get-MailboxExportRequest -Name $u | select status).status
if ((!$u_status) -or (!(Test-Path "$export_path\$u.pst"))) {
# Export mailbox
Write-Host "Start export to $export_path\$u.pst" -ForegroundColor Green
New-MailboxExportRequest -Name $u -AcceptLargeDataLoss -Confirm:$False -BadItemLimit 999 -mailbox $u -FilePath "$export_path\$u.pst"
Add-Content "$u" -Path $result_file_OK
} else { Write-Host "Mailbox $u already in export process" -ForegroundColor Yellow
Add-Content "$u" -Path $result_file_OK }
} else { Write-Host "On $u assigned FullAccess rights!`n$u_mbx_perm" -ForegroundColor Yellow
Add-Content "$u" -Path $result_file }
#} else { Write-Host "On $u mailbox settings up Send-As rights!`n$u_mbx_SendAs" -ForegroundColor Yellow
# Add-Content "$u" -Path $result_file }
} else { Write-Host "For $u mailbox settings up mail forwarding to $u_mbx_frwrd!" -ForegroundColor Yellow
Add-Content "$u" -Path $result_file }
#} else { Write-Host "Login in this account less the 3 mounth ago!" -ForegroundColor Yellow
# Add-Content "$u" -Path $result_file }
} else { Write-Host "Password for $u changed less then 3 months ago!" -ForegroundColor Yellow
Add-Content "$u" -Path $result_file }
} else { Write-Host "Mailbox $u is in enabled state" -ForegroundColor Yellow
Add-Content "$u" -Path $result_file }
} else { Write-Host "У $u нет почтового ящика!" -ForegroundColor Yellow
$user = $user -replace $u,$null }
} # Excludes
}
# Delete mailboxes which doesn't exist
$user > "$path\NewDisabledMDB2.txt"
Remove-Item "$path\NewDisabledMDB.txt" -Force
Rename-Item "$path\NewDisabledMDB2.txt" "NewDisabledMDB.txt"
# Disable mailboxes what succesfully export to pst
#https://technet.microsoft.com/ru-ru/library/aa995948(v=exchg.160).aspx
$path = 'D:\automation'
$result_file_OK = "$path\export.txt"
if (Test-Path $result_file_OK) {
Get-MailboxExportRequest | Get-MailboxExportRequestStatistics
Write-Host "=== Disable mailboxes what succesfully export to pst ===" -ForegroundColor Yellow
$user = gc -Path $result_file_OK
do
{
$flag = $false
foreach ($u in $user) {
$u_status = $null
$u_status = (Get-MailboxExportRequest -Name $u | select status).status
if ($u_status) {
# Check export process
if ($u_status -eq 'Completed') {
if (Test-Path "$export_path\$u.pst") {
Write-Host "Disable mailbox $u" -ForegroundColor Green
Disable-Mailbox $u -Confirm:$false
Get-MailboxExportRequest -Name $u | Remove-MailboxExportRequest -Confirm:$false
} else { Write-Host "No exist .pst file for $u" -ForegroundColor Yellow }
} elseif ($u_status -eq 'InProgress') {
Write-Host "Export $u mailbox in process..." -ForegroundColor DarkYellow
$flag = $true
} elseif (($u_status -eq 'Failed') -or ($u_status -eq 'FailedOther')) { Write-Host "Export error for $u mailbox" -ForegroundColor Red
Get-MailboxExportRequest -Name $u | Remove-MailboxExportRequest -Confirm:$false
} elseif ($u_status -eq 'Queued') {
Write-Host "$u in queue" -ForegroundColor Yellow
$flag = $true
} else { Write-Host "$u - $u_status" -ForegroundColor Yellow }
} else { # Write-Host "No export task for $u mailbox" -ForegroundColor Red
}
}
if ($flag -eq $true) { sleep -Seconds 300 }
Write-Host "===========================================================" -ForegroundColor Yellow
}
while ($flag -eq $true)
Write-Host "=== Script Ended ===" -ForegroundColor Yellow
Get-MailboxExportRequest | Get-MailboxExportRequestStatistics
} else { Write-Host "No mailboxes for export and disabling" -ForegroundColor Green }
Remove-PSSession $Session