Tuesday, December 15, 2009

LastLogonTimeStamp

Hey VBScripters,

Do you like working with LastLogonTimeStamp (remember this):


Set oUser = GetObject("LDAP://" & sUserDN)
Set oLastLTS = oUser.Get("lastlogontimestamp")
iLastLogon = oLastLTS.HighPart * (2^32) + oLastLTS.LowPart
iLastLogon = iLastLogon / (60 * 10000000)
iLastLogon = iLastLogon / 1440
dLastLogon = iLastLogon + #1/1/1601#



Here is how this works in Powershell v2 RTM using the ActiveDirectory module:

$ADUser = Get-ADUser johndoe -Properties lastlogontimestamp
if ($ADUser.lastlogontimestamp -ne $null){
        $lastlogon = Get-Date -Date ([DateTime]::FromFileTime([Int64]::Parse($ADUser.lastlogontimestamp))) -Format MM/dd/yyyy
    } else {
        $lastlogon = Get-Date -Date $ADUser.created -Format MM/dd/yyyy
    }

I think that is just so elegant.  If I hadn't added the checks and the formatting (like I didn't do in the VB script) this would have only taken two lines.  Cool huh?

Have fun,
Cameron out.

2 comments: