-1

我需要将 Active Directory 中的 pwdLastSet 字段与当前日期进行比较,如果该日期差是可配置的数字(由客户端固定),我们需要触发密码即将过期的电子邮件。此日期比较还需要使用 Active Directory 中的 LastLoginTimeStamp 完成以发送电子邮件。比较这些字段的最佳方法是什么,因为 pwdLastSet 是一个长整数,并且无法使用 DirectorySearcher 过滤器与当前日期进行比较。

4

3 回答 3

0

查看密码何时到期

Get-ADUser -filter {Enabled -eq $True -and PasswordNeverExpires -eq $False} –Properties “DisplayName”, “msDS-UserPasswordExpiryTimeComputed” |
    Select-Object -Property “Displayname”,@{Name=“ExpiryDate”;Expression={[datetime]::FromFileTime($_.“msDS-UserPasswordExpiryTimeComputed”)}} | export-csv -path c:\paswsword-expiring.csv

查看上次设置密码的时间

Get-ADUser -filter * -properties Displayname,PasswordLastSet | select displayname,passwordlastset |Format-List

设置电子邮件警报

http://www.powershelladmin.com/wiki/Active_directory_password_expiration_notification

于 2016-07-12T17:53:14.083 回答
0

您可以通过使用命名空间中类的可为DateTimeLastPasswordSet属性来获取类型中目录用户的最后密码设置日期。 UserPrincipalSystem.DirectoryServices.AccountManagement

如果User must change password at next logon选中选项,则LastPasswordSet属性返回null值。否则,它会返回最后一次在 type 中设置密码的日期和时间DateTime。然后,您可以将日期与DateTime.Comparemathod 进行比较。

using(PrincipalContext context = new PrincipalContext(ContextType.Domain))
{
    UserPrincipal user = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, Username);
    //? - to mark DateTime type as nullable
    DateTime? pwdLastSet = (DateTime?)user.LastPasswordSet;
    int delta = 7;
    if (pwdLastSet != null)
    {
        if (DateTime.Compare((DateTime)pwdLastSet, DateTime.Now) < delta)
        {
            //send email
            ...
        }
    }
}

MSDN:用户主体
MSDN:LastPasswordSet

于 2016-07-16T16:21:01.983 回答
0

从 pwdLastSet 获取日期时间:

SearchResult sr = ds.FindOne(); 
hacked = DateTime.FromFileTime((long)sr.Properties["pwdLastSet"][0]); 

请参阅Casting ActiveDirectory pwdLastSet 属性而不使用 ActiveDs检查时间跨度

于 2016-07-12T07:39:42.917 回答