Powershell Script #2 - Getting last logon date for Exchange mailboxes

I have been attempting to do some cleanup of the Active Directory environment. My latest endeavor is to capture the last logon time from AD and correlate it to active accounts. If they're not logging on, maybe they don't know how, or don't need a mailbox??

My script here, will query all the Exchange servers for user display name and last logon time (LastLogonTime). Since the last logon is a 64-bit integer, you need to do some special handling of it. I found a number of tricks that used bit-level handling to convert the value to a date-time format, but after some pointing and nudging, I realized the simplicity of simply pulling it apart as a string value.

Last bit, I wanted a single CSV file for all my Exchange servers. There is no way I wanted to have 8 CSV files to sort through for all the mailbox information. Even more so, I didn't want to have to merge them all back together, this is a computer I am spending my life in front of.

Finally, I have to thank Microsoft for a great resource for converting my VBScript experience into Powershell. This site is an easy to understand reference for experienced VB scriptter who want to quickly learn PS.

Update (May 2008): In the BETA version of the Quest Active Roles Management shell includes a lastlogon field. This means you can quickly export the data without special formatting (like below).


Get-QADUser -SearchRoot 'your.domain.local/' -IncludeAllProperties | Format-List name, lastlogon*, accountis*

For each account on your domain, this script returns.
Name: guest
LastLogonTimeStamp: May 1, 2008
LastLogon: May 7, 2008
AccountIsDisabled: False
AccountIsLocked: False
...

Re-route this to a CSV file, and you have something to share with the whole family!

Thanks Quest, Nice addition to this tool!

[end update]


# Name: get-lastlogon.ps1
# Author: Eric Woodford - www.ericwoodford.com
# Date: 09/26/2007
# Description: Mailbox last logon information to single CSV file.
# Display Name, Last Logon Time
#

#create header for CSV file.
$CSVFilePath = 'c:\mail_LastLogon.csv'
$strDate = get-date -uformat "%Y/%m/%d"
$strDate | out-file -filepath $CSVFilePath -encoding ascii
$strOutputString = "Display Name,LastLogonTime"
$strOutputString | out-file -filepath $CSVFilePath -encoding ascii -append

#set this to match your environment.
$Computers = get-qadComputer -searchRoot 'corp.ent/Member Servers/Exchange Servers'
foreach ($computer in $computers) {
$users = Get-Wmiobject -namespace root\MicrosoftExchangeV2 -class Exchange_Mailbox -computer $computer.name | Select-Object MailBoxDisplayName, LastLogonTime
# Get-Wmiobject -namespace root\MicrosoftExchangeV2 -class Exchange_Mailbox -computer $computer.name | Select-Object MailBoxDisplayName, LastLogonTime
foreach ($user in $users) {
$date= [string] $user.LastLogonTime
if ($date.length -eq 0) {
$strOutputString = """"+ $user.MailBoxDisplayName + """, N/A"
}
else {
$strOutputString = """"+ $user.MailBoxDisplayName + """," + $date.substring(4,2)+"/"+$date.substring(6,2) +"/"+ $date.substring(0,4)
}
$strOutputString | out-file -filepath $CSVFilePath -encoding ascii -append
}
}