排除AD列表的帐户



Heloo

我有这个脚本来搜索AD中的非活动用户。有没有办法像我在列表中看到的服务帐户和类似的帐户一样,广告排除帐户?如果可能的话,我不在乎在运行之前是否必须手动添加每个帐户。

$Date = (Get-Date).AddDays(-90)
Get-ADUser -Filter {((Enabled -eq $true) -and (LastLogonDate -lt $date))} -Properties LastLogonDate | select samaccountname, Name, LastLogonDate | Sort-Object LastLogonDate | Export-CSV -Path "C:OutputInactiveADUsers.csv" -NoTypeInformation -Encoding UTF8

您可以使用Where-Object子句来进一步筛选您不想要的帐户,如下所示:

$Date = (Get-Date).AddDays(-90).Date
# create an array of SamAccountNames to exclude.
# if you have only one item, surround it with @() to force it as array
$excludes = 'jdoe', 'jbloggs'
Get-ADUser -Filter "Enabled -eq 'True' -and LastLogonDate -lt $date" -Properties LastLogonDate | 
Where-Object { $excludes -notcontains $_.SamAccountName } |
Select-Object SamAccountName, Name, LastLogonDate | 
Sort-Object LastLogonDate | 
Export-CSV -Path "C:OutputInactiveADUsers.csv" -NoTypeInformation -Encoding UTF8

p.S.-Filter应该是一个字符串,而不是脚本块,我已经使用.Date设置了与午夜进行比较的日期

相关内容

最新更新