我一直在使用下面的代码来查找过去30天内未登录的非活动广告用户,效果很好。
现在,我想从结果中排除一些用户,我需要帮助。我想排除Guest
和krbtgt
,也可能排除所有带有术语temporary
或template
的帐户。
import-module activedirectory
$domain = "%domain%"
$DaysInactive = 30
$time = (Get-Date).Adddays(-($DaysInactive))
# Get all AD User with lastLogonTimestamp less than our time and set to enable
Get-ADUser -Filter {LastLogonTimeStamp -lt $time -and enabled -eq $true} -Properties LastLogonTimeStamp |
# Output Name and lastLogonTimestamp into CSV
select-object Name,@{Name="Stamp"; Expression={[DateTime]::FromFileTime($_.lastLogonTimestamp)}} | export-csv %ltsvcdir%\Nem\InactiveUsers.csv -notypeinformation
答案 0 :(得分:1)
只需将您的过滤器扩展为包含Get-ADUser的其余要求
Get-ADUser -Filter {(LastLogonTimeStamp -lt $time) -and (enabled -eq $true) -and (Name -notlike "*Guest*") -and (Name -NotLike "*krbtgt*") -and (Name -NotLike "*temporary*") -and (Name -NotLike "*template*")} -Properties LastLogonTimeStamp