We have a script to create all new users based on what is being created in our HR system. This script creates a mailbox for the user in the database "NewMailboxes". Then everyday a script is being called on the exchangeserver to move the newly created mailboxes to the database least used. This has worked very well until now, in our exchange 2010 environment, which is a DAG cluster. The reason for this is that all users have mailboxes on the same exchange cluster. These users are placed in Norway and UK. We have now set up a new server placed in Singapore. It is in the same exchange organization, but not members of the cluster.
The script we are running today looks like this:
# ************************************************************************
#
#
# 1. Enumerates Mailboxes in database $NewMDB and moves
# each mailbox to the database with the least amount of mailboxes
# 2. Cleans up old move requests
#
# Schedule with command:
# powershell.exe -command ". 'C:\Program Files\Microsoft\Exchange Server\
# V14\bin\RemoteExchange.ps1'; Connect-ExchangeServer -auto; C:\Progra~1\
# Microsoft\Exchan~1\V14\Scripts\NewMailboxHandler.ps1"
#
# ************************************************************************
$NewMDB = "NewMailboxes"
function Get-MDBMailboxCount ([string]$DN) {
$Searcher = New-Object System.DirectoryServices.DirectorySearcher
$Searcher.SearchRoot = New-Object System.DirectoryServices.DirectoryEntry ("LDAP://$(([system.directoryservices.activedirectory.domain]::GetCurrentDomain()).Name)")
$Searcher.Filter = "(&(objectClass=user)(homeMDB=$DN))"
$Searcher.PageSize = 10000
$Searcher.SearchScope = "Subtree"
($Searcher.FindAll())
}
$DBUsers = @{}
$NewMailboxes = Get-Mailbox -ResultSize:Unlimited -Database $NewMDB
If ($NewMailboxes) {
$Databases = Get-MailboxDatabase | where { $_.Name -ne $NewMDB -and $_.Recovery -ne $true}
foreach ($MDB in $Databases) { $DBUsers[$MDB.Name] = @(Get-MDBMailboxCount -DN $MDB.DistinguishedName).Count}
Get-MoveRequest | where {$_.Status -ne 'completed' -and $_.Status -ne 'failed'} | foreach {$DBUsers[$_.SourceDatabase.Name] --; $DBUsers[$_.TargetDatabase.Name] ++ }
$NewMailboxes | foreach {
$DBUSers.GetEnumerator() | Sort-Object Value,Name -Descending | where { $_.Name -ne $NewMDB -and $_.Recovery -ne $true} | foreach { $TargetDB = $_.Key }
New-MoveRequest $_ -TargetDatabase $TargetDB -Confirm:$False
$DBUsers[$TargetDB] ++
}
}
# Clean up older move requests
Start-Sleep -Seconds 60
Get-MoveRequest | where { $_.Status -like 'Completed' } | Remove-MoveRequest -Confirm:$False
##
##
What I would need to do here, is to first move all mailboxes in database "Newmailboxes" where $._physicalOfficeDeliveryName equals Singapore, and then let the script continue with the rest of the mailboxes.
I have figured out how to get all users with office equals Singapore listed:
Get-ADuser -filter * -properties physicaldeliveryofficename | Where-Object { ($_.physicaldeliveryofficename -eq "singapore") } | select samaccountname | ft
But I cannot figure out how I can connect this with only users who's mailbox is in a specific mailboxdatabase, and how to move these to the specific Singapore mailboxdatabase.
Help on the subject would be highly appreciated.
PS! The script itself is created by a consultant, and not myself... I am (unfortunately) not that skilled in powershell scripting.