I am trying to get user info based on LastLogondate from AD. I am converting final output in JSON format.
Here the small script to handle dates. Actually I am passing $PasswordAge as LastLogonDate of the user.
$PasswordAge = (Get-Date).AddDays(-60) # This is actually LastLogonDate from user info
$CurrentDate = (Get-Date)
#Write-Host $PasswordAge
try
{
$LastLogonDays = (New-TimeSpan -Start $PasswordAge -End $CurrentDate).Days
#Write-Host $LastLogonDays
}
catch{
$Message = $_.Exception.Message
}
$JSONOutput = @{"result"=$LastLogonDays;"error"=$Message} | ConvertTo-Json #-Compress
Write-Output $JSONOutput
Receiving below error if I use ConvertTo-Json and print output. Day difference is correct but every-time getting message in error
{
"error": "Cannot bind parameter \u0027Start\u0027 to the target. Exception setting \"Start\": \"Cannot convert null to type \"System.DateTime\".\"",
"result": 60
}
Thank for your help in advance.