由於需要進行 2010 to 2016 的遷移, 就寫了一個小程式來自動判斷 DB 大小
然後把 User 自動遷移到當下最小的一個 DB 去
# ----- 參數設定 -------------------------------------------------------------------------------------------
# 遷移的方式
# DB: 將一個 DB 內的所有 MailBox 全部遷移
# TXT: 提供一個 TXT 檔, 遷移 TXT 檔中的 User Account
$MoveMethod = "TXT"
# 要遷移的 DB
# 如果 MoveMethod 是 DB 的話, 這裡要設定來源 DB, DB 中所有的 MailBox 都會搬移
$DBToMove = "SourceDB"
# 要遷移的 User Account 一行一個 User 寫到純文字檔中
# 跟 PowerShell 的 .ps1 檔放在相同路徑
$UserToMove = "Users.txt"
# 每遷移幾個 User 就判斷一次最小 DB
$UserTurn = 10
$DBNameArray = @()
# 要遷入的 DB Name
$DBNameArray += "TargetDB2"
$DBNameArray += "TargetDB3"
$DBNameArray += "TargetDB4"
$DBNameArray += "TargetDB5"
# 自動將完成的 Job 刪除 (1:刪除 / 0: 不刪除)
$AutoRemoveCompletedJob = 1
# 測試執行, 會將所有 Command 寫入 Log 但不會執行
$TestRun = 0
# ----- 程式開始 -------------------------------------------------------------------------------------------
Function RemoveCompletedJob {
if ($TestRun -ne 1) {
$ProcessingJob = Get-MoveRequest | Where {($_.Status -eq "InProgress") -or ($_.Status -eq "Queued")}
While ($ProcessingJob.Count -ne 0) {
Start-Sleep -m 1
$ProcessingJob = Get-MoveRequest | Where {($_.Status -eq "InProgress") -or ($_.Status -eq "Queued")}
};
$CompleteJob = Get-MoveRequest | Where {($_.Status -eq "Completed") -or ($_.Status -eq "Failed")}
Foreach ($Job in $CompleteJob) {
if ($AutoRemoveCompletedJob -eq 1) {
Write-Host "Remove $($Job.Status) Job: $($Job.Identity)"
Remove-MoveRequest -Identity $Job.Identity -Force -Confirm:$False
$ProcessCount -= 1
};
};
};
};
$DateTimeString = Get-Date -format yyyyMMdd_HHmmss
$TranscriptLog = (Get-Item -Path ".\" -Verbose).FullName + "\Logs\" + ("MoveUsers_" + $DateTimeString + ".log")
Start-Transcript -path $TranscriptLog
if ($TestRun -eq 1) {
Write-Host ""
Write-Host "This is Test Run."
Write-Host ""
};
if ($MoveMethod -eq "TXT") {
$Users = Get-Content -Path $UserToMove
} elseif ($MoveMethod -eq "DB") {
$Users = Get-MailBox -Database $DBToMove | Select -ExpandProperty Name
} else {
Write-Host "Please Check Move Method."
exit;
};
$UserCount = 0
$TargetDB = ""
Foreach ($User in $Users) {
if ($($UserCount%$UserTurn) -eq 0) {
RemoveCompletedJob
Start-Sleep -m 1
$TargetDB = $(Get-MailboxDatabase -Status | where {$DBNameArray.Contains($_.Name)} | select Name,DatabaseSize | Sort-Object -Property DatabaseSize | Select-Object -Property Name -first 1).Name
};
if ($TargetDB -ne "") {
$UserCount += 1
if (!$(Get-MailBox -Identity $User -ErrorAction SilentlyContinue).Name) {
Write-Host "User: $User Does Not Have a MailBox, Skippied, Please Check."
} else {
Write-Host "Move $User to $TargetDB."
Write-Host "Command: New-MoveRequest -Identity $User -TargetDatabase $TargetDB"
if ($TestRun -ne 1) {
$MoveJob = New-MoveRequest -Identity $User -TargetDatabase $TargetDB -ErrorAction SilentlyContinue
Clear-Variable -Name MoveJob
};
};
};
};
RemoveCompletedJob
Stop-Transcript
沒有留言:
張貼留言