查找密码已过期用户及长时间不登录

# 导入 Active Directory 模块
Import-Module ActiveDirectory

# 定义变量来存储过期时间和不活跃时间的阈值(单位为天)
$expiredThreshold = 30
$inactiveThreshold = 90

# 查找密码已过期的用户
Get-ADUser -Filter {Enabled -eq $true -and PasswordNeverExpires -eq $false -and PasswordExpired -eq $true} | Select-Object Name, SamAccountName, EmailAddress, PasswordLastSet, @{Name="PasswordAge"; Expression={(New-TimeSpan -Start $_.PasswordLastSet).Days}} | Where-Object {$_.PasswordAge -ge $expiredThreshold}

# 查找长时间未登录的用户
Get-ADUser -Filter {Enabled -eq $true -and PasswordNeverExpires -eq $false -and LastLogonTimeStamp -lt (Get-Date).AddDays(-$inactiveThreshold)} -Properties LastLogonTimeStamp | Select-Object Name, SamAccountName, EmailAddress, LastLogonTimeStamp, @{Name="LastLogon";

密码即将过期的用户

# 导入 Active Directory 模块
Import-Module ActiveDirectory

# 定义变量来存储过期时间和不活跃时间的阈值(单位为天)
$expiredThreshold = 30
$inactiveThreshold = 90

# 查找密码已过期的用户
Get-ADUser -Filter {Enabled -eq $true -and PasswordNeverExpires -eq $false -and PasswordExpired -eq $true} | Select-Object Name, SamAccountName, EmailAddress, PasswordLastSet, @{Name="PasswordAge"; Expression={(New-TimeSpan -Start $_.PasswordLastSet).Days}} | Where-Object {$_.PasswordAge -ge $expiredThreshold}

# 查找长时间未登录的用户
Get-ADUser -Filter {Enabled -eq $true -and PasswordNeverExpires -eq $false -and LastLogonTimeStamp -lt (Get-Date).AddDays(-$inactiveThreshold)} -Properties LastLogonTimeStamp | Select-Object Name, SamAccountName, EmailAddress, LastLogonTimeStamp, @{Name="LastLogon";

查找脱域的计算机对象

Get-ADComputer -Filter 'OperatingSystem -notlike "*Windows*" -and Enabled -eq $true'

搜索即将过期的计算机对象

Search-ADAccount -AccountInactive -ComputersOnly -TimeSpan "30"

查找过去的一天内使用指定用户名登录到域中的计算机的名称和登录时间

Get-WinEvent -FilterHashtable @{Logname='Security';ID=4624;StartTime=(Get-Date).AddDays(-1)} | where { $_.Message -like "*Logon Type:% 3*" -and $_.Properties[5].Value -eq 'DOMAIN\\USERNAME' } | select TimeCreated, @{Name="Computer";Expression={$_.Properties[1].Value}}

查找到哪个AD账户登录了特定AD计算机

Get-WinEvent -ComputerName <computername> -FilterHashtable @{LogName="Security";ID="4624"} | where { $_.Message -match "Logon type:\s* 2" }

上述命令会筛选出 ID 为 4624 的安全事件日志,并找到其中 Logon Type 为 2(互动式登录)的事件,从而获取到登录到该计算机上的用户信息。