Unable to display the LastLogonTimeStamp when exporting to CSV?

Hello All,

I need some help in modifying and fixing my script to export the member of an AD security group called Domain Admins to .CSV, the below script:

$ADGroupType = 'security'
$ADGroupNamePattern = 'Domain Admins'
$ResultFile = "C:\Admin.csv"

function ConvertFrom-DN {
    [cmdletbinding()]
    param(
    [Parameter(Mandatory,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)] 
    [ValidateNotNullOrEmpty()]
    [string[]]$DistinguishedName
    )
    process {
        foreach ($DN in $DistinguishedName) {
        Write-Verbose $DN
            foreach ( $item in ($DN.replace('\,','~').split(","))) {
                switch ($item.TrimStart().Substring(0,2)) {
                    'CN' {$CN = '/' + $item.Replace("CN=","")}
                    'OU' {$OU += ,$item.Replace("OU=","");$OU += '/'}
                    'DC' {$DC += $item.Replace("DC=","");$DC += '.'}
                }
            } 
            $CanonicalName = $DC.Substring(0,$DC.length - 1)
            for ($i = $OU.count;$i -ge 0;$i -- ){$CanonicalName += $OU[$i]}
            if ( $DN.Substring(0,2) -eq 'CN' ) {
                $CanonicalName += $CN.Replace('~','\,')
            }
            $qwer = [PSCustomObject]@{
			    'CanonicalName' = $CanonicalName;
		    }
            Write-Output $qwer

        }
    }
}

Function Get-ADGroupMemberRecursive {
[CmdletBinding()]
Param(
	[Parameter(ValueFromPipeline=$true)]
	$Identity,
	[string[]]$Property
)
	Begin {
		$splat = @{}
		If ($Property) {$splat['Property'] = $Property}
	}
	Process {
		Get-ADGroupMember -Identity $Identity | ForEach-Object {
			If ($_.objectClass -eq 'User') {
				Get-ADUser -Identity $_ @splat | Select-Object -Property @{n='Group'; e={$Identity.Name}}, whenCreated, lastLogon, lastLogonTimeStamp
			} ElseIf ($_.objectClass -eq 'Group') {
				Get-ADGroupMemberRecursive -Identity $_ @splat
			}
		}
	}
}

Get-ADGroup -Filter "(groupCategory -eq '$ADGroupType') -AND (name -like '$($ADGroupNamePattern)')" |
	Get-ADGroupMemberRecursive -Property Mail |
	Select-Object Group, 
        Name, 
        SamAccountName, 
        Mail, 
        whenCreated, 
        @{n='Last Logon'; e={[datetime]::FromFileTime($_.lastLogon)}},
        @{n='Last Logon TimeStamp'; e={[datetime]::FromFileTime($_.lastLogonTimeStamp)}}, 
        @{n='OU Location'; e={ConvertFrom-DN ($_.DistinguishedName)}}  |
	Export-Csv -Path $ResultFile -NoTypeInformation

ii $ResultFile

The issue is:

  1. The date is always pointing to the year 1601.
  2. The only column that is filled in correctly is just the Group Name and the LastLogon TimeStamp?
Ā 

Thanks in advance.

Were you able to figure out the issue? Iā€™m looking to get this information as well.

Aalborz,
Welcome to the forum. :wave:t4:

First of all: Please do not hijack other peoples threads. If you have a similar question you should create a new post yourself and link to this one if it is helpful.

Second: Do you really expect an answer 3 years later when the original question havenā€™t been answered at all?

Third: Did you try to search for it yourself? There are countless same and similar questions out there who have been ansered many times already.

https://social.technet.microsoft.com/Forums/azure/en-US/1ae08081-dcfe-44cd-bc3b-f5ac26d53f76/difference-between-lastlogon-and-lastlogontimestamp?forum=winserverDS

First off: I wasnā€™t trying to ā€œhijackā€ other peopleā€™s threads. I did search and based on what I had entered this is the closest one came up. I did try to create a post, but it wouldnā€™t let me, something along the line that questions have been exceeded or something. Besides that, so what if Iā€™m, in your words, ā€œhijackingā€ a post? Itā€™s not as if Iā€™m making off it or something!!! :joy: :joy:

Second: No it was a shot in the dark. I work in this nonprofit where the IT Mgr has left and now I have to clean up the mess, my boss on my *ss and got million other things to do. Oh forgot tot mention I was hired and supposed to work on security only!

Third: Yes, I did, on this site and million other sites, pretty much all day! NONE of the links, including the ones you posted here solved my problem!

FOURTH: KEEP YOUR CONDESCENDING ATTITUDE TO YOURSELF!

FIFTH: THANKS FOR NOTHING!

SIXTH: LEARN TO BE KIND TO OTHERS!

1 Like