none
Unable to get lastLogonTimestamp value from New-Object PSObject RRS feed

  • Question

  • I have the following code in a ForEach loop to log output to a CSV file but am having trouble getting it to populate the lastLogonTimestamp values... the output just comes out blank.  All the other columns are populating fine.  The value definitely exists in AD for these disabled users. I have a hunch that it has something to do with the actual value (e.g. - 130100816639918715) vs. the displayed value (e.g. - 4/10/2013 11:34:23 AM Eastern Standard Time). The syntax is categorized as "Large Integer/Interval."  Any ideas?

    #Create new object for logging.
    $obj = New-Object PSObject
    $obj | Add-Member -MemberType NoteProperty -Name "Name" -Value $DisabledUser.name
    $obj | Add-Member -MemberType NoteProperty -Name "samAccountName" -Value $DisabledUser.samaccountname
    $obj | Add-Member -MemberType NoteProperty -Name "DistinguishedName" -Value $DisabledUser.DistinguishedName
    $obj | Add-Member -MemberType NoteProperty -Name "lastLogonTimestamp" -Value $DisabledUser.lastLogonTimestamp
    $obj | Add-Member -MemberType NoteProperty -Name "Status" -Value 'Disabled'
    
    #Adds object to the log array.
    $LogArray += $obj
    
    #Exports log array to CSV file in the temp directory with a date and time stamp in the file name.
    $logArray | Export-Csv "C:\Temp\User_Report_$logDate.csv" -NoTypeInformation

    Friday, January 24, 2014 4:29 PM

Answers

  • Can you provide all of the code that you are using to perform this query? As Mike said, if you are using Get-ADUser then you need to use the -Property parameter and supply the appropriate property name that you wish to get back.

    As far as getting around the default format of the timestamp that is returned, you can do this:

    [datetime]::FromFileTime($DisabledUser.lastLogonTimestamp
    )


    Boe Prox
    Blog | Twitter
    PoshWSUS | PoshPAIG | PoshChat | PoshEventUI
    PowerShell Deep Dives Book

    • Marked as answer by stephengtang Friday, January 24, 2014 8:27 PM
    Friday, January 24, 2014 6:26 PM
    Moderator
  • Do you mean LastLogon?  I don't believe the attribute LastLogonDate exists.  If so, I didn't want to use LastLogon since that doesn't get replicated.

    No, LastLogonDate is a human friendly PowerShell representation of LastLogonTimeStamp.

    If you want that property returned, you'll need to add it to your Get-ADUser statement by adding -Properties LastLogonTimeStamp.


    Don't retire TechNet! - (Don't give up yet - 12,575+ strong and growing)

    • Marked as answer by stephengtang Friday, January 24, 2014 6:53 PM
    Friday, January 24, 2014 6:40 PM

All replies

  • Hi,

    Are you retrieving the property when you populate your $DisabledUser object? LastLogonTimeStamp isn't a default property (assuming you're using Get-ADUser). Also, consider using LastLogonDate instead of LastLogonTimeStamp, it's a much friendlier representation of the same data.


    Don't retire TechNet! - (Don't give up yet - 12,575+ strong and growing)

    Friday, January 24, 2014 4:47 PM
  • Hi Mike,

    This is what the code looks like:

    ForEach ($DisabledUser in (Get-ADUser -searchbase $SearchBase -filter {((lastlogondate -notlike "*") -OR (lastlogondate -le $Passwordage)) -AND (passwordlastset -le $Passwordage) -AND (whencreated -le $Passwordage) -AND (enabled -eq $True)} )) {
    
        #Create new object for logging.
        $obj = New-Object PSObject
        $obj | Add-Member -MemberType NoteProperty -Name "Name" -Value $DisabledUser.name
        $obj | Add-Member -MemberType NoteProperty -Name "samAccountName" -Value $DisabledUser.samaccountname
        $obj | Add-Member -MemberType NoteProperty -Name "DistinguishedName" -Value $DisabledUser.DistinguishedName
        $obj | Add-Member -MemberType NoteProperty -Name "lastLogonTimestamp" -Value $DisabledUser.lastLogonTimestamp
        $obj | Add-Member -MemberType NoteProperty -Name "Status" -Value 'Disabled'
    
        #Adds object to the log array.
        $LogArray += $obj
    }

    Do you mean LastLogon?  I don't believe the attribute LastLogonDate exists.  If so, I didn't want to use LastLogon since that doesn't get replicated.

    What would I need to change to be able to create a column for lastLogonTimestamp?

    Thanks!

    Friday, January 24, 2014 6:25 PM
  • Can you provide all of the code that you are using to perform this query? As Mike said, if you are using Get-ADUser then you need to use the -Property parameter and supply the appropriate property name that you wish to get back.

    As far as getting around the default format of the timestamp that is returned, you can do this:

    [datetime]::FromFileTime($DisabledUser.lastLogonTimestamp
    )


    Boe Prox
    Blog | Twitter
    PoshWSUS | PoshPAIG | PoshChat | PoshEventUI
    PowerShell Deep Dives Book

    • Marked as answer by stephengtang Friday, January 24, 2014 8:27 PM
    Friday, January 24, 2014 6:26 PM
    Moderator
  • Do you mean LastLogon?  I don't believe the attribute LastLogonDate exists.  If so, I didn't want to use LastLogon since that doesn't get replicated.

    No, LastLogonDate is a human friendly PowerShell representation of LastLogonTimeStamp.

    If you want that property returned, you'll need to add it to your Get-ADUser statement by adding -Properties LastLogonTimeStamp.


    Don't retire TechNet! - (Don't give up yet - 12,575+ strong and growing)

    • Marked as answer by stephengtang Friday, January 24, 2014 6:53 PM
    Friday, January 24, 2014 6:40 PM
  • Awesome!  Thanks guys - Mike for the solution and Boe for the time stamp formatting.
    Friday, January 24, 2014 6:54 PM
  • Awesome!  Thanks guys - Mike for the solution and Boe for the time stamp formatting.

    Cheers, you're very welcome.

    Just FYI, you're not limited to a single answer per thread, so you can mark Boe's post as an answer as well.


    Don't retire TechNet! - (Don't give up yet - 12,575+ strong and growing)

    Friday, January 24, 2014 7:24 PM