Skip to content

Instantly share code, notes, and snippets.

@robderickson
Created July 11, 2020 04:39
Show Gist options
  • Save robderickson/c67e52b7cca15491d15c23b6b69502f3 to your computer and use it in GitHub Desktop.
Save robderickson/c67e52b7cca15491d15c23b6b69502f3 to your computer and use it in GitHub Desktop.
TL;DR: Over-complicate Exchange database selection during mailbox provisioning. For each mailbox in a collection of Exchanges mailboxes, return PrimarySmtpAddress and the smallest database (factoring in AvailableNewMailboxSpace). As databases are selected, add the mailbox's size to the database size so the next database selected factors in the s…
function GetMoveParameters {
[CmdletBinding()]
param(
[string[]]$Mailbox,
[string[]]$Databases
)
PROCESS {
if ($Databases) {
$dbs = $Databases | Where-Object {$_.Mounted -eq $true}
} else {
$dbs = Get-MailboxDatabase -Status | Where-Object {$_.Mounted -eq $true}
}
$DbSizes = foreach ($db in $dbs) {
[PSCustomObject]@{
Name = $db.Name
Size = $db.DatabaseSize.ToBytes() - $db.AvailableNewMailboxSpace.ToBytes()
}
}
foreach ($mb in $Mailbox) {
$MbStats = Get-Mailbox $mb | Get-MailboxStatistics
$MbSize = $MbStats.TotalItemSize.Value.ToBytes() + $MbStats.TotalDeletedItemSize.Value.ToBytes()
$SmallestDatabase = $DbSizes | Sort-Object Size | Select-Object -First 1
($DbSizes | Where-Object {$_.name -eq $SmallestDatabase.name}).Size = $SmallestDatabase.Size + $MbSize
[PSCustomObject]@{
PrimarySmtpAddress = Get-Mailbox $mb | Select-Object -ExpandProperty PrimarySmtpAddress
TargetDatabase = $SmallestDatabase.Name
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment