I am trying to get user info based on LastLogondate from AD and figure out how many days since user has logged in based on LastLogonDate and Current Date.
Here is the script
$Result = Get-ADUser -filter {SamAccountName -eq 'username'} -Properties UserPrincipalName, mail, LastLogonDate, Enabled
$CurrentDate = (Get-Date)
Write-Host $Result.LastLogonDate
try
{
$LastLogonDays = ($CurrentDate - $Result.LastLogonDate).Days
Write-Host $LastLogonDays
$Json = @{
"UserPrincipalName"= $Result.UserPrincipalName;
"mail"= $Result.mail;
"LastLogonDate"= ($Result.LastLogonDate).ToString();
"days"= $LastLogonDays
}
$User = $Json
}
catch{
$Message = $_.Exception.Message
}
$JSONOutput = @{"result"=$User;"error"=$Message} | ConvertTo-Json #-Compress
Write-Output $JSONOutput
I am getting expected result but there is also error captured in log, not able to figure out why/how error is occurring
8/18/2021 4:16:45 AM
61
{
"error": "Cannot find an overload for \"op_Subtraction\" and the argument count: \"2\".",
"result": {
"LastLogonDate": "8/18/2021 4:16:45 AM",
"mail": "user@user.com",
"UserPrincipalName": "UserPrincipalName",
"days": 61
}
}