我試圖從 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
}
提前感謝您的幫助。
uj5u.com熱心網友回復:
New-TimeSpan糟透了。我幾乎總是遇到同樣的錯誤。據報道,該問題源于作業系統上的文化(語言)設定、作業系統配置為用于日期和時間的格式以及DateTime物件實際使用的格式不匹配。我住在美國,請使用英語語言,并使用我們的標準日期格式,但我仍然New-TimeSpan無法正常作業。我早已放棄讓這個 cmdlet 始終如一地作業。
幸運的是,有一個簡單的解決方法;只需減去兩個日期時間即可得到一個System.TimeSpan。每次都有效。
$LastLogonDays = ( $CurrentDate - $PasswordAge ).Days
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/323313.html
標籤:电源外壳
