【发布时间】:2021-10-19 06:25:11
【问题描述】:
我正在尝试根据来自 AD 的 LastLogondate 获取用户信息。我正在将最终输出转换为 JSON 格式。
这里是处理日期的小脚本。实际上,我将 $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
如果我使用 ConvertTo-Json 并打印输出,则会收到以下错误。日差是正确的,但每次都收到错误消息
{
"error": "Cannot bind parameter \u0027Start\u0027 to the target. Exception setting \"Start\": \"Cannot convert null to type \"System.DateTime\".\"",
"result": 60
}
提前感谢您的帮助。
【问题讨论】:
标签: powershell