How to get maxpwdAge attribute value in ActiveDire

2019-09-07 15:58发布

i am working with AD Server,i want to get the maxpwdAge attribute value...

i already try ADSi for that,but it gives an issue.

VARIANT var;
bsNamingContext=L"maxpwdAge";

hr = ADsGetObject(pszADsPath, IID_IADsUser, (void**) &pUser);
if(SUCCEEDED(hr))
{
VariantInit(&var);
hr = pUser->Get(bsNamingContext, &var);  
}

but,it gives -2147463155 (8000500d) error...

but i am using bsNamingContext=L"cn"; it gives the CN values correctly...

anyone can resolve it?

1条回答
ゆ 、 Hurt°
2楼-- · 2019-09-07 16:28

maxpwdAge is not included in user/contact/person LDAP class, so you can not retrieve it that way.

You need to query it from domain object, not user object

Try this:

Const ONE_HUNDRED_NANOSECOND = .000000100   ' .000000100 is equal to 10^-7
Const SECONDS_IN_DAY = 86400

Set objDomain = GetObject("LDAP://DC=fabrikam,DC=com")     ' LINE 4
Set objMaxPwdAge = objDomain.Get("maxPwdAge")              ' LINE 5

If objMaxPwdAge.LowPart = 0 Then
  WScript.Echo "The Maximum Password Age is set to 0 in the " & _
               "domain. Therefore, the password does not expire."
  WScript.Quit
Else
  dblMaxPwdNano = Abs(objMaxPwdAge.HighPart * 2^32 + objMaxPwdAge.LowPart)
  dblMaxPwdSecs = dblMaxPwdNano * ONE_HUNDRED_NANOSECOND   ' LINE 13
  dblMaxPwdDays = Int(dblMaxPwdSecs / SECONDS_IN_DAY)      ' LINE 14
  WScript.Echo "Maximum password age: " & dblMaxPwdDays & " days"
End If

UPDATE:

To convert large integer to human readable value use IADsLargeInteger dispatch interface

Note 1 : Example is in VB, but you can easily rewrite it, because of COM.

Note 2 : maxpwdAge is not configured per user, but per domain (until fine-grained password policies are enabled)

Further readings:

查看更多
登录 后发表回答