-
Notifications
You must be signed in to change notification settings - Fork 4
/
XM04-CSV-Mover (2).ps1
375 lines (318 loc) · 18.4 KB
/
XM04-CSV-Mover (2).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
# **********************************************************************************
#
# Script Name: XM04-CSV-Mover.ps1
# Version: 1.0
# Author: Dave M
# Date Created: 8-9-12
# _______________________________________
#
# MODIFICATIONS:
# Date Modified: N/A
# Modified By: N/A
# Reason for modification: N/A
# What was modified: N/A
#
# Description: Moves users based on the contents of \\janus.cap\groups$\IT\ITSM and Exchange\ExchangeMigrationDailySchedule.csv
#
# Usage:
# ./XM04-CSV-Mover.ps1
#
# **********************************************************************************
<#
.SYNOPSIS
Automates User Moves
.DESCRIPTION
Moves users based on the contents of \\janus.cap\groups$\IT\ITSM and Exchange\ExchangeMigrationDailySchedule.csv
.EXAMPLE
./XM04-CSV-Mover.ps1
.NOTES
Requires Exchange 2020 SnapIn and Janus PS Module.
#>
# Functions and Filters
# Main Script
# The Begin section executes once regardless of how many objects are passed through the pipeline
begin
{
$test=Show-KVSTemp
$global:JanusPSModule=$?
if ($global:JanusPSModule -ne $true) {Import-Module Janus -erroraction stop}
# Unload X2k7 snapins
Get-PSSnapin | where {$_.name -like "*exchange*"} | Remove-PSSnapin
$test=Get-DatabaseAvailabilityGroup
$global:E2010SnapIn=$?
# Load Exchange 2010 snapin if it is not already
if ($global:E2010SnapIn -ne $true)
{
. 'C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1'; Connect-ExchangeServer -auto
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010
$test=Get-DatabaseAvailabilityGroup
$success=$?
If ($success -ne $true)
{
write-error ("Unable to load Exchange 2010 Module, exiting...") -erroraction stop
} else
{
$global:E2010SnapIn = $true
}
}
$cdatetime=$null
$Schedule=$null
$target=$null
$XM4_1=$null
$XM4_2=$null
$XM4_3=$null
$XM4_4=$null
$XM4_5=$null
$XM4_6=$null
$XM4_7=$null
$XM4_8=$null
$csv=$null
$SUsers=$null
$RTime=get-date -Format MMddyyHHmm
$sname=gc env:computername
do {
$csv=Import-Csv "\\janus.cap\groups$\IT\ITSM and Exchange\ExchangeMigrationDailySchedule.csv"
$success=$?
} while ($success -ne $true)
$SUsers=$CSV | where { [system.datetime]$_.Day_Time -gt (get-date).addminutes(-15) -and [system.datetime]$_.Day_Time -lt (get-date) }
}
# The process section runs once for each object in the pipeline
process
{
# This will gather the mailbox info for users destined to mobe to USXM04-DB1 and p[ut them into an array
$XM4_1=$SUsers | where { $_.Target_Store -like "USXM04-DB1" } | foreach { $_.Mailbox } | get-mailbox
$XM4_2=$SUsers | where { $_.Target_Store -like "USXM04-DB2" } | foreach { $_.Mailbox } | get-mailbox
$XM4_3=$SUsers | where { $_.Target_Store -like "USXM04-DB3" } | foreach { $_.Mailbox } | get-mailbox
$XM4_4=$SUsers | where { $_.Target_Store -like "USXM04-DB4" } | foreach { $_.Mailbox } | get-mailbox
$XM4_5=$SUsers | where { $_.Target_Store -like "USXM04-DB5" } | foreach { $_.Mailbox } | get-mailbox
$XM4_6=$SUsers | where { $_.Target_Store -like "USXM04-DB6" } | foreach { $_.Mailbox } | get-mailbox
$XM4_7=$SUsers | where { $_.Target_Store -like "USXM04-DB7" } | foreach { $_.Mailbox } | get-mailbox
$XM4_8=$SUsers | where { $_.Target_Store -like "USXM04-DB8" } | foreach { $_.Mailbox } | get-mailbox
# don't process if the array is null
if ($XM4_1 -ne "" -and $XM4_1 -ne $null -and $XM4_1 -ne " ")
{
$error.clear()
# Store is a text value, set it and forget it
$store="USXM04-DB1"
# Logging
$XM4_1 >> "D:\Scripts\Logs\USXM01-DB1.log"
# Let the oncall person know a move is happening
Send-MailMessage -SmtpServer mailman.janus.cap -From "[email protected]" -To "[email protected]" -Subject "Mailbox Move Initiation" -Body "The move of mailbox(es) $XM4_1 to $store is starting using PID $pid on $sname."
# Generate a move request for each element of the array
$XM4_1 | New-MoveRequest -targetdatabase "$store" -AcceptLargeDataLoss -baditemlimit Unlimited -DomainController p-jcdcd07.janus.cap -confirm:$false
$movesuccess=$?
# IF the move failed, we need to alert the on-call person
if ($movesuccess -ne $true)
{
Send-MailMessage -SmtpServer mailman.janus.cap -From "[email protected]" -To "[email protected]" -Subject "High Alert: Mailbox Move Failure" -Body "The move of mailbox(es) $XM4_1 just failed. User `"$XM4_1`" | get-moverequest to see a report of issues."
"ERROR - New-MoveRequest -targetdatabase `"$store`" -AcceptLargeDataLoss -baditemlimit Unlimited - Successful: $movesuccess" >> "D:\Scripts\Logs\USXM01-DB1.log"
}
else
{
Send-MailMessage -SmtpServer mailman.janus.cap -From "[email protected]" -To "[email protected]" -Subject "Mailbox Move Initiation" -Body "The move of mailbox(es) $XM4_1 to $store is starting using PID $pid on $sname."
"New-MoveRequest -targetdatabase `"$store`" -AcceptLargeDataLoss -baditemlimit Unlimited - Successful: $movesuccess" >> "D:\Scripts\Logs\USXM01-DB1.log"
}
$error | out-string >> "D:\Scripts\Logs\USXM01-DB1.log"
}
# don't process if the array is null
if ($XM4_2 -ne "" -and $XM4_2 -ne $null -and $XM4_2 -ne " ")
{
$error.clear()
# Store is a text value, set it and forget it
$store="USXM04-DB2"
# Logging
$XM4_2 >> "D:\Scripts\Logs\USXM01-DB1.log"
# Let the oncall person know a move is happening
Send-MailMessage -SmtpServer mailman.janus.cap -From "[email protected]" -To "[email protected]" -Subject "Mailbox Move Initiation" -Body "The move of mailbox(es) $XM4_2 to $store is starting using PID $pid on $sname."
# Generate a move request for each element of the array
$XM4_2 | New-MoveRequest -targetdatabase "$store" -AcceptLargeDataLoss -baditemlimit Unlimited -DomainController p-jcdcd07.janus.cap -confirm:$false
$movesuccess=$?
# IF the move failed, we need to alert the on-call person
if ($movesuccess -ne $true)
{
Send-MailMessage -SmtpServer mailman.janus.cap -From "[email protected]" -To "[email protected]" -Subject "High Alert: Mailbox Move Failure" -Body "The move of mailbox(es) $XM4_2 just failed. User `"$XM4_2`" | get-moverequest to see a report of issues."
"ERROR - New-MoveRequest -targetdatabase `"$store`" -AcceptLargeDataLoss -baditemlimit Unlimited - Successful: $movesuccess" >> "D:\Scripts\Logs\USXM01-DB1.log"
}
else
{
Send-MailMessage -SmtpServer mailman.janus.cap -From "[email protected]" -To "[email protected]" -Subject "Mailbox Move Initiation" -Body "The move of mailbox(es) $XM4_2 to $store is starting using PID $pid on $sname."
"New-MoveRequest -targetdatabase `"$store`" -AcceptLargeDataLoss -baditemlimit Unlimited - Successful: $movesuccess" >> "D:\Scripts\Logs\USXM01-DB1.log"
}
$error | out-string >> "D:\Scripts\Logs\USXM01-DB1.log"
}
# don't process if the array is null
if ($XM4_3 -ne "" -and $XM4_3 -ne $null -and $XM4_3 -ne " ")
{
$error.clear()
# Store is a text value, set it and forget it
$store="USXM04-DB3"
# Logging
$XM4_3 >> "D:\Scripts\Logs\USXM01-DB1.log"
# Let the oncall person know a move is happening
Send-MailMessage -SmtpServer mailman.janus.cap -From "[email protected]" -To "[email protected]" -Subject "Mailbox Move Initiation" -Body "The move of mailbox(es) $XM4_3 to $store is starting using PID $pid on $sname."
# Generate a move request for each element of the array
$XM4_3 | New-MoveRequest -targetdatabase "$store" -AcceptLargeDataLoss -baditemlimit Unlimited -DomainController p-jcdcd07.janus.cap -confirm:$false
$movesuccess=$?
# IF the move failed, we need to alert the on-call person
if ($movesuccess -ne $true)
{
Send-MailMessage -SmtpServer mailman.janus.cap -From "[email protected]" -To "[email protected]" -Subject "High Alert: Mailbox Move Failure" -Body "The move of mailbox(es) $XM4_3 just failed. User `"$XM4_3`" | get-moverequest to see a report of issues."
"ERROR - New-MoveRequest -targetdatabase `"$store`" -AcceptLargeDataLoss -baditemlimit Unlimited - Successful: $movesuccess" >> "D:\Scripts\Logs\USXM01-DB1.log"
}
else
{
Send-MailMessage -SmtpServer mailman.janus.cap -From "[email protected]" -To "[email protected]" -Subject "Mailbox Move Initiation" -Body "The move of mailbox(es) $XM4_3 to $store is starting using PID $pid on $sname."
"New-MoveRequest -targetdatabase `"$store`" -AcceptLargeDataLoss -baditemlimit Unlimited - Successful: $movesuccess" >> "D:\Scripts\Logs\USXM01-DB1.log"
}
$error | out-string >> "D:\Scripts\Logs\USXM01-DB1.log"
}
# don't process if the array is null
if ($XM4_4 -ne "" -and $XM4_4 -ne $null -and $XM4_4 -ne " ")
{
$error.clear()
# Store is a text value, set it and forget it
$store="USXM04-DB4"
# Logging
$XM4_4 >> "D:\Scripts\Logs\USXM01-DB1.log"
# Let the oncall person know a move is happening
Send-MailMessage -SmtpServer mailman.janus.cap -From "[email protected]" -To "[email protected]" -Subject "Mailbox Move Initiation" -Body "The move of mailbox(es) $XM4_4 to $store is starting using PID $pid on $sname."
# Generate a move request for each element of the array
$XM4_4 | New-MoveRequest -targetdatabase "$store" -AcceptLargeDataLoss -baditemlimit Unlimited -DomainController p-jcdcd07.janus.cap -confirm:$false
$movesuccess=$?
# IF the move failed, we need to alert the on-call person
if ($movesuccess -ne $true)
{
Send-MailMessage -SmtpServer mailman.janus.cap -From "[email protected]" -To "[email protected]" -Subject "High Alert: Mailbox Move Failure" -Body "The move of mailbox(es) $XM4_4 just failed. User `"$XM4_4`" | get-moverequest to see a report of issues."
"ERROR - New-MoveRequest -targetdatabase `"$store`" -AcceptLargeDataLoss -baditemlimit Unlimited - Successful: $movesuccess" >> "D:\Scripts\Logs\USXM01-DB1.log"
}
else
{
Send-MailMessage -SmtpServer mailman.janus.cap -From "[email protected]" -To "[email protected]" -Subject "Mailbox Move Initiation" -Body "The move of mailbox(es) $XM4_4 to $store is starting using PID $pid on $sname."
"New-MoveRequest -targetdatabase `"$store`" -AcceptLargeDataLoss -baditemlimit Unlimited - Successful: $movesuccess" >> "D:\Scripts\Logs\USXM01-DB1.log"
}
$error | out-string >> "D:\Scripts\Logs\USXM01-DB1.log"
}
# don't process if the array is null
if ($XM4_5 -ne "" -and $XM4_5 -ne $null -and $XM4_5 -ne " ")
{
$error.clear()
# Store is a text value, set it and forget it
$store="USXM04-DB5"
# Logging
$XM4_5 >> "D:\Scripts\Logs\USXM01-DB1.log"
# Let the oncall person know a move is happening
Send-MailMessage -SmtpServer mailman.janus.cap -From "[email protected]" -To "[email protected]" -Subject "Mailbox Move Initiation" -Body "The move of mailbox(es) $XM4_5 to $store is starting using PID $pid on $sname."
# Generate a move request for each element of the array
$XM4_5 | New-MoveRequest -targetdatabase "$store" -AcceptLargeDataLoss -baditemlimit Unlimited -DomainController p-jcdcd07.janus.cap -confirm:$false
$movesuccess=$?
# IF the move failed, we need to alert the on-call person
if ($movesuccess -ne $true)
{
Send-MailMessage -SmtpServer mailman.janus.cap -From "[email protected]" -To "[email protected]" -Subject "High Alert: Mailbox Move Failure" -Body "The move of mailbox(es) $XM4_5 just failed. User `"$XM4_5`" | get-moverequest to see a report of issues."
"ERROR - New-MoveRequest -targetdatabase `"$store`" -AcceptLargeDataLoss -baditemlimit Unlimited - Successful: $movesuccess" >> "D:\Scripts\Logs\USXM01-DB1.log"
}
else
{
Send-MailMessage -SmtpServer mailman.janus.cap -From "[email protected]" -To "[email protected]" -Subject "Mailbox Move Initiation" -Body "The move of mailbox(es) $XM4_5 to $store is starting using PID $pid on $sname."
"New-MoveRequest -targetdatabase `"$store`" -AcceptLargeDataLoss -baditemlimit Unlimited - Successful: $movesuccess" >> "D:\Scripts\Logs\USXM01-DB1.log"
}
$error | out-string >> "D:\Scripts\Logs\USXM01-DB1.log"
}
# don't process if the array is null
if ($XM4_6 -ne "" -and $XM4_6 -ne $null -and $XM4_6 -ne " ")
{
$error.clear()
# Store is a text value, set it and forget it
$store="USXM04-DB6"
# Logging
$XM4_6 >> "D:\Scripts\Logs\USXM01-DB1.log"
# Let the oncall person know a move is happening
Send-MailMessage -SmtpServer mailman.janus.cap -From "[email protected]" -To "[email protected]" -Subject "Mailbox Move Initiation" -Body "The move of mailbox(es) $XM4_6 to $store is starting using PID $pid on $sname."
# Generate a move request for each element of the array
$XM4_6 | New-MoveRequest -targetdatabase "$store" -AcceptLargeDataLoss -baditemlimit Unlimited -DomainController p-jcdcd07.janus.cap -confirm:$false
$movesuccess=$?
# IF the move failed, we need to alert the on-call person
if ($movesuccess -ne $true)
{
Send-MailMessage -SmtpServer mailman.janus.cap -From "[email protected]" -To "[email protected]" -Subject "High Alert: Mailbox Move Failure" -Body "The move of mailbox(es) $XM4_6 just failed. User `"$XM4_6`" | get-moverequest to see a report of issues."
"ERROR - New-MoveRequest -targetdatabase `"$store`" -AcceptLargeDataLoss -baditemlimit Unlimited - Successful: $movesuccess" >> "D:\Scripts\Logs\USXM01-DB1.log"
}
else
{
Send-MailMessage -SmtpServer mailman.janus.cap -From "[email protected]" -To "[email protected]" -Subject "Mailbox Move Initiation" -Body "The move of mailbox(es) $XM4_6 to $store is starting using PID $pid on $sname."
"New-MoveRequest -targetdatabase `"$store`" -AcceptLargeDataLoss -baditemlimit Unlimited - Successful: $movesuccess" >> "D:\Scripts\Logs\USXM01-DB1.log"
}
$error | out-string >> "D:\Scripts\Logs\USXM01-DB1.log"
}
# don't process if the array is null
if ($XM4_7 -ne "" -and $XM4_7 -ne $null -and $XM4_7 -ne " ")
{
$error.clear()
# Store is a text value, set it and forget it
$store="USXM04-DB7"
# Logging
$XM4_7 >> "D:\Scripts\Logs\USXM01-DB1.log"
# Let the oncall person know a move is happening
Send-MailMessage -SmtpServer mailman.janus.cap -From "[email protected]" -To "[email protected]" -Subject "Mailbox Move Initiation" -Body "The move of mailbox(es) $XM4_7 to $store is starting using PID $pid on $sname."
# Generate a move request for each element of the array
$XM4_7 | New-MoveRequest -targetdatabase "$store" -AcceptLargeDataLoss -baditemlimit Unlimited -DomainController p-jcdcd07.janus.cap -confirm:$false
$movesuccess=$?
# IF the move failed, we need to alert the on-call person
if ($movesuccess -ne $true)
{
Send-MailMessage -SmtpServer mailman.janus.cap -From "[email protected]" -To "[email protected]" -Subject "High Alert: Mailbox Move Failure" -Body "The move of mailbox(es) $XM4_7 just failed. User `"$XM4_7`" | get-moverequest to see a report of issues."
"ERROR - New-MoveRequest -targetdatabase `"$store`" -AcceptLargeDataLoss -baditemlimit Unlimited - Successful: $movesuccess" >> "D:\Scripts\Logs\USXM01-DB1.log"
}
else
{
Send-MailMessage -SmtpServer mailman.janus.cap -From "[email protected]" -To "[email protected]" -Subject "Mailbox Move Initiation" -Body "The move of mailbox(es) $XM4_7 to $store is starting using PID $pid on $sname."
"New-MoveRequest -targetdatabase `"$store`" -AcceptLargeDataLoss -baditemlimit Unlimited - Successful: $movesuccess" >> "D:\Scripts\Logs\USXM01-DB1.log"
}
$error | out-string >> "D:\Scripts\Logs\USXM01-DB1.log"
}
# don't process if the array is null
if ($XM4_8 -ne "" -and $XM4_8 -ne $null -and $XM4_8 -ne " ")
{
$error.clear()
# Store is a text value, set it and forget it
$store="USXM04-DB8"
# Logging
$XM4_8 >> "D:\Scripts\Logs\USXM01-DB1.log"
# Let the oncall person know a move is happening
Send-MailMessage -SmtpServer mailman.janus.cap -From "[email protected]" -To "[email protected]" -Subject "Mailbox Move Initiation" -Body "The move of mailbox(es) $XM4_8 to $store is starting using PID $pid on $sname."
# Generate a move request for each element of the array
$XM4_8 | New-MoveRequest -targetdatabase "$store" -AcceptLargeDataLoss -baditemlimit Unlimited -DomainController p-jcdcd07.janus.cap -confirm:$false
$movesuccess=$?
# IF the move failed, we need to alert the on-call person
if ($movesuccess -ne $true)
{
Send-MailMessage -SmtpServer mailman.janus.cap -From "[email protected]" -To "[email protected]" -Subject "High Alert: Mailbox Move Failure" -Body "The move of mailbox(es) $XM4_8 just failed. User `"$XM4_8`" | get-moverequest to see a report of issues."
"ERROR - New-MoveRequest -targetdatabase `"$store`" -AcceptLargeDataLoss -baditemlimit Unlimited - Successful: $movesuccess" >> "D:\Scripts\Logs\USXM01-DB1.log"
}
else
{
Send-MailMessage -SmtpServer mailman.janus.cap -From "[email protected]" -To "[email protected]" -Subject "Mailbox Move Initiation" -Body "The move of mailbox(es) $XM4_8 to $store is starting using PID $pid on $sname."
"New-MoveRequest -targetdatabase `"$store`" -AcceptLargeDataLoss -baditemlimit Unlimited - Successful: $movesuccess" >> "D:\Scripts\Logs\USXM01-DB1.log"
}
$error | out-string >> "D:\Scripts\Logs\USXM01-DB1.log"
}
}
# The End section executes once regardless of how many objects are passed through the pipeline
end
{
remove-variable cdatetime
remove-variable Schedule
remove-variable target
remove-variable XM4_1
remove-variable XM4_2
remove-variable XM4_3
remove-variable XM4_4
remove-variable XM4_5
remove-variable XM4_6
remove-variable XM4_7
remove-variable XM4_8
remove-variable csv
remove-variable SUsers
remove-variable RTime
remove-variable sname
}