禁用Active Directory帐户的SQL查询

时间:2009-08-24 19:48:30

标签: sql active-directory

我需要查询AD以确定是否禁用了用户帐户。

使用答案here

中使用的类似查询
SELECT *
FROM OPENQUERY(ADSI, 'SELECT sAMAccountName
FROM ''LDAP://DC=MyDC,DC=com,DC=uk''
WHERE objectCategory = ''Person''
AND objectClass = ''user'')

我相信确定帐户是否已禁用我必须以某种方式使用userAccountControl字段。我尝试了几件事,但它们似乎没有起作用:

WHERE userAccountControl & 2 <> 0

3 个答案:

答案 0 :(得分:7)

在OPENQUERY()中:

  

AND''userAccountControl:1.2.840.113556.1.4.803:''&lt;&gt; 2

SELECT *
FROM OPENQUERY(ADSI, 'SELECT sAMAccountName
FROM ''LDAP://DC=MyDC,DC=com,DC=uk''
WHERE objectCategory = ''Person''
AND objectClass = ''user''
AND ''userAccountControl:1.2.840.113556.1.4.803:''<>2)

答案 1 :(得分:6)

怎么样:

SELECT sAMAccountName
FROM OPENQUERY(ADSI, 'SELECT sAMAccountName, userAccountControl 
FROM ''LDAP://DC=MyDC,DC=com,DC=uk'' 
WHERE objectCategory = ''Person'' 
AND objectClass = ''user''') 
WHERE userAccountControl & 2 <> 0; -- disabled

答案 2 :(得分:4)

显然它确实有效......这将是一个ID-10-T:p