【发布时间】:2023-02-25 02:35:54
【问题描述】:
我想列出所有域控制器中根本没有经过身份验证的用户帐户。从不登录所有域控制器意味着在 LastLogon 属性中没有设置日期。
例子:
I have 36 DCs in total
User1 has LastLogonDate in DC1 and 2 --> do not show this user.
User2 has LastLogonDate in DC2 only --> do not show this user.
User3 has LastLogonDate in DC32 only --> do not show this user.
User4 has no LastLogonDate value in all 36 dcs --> this is the user to be added into the .CSV file.
User5 has no LastLogonDate value in all 36 dcs --> this is the user to be added into the .CSV file.
如何修改该功能,使其仅显示或导出 AD 用户帐户,其中最后一次登录返回或所有 $allDCs 中的 '1/01/1601 11:00:00 AM' | ForEach 对象循环?
不知何故,下面的脚本仍然返回或显示仅登录一个 AD 域控制器的帐户。
我拥有的 AD 域控制器总数是 36,所以当 AD 帐户没有在所有 36 个 DC 中填充上次登录日期时,这就是我想要的。
Function Get-ADLastLogonNever {
[CmdletBinding()]
Param (
[Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
[ValidateNotNullOrEmpty()]
[String]$SAMAccountName
)
Begin {
$allDCs = Get-ADDomainController -Filter *
}
Process {
$neverLoggedOn = $true
ForEach ($dc in $allDCs) {
$adUser = Get-ADUser -Identity $SAMAccountName -Properties LastLogon, DisplayName -Server $dc.Name
If ($_.lastLogon -gt 0) {
$neverLoggedOn = $false
Break
}
}
If ($neverLoggedOn) {
$adUser | Select-Object -Property `
SamAccountName,
DisplayName,
@{n = 'DC'; e = {'<All>'}},
@{n = 'LastLogon'; e = {'<Never>'}}
}
}
}
'IT Team', 'Finance Team', 'HR Team' |
Get-ADGroupMember |
Get-ADLastLogonNever |
Export-Csv -Path C:\result.csv -NoTypeInformation
谢谢。
【问题讨论】:
-
你真的应该在这里使用广告过滤器
标签: powershell active-directory