none
Powershell script to get users latest lastlogon and lastlogontimestamp RRS feed

  • General discussion

  • Can anyone have Powershell script which compare lastlogon and lastlogontimestamp of all users in all the domain controllers in domain (e.g we have 10 DCs on one domain ) and provide the latest logon date as a output.

    We have below script which only pulling lastlogon details.

    Need a script which compare lastlogon and lastlogontimestamp of all users in all the domain controllers in domain (e.g we have 10 DCs on one domain ) and provide the latest logondate as a output

    i am trying to achieve something like below.

    Output file should get below information.

    samaccount name

    recent logon details

     

     

    recent logon details  = output of compare lastlogon vs. lastlogontimestamp for each user across all the domain controllers to capture recent value<o:p></o:p>

    Its bit challenging code for me to write

    ================================================================

    Clear-Host

    import-module activedirectory 
    $lastUserLogons = @{}
    $filePathAndName = "E:\Logon\ad-last-logons.csv"
    $dateTimeFormat = "yyyy/MM/dd HH:mm:ss.ffffff"
    $ous = "Test1";

    $dcs = Get-ADDomainController -Filter * | Where-Object {$_.IsReadOnly -eq $false}

    foreach ($dc in $dcs) {
        Write-Host "DC = $($dc.HostName)"

        foreach ($ou in $ous) {
            Write-Host "`tOU = $ou"

            $users = Get-ADUser -Filter * -SearchBase "OU=$ou,OU=Users,OU=test,DC=XYZ,DC=AB" -Properties * | Where-Object {$_.lastLogon -ne $null}

            
            foreach ($user in $users) {
                $aduser = Get-ADUser $user.SamAccountName | Get-ADObject -Server $dc.HostName -Properties LastLogon
                $lastLogon = [DateTime]::FromFileTime($aduser.LastLogon)
                        
                if ($lastUserLogons[$user.SamAccountName]) {
                    $value = $lastUserLogons[$user.SamAccountName]
            
                    if ($lastLogon -gt $value) {
                        $lastUserLogons[$user.SamAccountName] = $lastLogon
                        Write-Host "`t`tUpdated $($user.SamAccountName) to last login of $($lastLogon.ToString($dateTimeFormat)) from $($dc.HostName) in OU = $ou"
                    }
                }
                else {
                    $lastUserLogons[$user.SamAccountName] = $lastLogon
                    Write-Host "`t`tAdded $($user.SamAccountName) with last login of $($lastLogon.ToString($dateTimeFormat)) from $($dc.HostName) in OU = $ou"
                }
            }    
        }
    }    

    Out-File -FilePath $filePathAndName -InputObject "username,lastlogon"
    foreach ($key in $lastUserLogons.Keys) {   
        $value = $lastUserLogons[$key]
        Out-File -FilePath $filePathAndName -Append -InputObject "$key,$($value.ToString($dateTimeFormat))"
    }


    • Edited by Kunalbhosale Wednesday, May 29, 2019 2:43 PM
    • Changed type Bill_Stewart Thursday, September 5, 2019 9:30 PM
    • Moved by Bill_Stewart Thursday, September 5, 2019 9:31 PM This is not "design solution for me" forum
    Tuesday, May 28, 2019 9:01 PM

All replies