模拟Active Directory lastLogonTimestamp属性更新

时间:2018-12-22 09:40:22

标签: powershell active-directory

根据以下文章,我试图复制计算结果,以决定是否更新lastLogonTimestamp。
https://blogs.technet.microsoft.com/askds/2009/04/15/the-lastlogontimestamp-attribute-what-it-was-designed-for-and-how-it-works/

# Get the users lastLogonTimestamp
$User = Get-ADUser -Identity TestUser -Properties lastLogonTimestamp

# Whatever value 'ms-DS-Logon-Time-Sync-Interval' is set (14 default)
$TimeSyncInterval = 14

# $TimeSyncInterval - (Random percentage of 5)
$X = $TimeSyncInterval - (5/100*(Get-Random -Minimum 0 -Maximum 100))

# CurrentDate - lastLogonTimestamp
$YFileTime = (Get-Date).ToFileTime() - $User.lastLogonTimestamp
$YDays = ([datetime]::FromFileTime($YFileTime)).Day

# Update or not?
if($X -lt $YDays) {
    Write-host "update lastLogonTimestamp" -ForegroundColor Cyan
}
elseif($X -gt $YDays) {
    Write-host "do not update lastLogonTimestamp" -ForegroundColor Yellow
}

如果我不将时差转换为“天”,则计算将类似于11 - 69352598407,因此没有任何意义。

如果我现在通过循环运行上述脚本块,它将永远不会告诉我会进行更新。 少于9天(如果您将TimeSyncInterval设置为14的话)的属性就没有机会更新

您可以通过添加以下内容来操纵CurrentDate以模拟未来。 9天。

$YFileTime = ((Get-Date).AddDays(9)).ToFileTime() - $User.lastLogonTimestamp

如果我现在再次通过循环运行脚本,它会告诉我有时会发生更新。

1..100 | Foreach-Object {
    # script from above
}

我觉得我在那里所做的事很正确,但也许有一些AD传奇人物在这里闲逛。

请留下您的反馈!

0 个答案:

没有答案
相关问题