Sort Mailboxes by Size

As part of the larger mailbox moves we're doing, I developed this short function to sort an array of mailbox objects by their size. I was hoping that by feeding Move-Mailbox cmdlet a sorted list, it would move mailboxes faster. My theory being that if I started with the Largest mailboxes first, it would only take the longest mailbox time for all the moves. By sorting the list, it appears to have cut down on the doubling up of moves..

For example if my group contains:

  • Tom = 5gb
  • Lisa = 3gb
  • Fred = 2gb
  • Bob = 10gb
  • Paul = 5mb

(For example purposes) Say, I limit my moves 2 mailboxes at a time. By using an unsorted list, it could potentially move Tom(5gb) then Bob(10gb) on the same thread. (Thread 1= Tom, Bob; Thread 2 = Lisa, Fred, Paul) This could potentially create an extra-long move.

By sorting the mailboxes, I would Move Bob on one thread and Tom on the second thread. When Tom completes, it would pickup the remaining mailboxes. Possibly finishing the moves around the same time as (or even before) Bob's move.

Function Sort-MoveGroup ($SortGroup) {
        write-host "sorting mailboxes by size"
        $SizeObj = @()
        Foreach ($Mbx in $SortGroup) {
                if ($Mbx.distinguishedname -ne "" -and $mbx.distinguishedname -ne $null) {
                        $Logical = New-Object System.Object
                        $logical | Add-Member -MemberType NoteProperty -Name Mailbox -value $mbx.distinguishedname
                        $size = (Get-MailboxStatistics $mbx.distinguishedname).TotalItemSize.value.tobytes()
                        $Logical | Add-Member -MemberType NoteProperty -Name MbxSize -value ([double]$size  )
                        $SizeObj += $Logical
                }
        }
        $sortedGroup = Sort -inputobject $SizeObj -property MBXSize #-descending
        $sortedMbxes= $SortedGroup | ?{$_.mailbox -ne ""} | %{get-mailbox $_.mailbox -resultsize 1}
        return $SortedMbxes
}

Use:

   $MoveGroup = Get-distributionGroupMember "HR Department"
   $SortedDept = Sort-MoveGroup $MoveGroup
#   If ($sortedDept.count -eq $DeptMbx.Count) {$MoveGroup = $sortedDept}
   $MoveGroup | Move-Mailbox ....

Note about code. I still tend to double-check my mailbox counts before and after the sort. On one occasion, I had a null entry in my array (a distribution list was added) and it pulled an extra mailboxes. That is why I set the get-mailbox to only return 1 entry. At least it isn't 1000 extra.

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <blockquote> <center> <hr> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img>
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <drupal6>, <html>, <java>, <javascript>, <php>, <posh>.

More information about formatting options

Type the characters you see in this picture. (verify using audio)
Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.