如何使用 PowerShell 在 Active Directory 中查找孤立的计算机对象?

MDM*_*rra 10 powershell active-directory

如何使用 PowerShell 查找我的 Active Directory 域中已不活动 x 天的所有计算机帐户?

请注意,我确实知道如何做到这一点。这是一个自我回答的问题,只是为了获得知识。如果其他人有更好的方法,请随时发布!

Mik*_*ike 10

这将为您提供过去 365 天内没有活动的所有计算机帐户。

Search-ADAccount -AccountInactive -ComputersOnly -TimeSpan 365.00:00:00
Run Code Online (Sandbox Code Playgroud)

这将按 lastlogondate 为您排序。

Search-ADAccount -AccountInactive -ComputersOnly -TimeSpan 365.00:00:00 | Sort-Object lastlogondate | Ft name,lastlogondate -auto
Run Code Online (Sandbox Code Playgroud)

这将使您禁用计算机帐户。

Search-ADAccount -AccountDisabled -ComputersOnly 
Run Code Online (Sandbox Code Playgroud)


MDM*_*rra 5

默认情况下,计算机每 30 天更改一次帐户密码。如果计算机在很长一段时间内没有更改其密码,则意味着它们不再连接到网络。

此 PowerShell 脚本将输出 2 个文本文件。一种用于禁用的计算机,一种用于孤立的计算机帐户对象。您必须安装 Active Directory PowerShell 模块。

在此示例中,我排除了“加密笔记本电脑”OU,因为它们是长时间断开连接的移动笔记本电脑。如果您没有类似的设置,可以删除该部分

Import-Module ActiveDirectory

$Date = [DateTime]::Today

#Sets the deadline for when computers should have last changed their password by.
$Deadline = $Date.AddDays(-365)   

#Makes the date string for file naming
$FileName = [string]$Date.month + [string]$Date.day + [string]$Date.year 


#Generates a list of computer accounts that are enabled and aren't in the Encrypted Computers OU, but haven't set their password since $Deadline
$OldList = Get-ADComputer -Filter {(PasswordLastSet -le $Deadline) -and (Enabled -eq $TRUE)} -Properties PasswordLastSet -ResultSetSize $NULL |
Where {$_.DistinguishedName -notlike "*Encrypted Laptops*"} | 
Sort-Object -property Name | FT Name,PasswordLastSet,Enabled -auto 

#Generates a list of computer accounts that are disabled and sorts by name.
$DisabledList = Get-ADComputer -Filter {(Enabled -eq $FALSE)} -Properties PasswordLastSet -ResultSetSize $null | 
Sort-Object -property Name | FT Name,PasswordLastSet,Enabled -auto

#Creates the two files, assuming they are not $NULL. If they are $NULL, the file will not be created.
if ($OldList -ne $NULL) {
    Out-File "C:\users\marra\desktop\Old$Filename.txt" -InputObject $OldList
}

if ($DisabledList -ne $NULL) {
    Out-File "C:\users\marra\desktop\Disabled$Filename.txt" -InputObject $DisabledList
}
Run Code Online (Sandbox Code Playgroud)