What's the best way to compare and combine the DateTime stamp as calculated property?

Viewed 64

The below script was created to get the actual most recent AD LastLogon from all domain controllers. I need to get the oldest date or the most recent entry from the LastLogon attribute.

However, the results are different and it is quite confusing like below: enter image description here

This is the script I've come up so far:

function Get-ADUserLastLogon([string]$userName) {
    $dcs = Get-ADDomainController -Filter { Name -like "*" }
    $time = 0
    foreach ($dc in $dcs) {
        Write-Host "Processing $($userName) ... $($dc.HostName) [$($(Resolve-DnsName -Name $dc.hostname).IPaddress)]" -ForegroundColor Green
        $user = Get-ADUser $userName | Get-ADObject -Properties lastLogon
        if ($user.LastLogon -gt $time) {
            $time = $user.LastLogon
        }
    }
    $dt = [DateTime]::FromFileTime($time)
    Write-Host "`n" $username "last logged on at:" $dt " `n" -ForegroundColor Cyan
    return $dt
}

$filter = { Enabled -eq $True}
$properties = 'Name', 'canonicalName', 'whenCreated', 'displayName', 'lastlogondate', 'lastlogon'
$Name = 'Enterprise.Admins'

Get-ADUser $Name -Properties $properties |
    Select-Object -Property DisplayName,
                            SamAccountName,
                            UserPrincipalName,
                            WhenCreated,
                            @{ n = 'Most Recent Logon'; e = { Get-ADUserLastLogon -UserName $_.SamaccountName } },
                            @{ n = 'Real Last Logon'; e = { $sam = $_.SamaccountName; [datetime]::FromFileTime((Get-ADDomainController -Filter { Name -like "*" } | ForEach { Write-Host "Server: $($_.Name) - DisplayName: $($sam)" -ForegroundColor Yellow ; Get-ADUser $sam -Properties $properties -Server $_.Name | Select-Object LastLogon} | Measure-Object -Property LastLogon -Maximum).Maximum) } },
                            LastLogonDate | Out-GridView

How can we combine the above three columns Real Last Logon, Most Recent Logon and LastLogonDate into one proper column?

2 Answers

As for ...

"How can we combine the above three columns Real Last Logon, Most Recent Logon and LastLogonDate into one proper column?"

I don't have an ADDS environment handy to use that, so just using the filesystem and random text stuff.

You can use one calculated property for all three dates. Say, @{Name = 'LogOnDates';Expression {' all your date info here'}}, but this then will show up as a single line representation in that single column.

Clear-Host
Get-ChildItem -Path 'D:\Temp\*.txt' | 
Select-Object -First 1 | 
Select-Object -Property FullName, 
@{
    Name = 'DateDetails'
    Expression = {@(
                        $PSItem.CreationTime, 
                        $PSItem.LastAccessTime, 
                        $PSItem.LastWriteTime
                   )
                  }
} | Out-Gridview

Yet, what it sounds like what you are after is to have those heading as well, and that is not something OGV has the ability to do. You'd have to define your own custom version of OGV or doing stuff like this:

Clear-Host
Get-ChildItem -Path 'D:\Temp\*.txt' | 
Select-Object -First 1 | 
Select-Object -Property FullName, 
@{
    Name = 'DateDetails'
    Expression = {@(
                        [PSCUstomObject]@{
                            CreationTime   = $PSItem.CreationTime
                            LastAccessTime = $PSItem.LastAccessTime
                            LastWriteTime  = $PSItem.LastWriteTime
                        }
                   )
                  }
} | Out-Gridview

Or getting more creative with approaches like this:

$col = @(

    (New-Object –TypeName PSObject –Prop @{'id'='01';'name'='a';'items'=@('the first item','the second item', 'the third item')}),
    (New-Object –TypeName PSObject –Prop @{'id'='02';'name'='b';'items'=@('the first item','the second item', 'the third item', 'the third item', 'the third item', 'the third item', 'the third item', 'the third item', 'the third item', 'the third item', 'the third item', 'the third item', 'the third item', 'the third item', 'the third item', 'the third item', 'the third item', 'the third item', 'the third item')}),
    (New-Object –TypeName PSObject –Prop @{'id'='03';'name'='c';'items'=@('the first item','the second item', 'the third item')})
    )

$col | Select -p id,@{E={$_.items -join "`n"};L="Items"},name | Out-GridView

Or trick like this:

 ('[{"a":1,"b":2},{"a":3,"b":4},{"a":5,"b":6}]' | 
ConvertFrom-Json) | 
Out-GridView

Or

$List = @()
$List += [pscustomobject]@{
 Column1 = "abc"
 Column2 = "cde"
 Column3 = "fgd"
}
$List += [pscustomobject]@{
 Column1 = "cab"
 Column2 = "dre"
 Column3 = "fde"
}
$List | Out-GridView

It sounds like your goal is to extract three datetime values, compare them with each other, and return the recent-most one. In this case, you could pass all variables to Measure-Object like so:

(($MostRecentLogon,$RealLastLogon,$LastLogonDate) | Measure-Object -Maximum).Maximum

This will compare all three dates and output the highest value (i.e., the most recent date).

Related