Wednesday, 18 December 2024

Analyze Exchange On-prem Databases for Local Move

 
Param(
    $Output=[System.Collections.ArrayList]@(),
    $Report=[System.Collections.ArrayList]@(),
    $MigrationStatus=[System.Collections.ArrayList]@(),
    $Val,
    $Database,
    $i=0,
    $Count=0,
    $CurrentCount=1,
    $Percent=0,
    $MbxCount,
    $DBCount,
    $Users,
    $HtmlReport = $null
)
 
#Add-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn;
 
$date = "{0:yyyy_MM_dd-HH_mm}" -f (get-date)
 
#Start-Transcript "C:\Scripts\MBX-Move\$date.txt"
 
#$date = "{0:yyyy_MM_dd-HH_mm}" -f (get-date)
 
#Start-Transcript "C:\Scripts\MBX-Move\$date.txt"
 
Write-Host `n
Write-Host "--------------------------------------------" -ForegroundColor White
Write-Host "| " -ForegroundColor White -NoNewline
Write-Host "Mailbox Database Analysis & Mailbox Move " -ForegroundColor Green -NoNewline
Write-Host "|" -ForegroundColor White 
Write-Host "--------------------------------------------" -ForegroundColor White
Write-Host `n
 
 
########## Removing Failed Mailbox Move
#Write-Host "Step 0: " -ForegroundColor Cyan -NoNewline
##Get-MoveRequest | where-Object{$_.Status -match "Fail"} | Remove-MoveRequest -Confirm:$False
#Write-Host "[Done]" -ForegroundColor Green
 
########## Importing Mailbox Database
Write-Host "Step 1: " -ForegroundColor Cyan -NoNewline
Write-Host "Importing Mailbox Database..." -ForegroundColor White -NoNewline
$Val = (Get-MailboxDatabase -Status| Where-Object{$_.name -match '2019'}|Select Name,@{n='DatabaseSize';e={($_.DatabaseSize).ToGb()}})
Write-Host "[Done]" -ForegroundColor Green
 
########## Filtering Out Journaling and Temp Database
Write-Host "Step 2: " -ForegroundColor Cyan -NoNewline
Write-Host "Filtering Out Journaling and Temp Database..." -ForegroundColor White -NoNewline
$Database = $Val| Where-Object{($_.name -notlike "*TEMP*")}| Sort DatabaseSize -Descending|Select Name,DatabaseSize
Write-Host "[Done]" -ForegroundColor Green
 
########## Fetching Database Size and Mailbox Count
Write-Host "Step 3: " -ForegroundColor Cyan -NoNewline
Write-Host "Fetching Database Size and Mailbox Count..." -ForegroundColor White -NoNewline
#Write-Host "[Please Wait]" -ForegroundColor Green
$Output.Clear()
$DBCount = ($Database|measure).Count
foreach($db in $Database){
    $Percent += (($db|measure).count/ $DBCount)* 100 
    $MbxCount = (Get-mailbox -database $db.name).count
    $Property=[pscustomobject][ordered]@{
        Name=$db.name
        Size=$db.DatabaseSize
        Mailbox=$MbxCount
    }
    $null=$Output.Add($Property)
    Write-Progress -Activity '[Running...]' -Status "[$($CurrentCount) of $($DBCount)] Fetching Details of $($db.Name)" -PercentComplete $Percent
    $CurrentCount++
}
Write-Progress -Activity '[Running...]' -Status "Completed" -Completed
Write-Host "[Done]" -ForegroundColor Green
 
########## Analyzing Database to MoveIn and MoveOut Mailboxes
Write-Host "Step 4: " -ForegroundColor Cyan -NoNewline
Write-Host "Analyzing Database to MoveIn and MoveOut Mailboxes..." -ForegroundColor White -NoNewline
 
$Output=$Output| Select Name,Size,Mailbox,
@{n='MoveIn';e={
        if(($_.Size -lt 150) -and ($_.Mailbox -lt 120)){
            return (120 - $_.Mailbox)
        }
        else{
            return 0
        }
    }
},
@{n='MoveOut';e={
        if(($_.Size -gt 150) -or ($_.Mailbox -gt 120)){
            return ($_.Mailbox - 120)
        }
        else{
            return 0
        }
    }
}
 
$Output|Select Name,Size,Mailbox,MoveIn,MoveOut | Sort-Object MoveIn



--------------------------------- End --------------------------------- 

No comments:

Post a Comment