Tuesday, July 22, 2008

Working with Active Directory Dates

Recently, my friend and colleague asked me about how to limit an ldap search to only return objects that were created after a certain date. 'whenCreated' should be a logical choice to solve that. However, this attribute has an interesting format, see this reference. So, i marked up a few functions to help us work with the date naturally within Powershell.
function ConvertToAdDate([DateTime]$date) {
"{0:0000}{1:00}{2:00}{3:00}{4:00}{5:00}.0Z" -f $date.year,$date.month, `
$date.day, $date.Hour, $date.Minute, $date.Second
}
function ConvertFromAdDate([String] $date) {
$pattern = `
'^(?<year>\d{4})(?<month>\d{2})(?<day>\d{2})(?<hrs>\d{2})`
(?<min>\d{2})(?<sec>\d{2})\.0Z$
'
$match = [regex]::Match($date,$pattern)
if ($match.success) {
$result = New-Object System.DateTime($match.groups["year"].value,`
$match.groups["month"].value,$match.groups["day"].value,`
$match.groups["hrs"].value,$match.groups["min"].value, `
$match.groups["sec"].value,[DateTimeKind]::Utc)
$result.ToLocalTime()
}
else {
$null
}
}

Friday, July 11, 2008

Purging Mailboxes In Exchange 2003

I have recently demonstrated how to Purge a mailbox in Exchange 2007 but did not show anything for Exchange 2003. Here is what I do to purge mailboxes on Exchange 2003 server.

$list = gwmi -ComputerName "ex2k3srv" -Class Exchange_Mailbox -Namespace Root\MicrosoftExchangeV2 | ?{ $_.DateDiscoveredAbsentInDS -ne $null }
$list | %{ $_.Purge() }

For IT admins and Sales guys!!!

It's been all over the web. Still, I need to post this link b/c I'll be coming back to it for entertainment for awhile.

http://www.thewebsiteisdown.com/salesguy.html

Thursday, July 3, 2008

Purging Mailboxes in Exchange 2007

We all know how vastly different Exchange 2007 Management Console (EMC) is from Exchange System Manager (ESM) in Exchange 2003. The other day I had to test a migration for work and realized that I no longer have an easy ability to purge mailboxes through EMC just like I used to in ESM:

Under the hood, selecting Purge in ESM calls Exchange_Mailbox.Purge method.
In Exchange 2007, WMI is gone. So Powershell comes to the rescue.
After reviewing "Remove-Mailbox" reference at TechNet
, it sounds like "-Permanent" should be piped when removing the user and that's it. However, things are not that simple.
If mailbox is already disassociated, then I can query a list of disconnected mailboxes like this:
Get-MailboxStatistics | ?{ $_.DisconnectDate -ne $null}
Now, I need to pipe the "Remove-Mailbox", but which parameters do I choose?
It took me some mocking around but it turns out this is what you need:
Get-MailboxStatistics | ?{ $_.DisconnectDate -ne $null} | `
%
{ Remove-Mailbox -Database "Mailbox Database" `
-StoreMailboxIdentity
$_.MailboxGuid -WhatIf:$false}