Powershell and Microsoft Exchange

I thought I would post some work I did a few years ago showing the powerfull PowerShell scripting language.

getMDB attempts to scatter requests for mailbox assignment by using a random number to choose servers over a region where there are just two regions in this example (East/North) and (West/South)

It uses a COM hook into the Exchange Server to read server attributes and storage groups.

 #  Name.......: getMdb()
 #  Description: Used to select a random mailstore based on region and<!--DVFMTSC--> </code><code>archive attributes
 #  Inputs.....: $region (North, West, South, East) Used to select destination mailbox server
 #               $archive (True, False) Used to select proper mailbox store based on journaling flag

function getMdb(){
param($region, $archive)

#Local Variables
$mailstore = @()
$archivestore = @()

$Random = New-Object Random

#Exchange server selection process based on region

if($region -like "EAST" -or $region -like "NORTH"){
$mservers = "server list A..."
elseif($region -like "SOUTH" -or $region -like "WEST"){
$mservers = "server list B..."

foreach ($server in $mservers){

$excObj = New-Object -ComObject CDOEXM.ExchangeServer
$sgObj = New-Object -ComObject CDOEXM.StorageGroup
$sgs = $excObj.StorageGroups

foreach ($objItem in $sgs) {
$dbs = $sgObj.MailboxStoreDbs

foreach ($mstore in $dbs){
if($mstore -like "*Zantaz*"){
$archivestore += $mstore
else {
$mailstore += $mstore
} # EndFor
} # EndForEach
} # EndForEach
"LOG: getMdb - Return mailstores $mailstore" >> $logfile

if($archive -eq $True){
$index = get-RandomElement($archivestore)
$global:mdb = [string] $zantazstore[$index]
$index = get-RandomElement($mailstore)
$global:mdb = [string] $mailstore[$index]
write-host "Return MDB $($mdb)"

"LOG: getMdb - Return MDB $mdb" >> $logfile
#// -end getMdb