我正在嘗試獲取帳戶過期超過 6 個月的 AD 用戶串列。
這是我的代碼:
Get-ADUser -Filter {Enabled -eq $False} -Properties employeeID,sn,givenName,LastLogonDate | Where-Object {[STRING]$_.LastLogonDate -ne "" -and (Get-Date) -gt ([datetime]::parseexact([STRING]$_.LastLogonDate, "dd/MM/yyyy HH:mm:ss", $null)).AddMonths(6)} | select employeeID,sn,givenName,sAMAccountName,LastLogonDate | Sort-Object -Property sn | Export-Csv -Path $filename
我得到錯誤:
The DateTime represented by the string is not supported in the System.Globalization.GregorianCalendar
我還在 parseexact 中嘗試過(Get-Culture)。也不行。
而當簡單地執行類似的事情時,[datetime]::parseexact("21/05/2015 16:14:37", "dd/MM/yyyy HH:mm:ss", $null)
它會按預期作業。
我究竟做錯了什么 ?
謝謝
uj5u.com熱心網友回復:
您可以通過添加LastLogonDate到過濾器和洗掉來改進您的查詢Where-Object:
$limit = [datetime]::Now.AddMonths(-6).Date
$params = @{
Filter = "LastLogonDate -lt '$limit' -and Enabled -eq '$false'"
Properties = 'LastLogonDate', 'sn', 'EmployeeID'
}
Get-ADUser @params | Sort-Object sn | Export-Csv ....
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/417179.html
標籤:
