Created
October 8, 2018 17:12
-
-
Save colehocking/a9ee0e59c8b684d3346b02a289b71fbe to your computer and use it in GitHub Desktop.
Get last AD User logon
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Check if the account exists. | |
If($Results.Count -eq 0) | |
{ | |
Write-Warning "The SamAccountName '$UserName' cannot find. Please make sure that it exists." | |
} | |
Else | |
{ | |
Foreach($Result in $Results) | |
{ | |
$DistinguishedName = $Result.Properties.Item("DistinguishedName") | |
$LastLogonTimeStamp = $Result.Properties.Item("LastLogonTimeStamp") | |
If ($LastLogonTimeStamp.Count -eq 0) | |
{ | |
$Time = [DateTime]0 | |
} | |
Else | |
{ | |
$Time = [DateTime]$LastLogonTimeStamp.Item(0) | |
} | |
If ($LastLogonTimeStamp -eq 0) | |
{ | |
$LastLogon = $Time.AddYears(1600) | |
} | |
Else | |
{ | |
$LastLogon = $Time.AddYears(1600).ToLocalTime() | |
} | |
#Output in comma delimited format. | |
$Hash = @{ | |
SamAccountName = $UserName | |
LastLogonTimeStamp = $(If($LastLogon -match "12/31/1600") | |
{ | |
"Never Logon" | |
} | |
Else | |
{ | |
$LastLogon | |
}) | |
} | |
$Objs = New-Object -TypeName PSObject -Property $Hash | |
$Objs | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment