Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#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