locked
Last Logon Null Attribute RRS feed

  • Question

  • I have formed the below as part of a larger script.  The AccountExpirationDate pulls out to a date or blank if null.  The lastlogon portion was received from research and RLMueller shared this on another post.  What I can't figure out how to do is turn just the lastlogon into "Never" or blank if null.  Instead right now it returns the UTC date of 12/31/1600 7:00:00 PM.

    Get-AdUser -server $DomainSet -Filter * -Properties * | Select Name,SamAccountName,EmailAddress,AccountExpirationDate,@{Name='lastlogon';Expression={[DateTime]::FromFileTime($_.lastlogon)}},DistinguishedName | Export-Csv -Path X:\XXXXXXXX.csv -NoTypeInformation
    Any help you can provide would be greatly appreciated, ladies and gentlemen.  I have received a ton of answers from these forums, absolutely spectacular.  I am not using the Quest cmdlets so finding answers anywhere else are often difficult.
    Friday, October 12, 2012 8:38 PM

Answers

  • This takes into account 0 or null, and implements the conversion in the function.  I've tested the function with zero and null, and it returns "Never":

    function LastLogonConvert ($ftDate) {
        $date = [DateTime]::FromFileTime($ftDate)
        if ($date -lt (Get-Date '1/1/1900') -or $date -eq 0 -or $date -eq $null) {"Never"} else {$date}
        } # End function LastLogonConvert
    
    Get-AdUser  -Filter * -Properties * |
    Select Name,SamAccountName,EmailAddress,AccountExpirationDate,@{Name='lastlogon'; Expression={LastLogonConvert $_.lastlogon}},DistinguishedName | 
    Export-Csv -Path X:\XXXXXXXX.csv -NoTypeInformation


    Grant Ward, a.k.a. Bigteddy

    • Proposed as answer by Richard MuellerMVP Saturday, October 13, 2012 11:52 AM
    • Marked as answer by IamMred Sunday, October 14, 2012 12:16 AM
    Saturday, October 13, 2012 7:27 AM

All replies

  • I've used code similar to below:

    # Retrieve Name and lastLogonTimeStamp for all users.
    $Users = Get-ADUser -Filter * `
        -Properties lastLogonTimeStamp, sAMAccountName
    ForEach ($User In $Users) {
        $Name = $User.sAMAccountName
        $LL = $User.lastLogonTimeStamp
        If (($LL -eq $Null) -or ($LL -eq 0))
        {
            "$Name,<Never>"
        }
        Else
        {
            $T = [Int64]::Parse($LL)
            $D = [DateTime]::FromFileTime($T)
            "$Name,$D"
        }
    }

    -----

    Similar applies for lastLogon. AccountExpirationDate is actually a method (which converts the accountExpires attribute), while lastLogon and lastLogonTimeStamp are attributes. The method handles nulls or zeros, but you need to do this for lastLogon. Also, AccountExpirationDate converts accountExpires from UTC to the local time zone. The above code does this for lastLogon (the difference is a few hours, depending on your time zone).


    Richard Mueller - MVP Directory Services

    Friday, October 12, 2012 9:01 PM
  • This works for me:

    function LastLogonConvert ($date) {if ($date -eq '12/31/1600 7:00:00 PM') {"Never"} else {$date}}
    
    Get-AdUser  -Filter * -Properties * |
    Select Name,SamAccountName,EmailAddress,AccountExpirationDate,@{Name='lastlogon';Expression={LastLogonConvert ([DateTime]::FromFileTime($_.lastlogon))}},DistinguishedName | 
    Export-Csv -Path X:\XXXXXXXX.csv -NoTypeInformation


    Grant Ward, a.k.a. Bigteddy


    • Edited by Bigteddy Friday, October 12, 2012 9:16 PM
    Friday, October 12, 2012 9:03 PM
  • Grant, won't that (the 7:00:00 pm) depend on your time zone? Is there a way to modify the function to check lastLogon for Null or "0" and convert FromFileTime?


    Richard Mueller - MVP Directory Services

    Friday, October 12, 2012 9:14 PM
  • Grant, won't that (the 7:00:00 pm) depend on your time zone? Is there a way to modify the function to check lastLogon for Null or "0" and convert FromFileTime?


    Richard Mueller - MVP Directory Services


    Yes, it does depend on time zone.  I had to adjust it to test it on my test domain.  It makes sense to use the function to also check for Null or 0, as well as doing the date/time conversion.

    Grant Ward, a.k.a. Bigteddy

    Saturday, October 13, 2012 7:12 AM
  • This takes into account 0 or null, and implements the conversion in the function.  I've tested the function with zero and null, and it returns "Never":

    function LastLogonConvert ($ftDate) {
        $date = [DateTime]::FromFileTime($ftDate)
        if ($date -lt (Get-Date '1/1/1900') -or $date -eq 0 -or $date -eq $null) {"Never"} else {$date}
        } # End function LastLogonConvert
    
    Get-AdUser  -Filter * -Properties * |
    Select Name,SamAccountName,EmailAddress,AccountExpirationDate,@{Name='lastlogon'; Expression={LastLogonConvert $_.lastlogon}},DistinguishedName | 
    Export-Csv -Path X:\XXXXXXXX.csv -NoTypeInformation


    Grant Ward, a.k.a. Bigteddy

    • Proposed as answer by Richard MuellerMVP Saturday, October 13, 2012 11:52 AM
    • Marked as answer by IamMred Sunday, October 14, 2012 12:16 AM
    Saturday, October 13, 2012 7:27 AM
  • Worked perfectly, thanks all.
    Monday, October 15, 2012 10:52 PM