[英]Handling Date in Power-Shell JSON Object
I am trying to get user info based on LastLogondate from AD.我试图从 AD 获取基于 LastLogondate 的用户信息。 I am converting final output in JSON format.
我正在以 JSON 格式转换最终输出。
Here the small script to handle dates.这里是处理日期的小脚本。 Actually I am passing $PasswordAge as LastLogonDate of the user.
实际上我将 $PasswordAge 作为用户的 LastLogonDate 传递。
$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.如果我使用 ConvertTo-Json 并打印输出,则会收到以下错误。 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.提前感谢您的帮助。
New-TimeSpan
sucks. New-TimeSpan
很烂。 I almost always get the same error and I have no idea how to fix it.我几乎总是遇到同样的错误,我不知道如何解决它。 Fortunately there's an easy workaround;
幸运的是,有一个简单的解决方法; just subtract two datetimes to get a
System.TimeSpan
instead.只需减去两个日期时间即可获得
System.TimeSpan
。
$LastLogonDays = ( $CurrentDate - $PasswordAge ).Days
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.