Tuesday 28 June 2016

A unique problem

At my CPLS, Auldhouse Computer Training, we have a huge blend of applications courses, and technical courses. With everything from Microsoft, Cisco, Citrix, VMWare, CompTIA, Pink Elephant, Linux and Unix, through to Office, Adobe Creative suite, the list goes on and on and on...

One problem that is faced by our Apps trainers, is teaching Outlook today, then tomorrow having to clean up the Outlook inboxes and rules etc. before the next course. This is a bit of a pain in the butt, and of course, it's not a useful use of time.  Honestly, some of the things that people from the work place send to each other, just because they are on a training course.... SHEESH!
 
Today, I whipped up a script that will take care of that, and I was really quite surprised at how simple it was.

The problem. Destroy and recreate all the mailboxes without destroying the AD User accounts, as they're used for many other NTFS permissions all over the show. If it wasn't for that, I'd just as happily delete the AD accounts, and import-csv to fix it. Easy. But alas, I need those SIDs to remain.

As a solution, I came up with the below.  Now, as with all problems, there are more than likely a million other ways, but this is fit for purpose and does the trick.

Essentially there are four steps:
  1. Select all the users matching a criteria and disable their mailboxes
  2. Clean the database to make sure the disabled boxes are now in the Disconnected Mailboxes container in Exchange
  3. Wipe out all the disconnected mailboxes
  4. Re-create the mailboxes, essentially doing the opposite of step 1
It looks a little like this:

get-user | where-object {$_.name -like "wlg*" } | disable-mailbox

clean-mailboxdatabase "Wellington Mailbox Database"

$(Get-MailboxStatistics -Database "Wellington Mailbox Database")|where{$_.disconnectreason -eq "disabled"} | foreach {remove-storemailbox -database $_.database -Identity $_.mailboxguid -mailboxstate disabled -Confirm:$false}

get-user | where-object {$_.name -like "wlg*"} | Enable-Mailbox

I am pretty tickled with this, as I am not an Exchange hound. I should add that this is all done on an older Exchange 2010 box. Why Exchange 2010? Just the way it is.....one day I'll get around to updating that. Then I could setup a nice wee template to stop the naughty words anyway!

Cheers,

Malc.