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
}
}

Comments

I think this is an

I think this is an alternative way to get the Last Log On time and other detail in Exchange 2007 with out installing any third party tool or software. Where this particular peice of information that was present under the previous Exchange 2003 version. In Exchange 2007 you will not find this in in Mailbox node of the Recepient Configuration. We can use either Power Shell or the Exchange Management Shell that comes with our Exchange 2007 Server.

Open Exchange Management Shell (I prefer this) and try to run these commands.

To dispaly on Screen Only.

Get-MailboxStatistics -Server  servername

To dump to a text file you can dump it into atext file or excel by using the proper extensions.
Get-MailboxStatistics -Server  servername | out-file textfilename.txt

By running these command. you will get not only the Last Log on time in every user mailbox in your exchange 2007 server but username, total mailbox items count ...etc

I have been looking for an article over the net that would get this done but unluckily havent found one.
Hope this works for you too as it works for me .

Hope it help...

George

Nice, but from my testing it

Nice, but from my testing it does not work with Exchange 2003. To expand on your script, this will run through all your Exchange Mailbox servers if you have more than one.

get-mailboxserver | %{Get-MailboxStatistics -Server $_.NAME} | sort-object LastLogonTime

when i enter the Exchange

when i enter the Exchange server name or use AD i get this error msg

Get-WmiObject : Cannot validate argument because it is null.
At C:\get-lastlogon.ps1:18 char:93
+ $users = Get-Wmiobject -namespace root\MicrosoftExchangeV2 -class Exchange_Mailbox -computer <<<< $computers.name |
Select-Object MailBoxDisplayName, LastLogonTime

any sugestions
thanks
irving

Hi Irving, My guess is that

Hi Irving,

My guess is that the script is attempting to query your AD environment and is returning no valid responses.

Make sure you don't have a space after the "-" in the following line:


-computer $computers.name

$computers is a variable generated earlier in the script when it queries your AD environment.

I have not tested this script with the final release of PowerShell, only the RC version.

For #set this to match your

For #set this to match your environment.
$Computers = get-qadComputer -searchRoot 'corp.ent/Member Servers/Exchange Servers'
foreach ($computer in $computers) {

I get an error each time I run it where the get-qadcomputer is not valid

Sorry, I forgot to mention

Sorry, I forgot to mention that you need to have the Quest AD tools to get that data.
http://www.quest.com/activeroles-server/arms.aspx

You could look at simply providing a comma separated list of names.
$computers = "server001","server002"

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <blockquote> <center> <hr> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • Web page addresses and e-mail addresses turn into links automatically.

More information about formatting options