0

I'm trying to write a powershell script that accepts an username as an argument, and displays the last logon time of the user. If the user has not logged in before, the message has not logged in before should be displayed.

For example, if you run .\lastlogon -username marywong the message is displayed:

marywong last logon time 13/07/2017

If you run .\lastlogon -username guest, I get the message:

guest has not logged in before

Below is my code, however it doesn't seem to be looping into the else loop when the user has not logged in before.

param (
    [string]$username
)

$user = Get-ADUser -Filter {sAMAccountName -eq $username} | Get-ADObject -Properties lastLogon

$lastlogontime = $user.lastlogon

If ($user -ne $Null) {
    if($user.LastLogon -gt $time) {
        $displaylastlogon = [datetime]::FromFileTime($lastlogontime)
        Write-Host  $username " last logon time" $displaylastlogon 
    }
    else {
        $displaylastlogon = [datetime]::FromFileTime($lastlogontime)
        Write-Host  $username " has not logged in before"
    }
}
else {
    Write-Host  $username " does not exist"
}
1
  • 1
    Have you checked whether $null is returned for a user who hasn't logged on before? Commented Sep 24, 2018 at 15:19

2 Answers 2

1

There is information to be gained from using Get-ADUser and Get-ADObject separately. If the user has never logged in, they are still a user that exists. That is different from a user that does not exist.

[CmdletBinding()]
Param (
    [Parameter(Mandatory = $true, Position = 0)]
    [string]$username
)

$user = Get-ADUser -Filter {SamAccountName -eq $username}
if ($user -ne $null) {
    $userlogon = $user | Get-ADObject -Properties lastLogon
    if ($userlogon.LastLogon -ne $null) {
        $lastlogontime = [DateTime]::FromFileTime($userlogon.LastLogon)
        Write-Host  $username " last logon time" $lastlogontime
    } else {
        Write-Host  $username " has not logged in before"
    }
} else {
    Write-Host  $username " does not exist"
}
Sign up to request clarification or add additional context in comments.

Comments

0

When you use the lastLogon you get a format that AD uses... then when the if is running you get

Could not compare "131820853335016078" to "09/24/2018 18:18:57". Error: "Cannot convert value "9/24/2018 6:18:57 PM" to type "System.Int64". Error: "Invalid cast from 'DateTime' to 'Int64'.""

so it's not getting to the else..

try using the LastLogonDate property, it will help you more.

try to use this:

$user = Get-ADUser -Filter {sAMAccountName -eq $username} -Properties LastLogonDate

$lastlogontime = $user.lastlogonDate

Edit:

you have some more issues with you code:

  1. You need to remove the displaylastlogon

  2. you cant use -gt because it will always be false.. the user cant log in the future.. you need to use -lt

here is the full script, working:

$user = Get-ADUser -Filter {sAMAccountName -eq $username} -Properties LastLogonDate

$lastlogontime = $user.lastlogonDate

If ($user -ne $Null) {

if($lastlogontime -lt $time)
    {
          Write-Host  $username " last logon time" $lastlogontime 
    }

    else

    {
          Write-Host  $username " has not logged in before"
    }
}

else

    {
      Write-Host  $username " does not exist"
    }

Another Edit:

I just notice that its not answering the case when user never logon, because you will get $null and $null is lower then the current Time. so you need to check that the lastlogontime is not null

change the if to this:

if($lastlogontime -ne $null -and $lastlogontime -lt $time)

2 Comments

Hi, I have tried but even though the user has logon before but it still looping to user has not logged in before and I have checked the lastlogontime is not null. Please advice.
Hey @Nyleve, Have you set the $time parameter? I didnt include it in my script as I sew in your script you already included it.. if no, in the second line, add $time = get-date . Let me know if it helped you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.