I remember writing plenty of scripts to report on different things such as user accounts created every week/month, user accounts modified, accounts disabled, etc. for
SOX compliance. Some of those scripts used to be rather long, and in hindsight— involved a lot more lines of code than an administrator should have to write. Although I had a lot of fun
(and still do... albeit with PowerShell), I would totally understand if you said you never wanted to hear about things like Wscript, VBScript,
WSH,
COM objects,
ADSI, and
WMI ever again.
Let's take a look at how the shell (
EMS) makes it so easy.
In this examnple, we need to get a list of all accounts created in the last 7 days. When a user account is created, its
whenCreated attribute gets stamped with the time of creation. Here's how it can be used:
Get-User -resultsize unlimited | where {$_.WhenCreated -gt (get-date).adddays(-7) | ft Name,whenCreated -Autosize
Similarly, when an AD object is changed, it's
whenChanged attribute gets stamped with the time the change was made. This makes it easy to determine which objects were changed in a given period, a useful tool for auditing/reporting as well as troubleshooting. In the following example, we determine if any Receive Connectors were changed in the last 7 days.
Get-ReceiveConnector | where {$_.whenChanged -gt (get-date).adddays(-7)}
Another frequently required and requested report— how do I get a list of mailboxes that haven't been accessed in the last X days. Let's use 100 days as the value here:
Get-MailboxStatistics -resultsize unlimited | where {$_.LastLogonTime -lt (get-date).AddDays(-100)} | ft displayName,lastlogontime,lastloggedonuseraccount,servername
Or mailboxes that have never been logged on to:
Get-MailboxStatistics -resultsize unlimited | where {$_.LastLogonTime -eq $null | ft displayName,lastlogontime,lastloggedonuseraccount,servername
Note, you can filter mailboxes by
Database or
ServerName to restrict the results to a more manageable size.
Next, let's list mailboxes disabled in the last 14 days:
Get-MailboxStatistics | Where {$_.DisconnectDate -gt (get-date).AddDays(-14)} | ft displayName,ServerName,DatabaseName,TotalItemSize -Autosize
Labels: Administration, Compliance, Exchange Server 2007, Exchange Shell