2023年6月21日发(作者:)

LDAP查询基础这篇⽂章讨论了轻量级⽬录访问协议(Lightweight Directory Access Protocol - LDAP)的查询. 在排查Microsoft® Exchange Server 和它与⽬录之间关系的时候这项技术⾮常有⽤, 但是确有点不容易搞清楚. 这篇⽂章提供了关于LDAP查询的基础知识.

LDAP语法基础============== (EQUAL TO)这个LDAP参数的意思是某项属性与某个值相等这项条件必须为真. ⽐如说, 如果你想要找到所有名(firstname)为John的对象, 你应该使⽤:(givenName=John)这会返回所有first name是John的对象. 这⾥的括号⽤来强调LDAP语句的开始与结束的位置.

& (logical AND)当你有多个条件都需要满⾜的时候, 你可以使⽤这项语法. ⽐如说, 如果你想要找到所有的first name是John, 并且住在Dallas的⼈, 你应该这样⽤:(&(givenName=John)(l=Dallas))注意每⼀个参数都有⾃⼰的⼀套括号. 整个LDAP语句必须被⼀套主括号包围起来. 这⾥的 & 操作符意味着对于你查询的对象, 对于您指定的filter中的每⼀个参数条件都必须为真.

! (logical NOT)这个操作符被⽤来排除满⾜某项条件的对象. 假设你需要找到除去那些first name是John的所有对象. 你需要使⽤下⾯的语句:(!givenName=John)这个语句会找到所有first name不是John的对象. 注意 ! 操作符直接放在参数的前⾯, 并且处在括号对⼉的⾥⾯. 因为这个语句⾥只有⼀个参数,为了说明问题把它⽤括号包围起来了.

* (wildcard)You use the wildcard operator to represent a value that could be equal to anything. One such situation might be if you wanted to find allobjects that have a value for title. You would then use:你可以使⽤通配符操作符来表⽰⼀个可以等于任何值的数值. ⼀个可能的情形是你想要找到所有title被设置了值的(有值的)对象. 你应该这样⽤:(title=*)这会返回所有title属性有值的对象. 另⼀个例⼦, 如果你知道⼀个对象的first name由Jo开始. 那么, 你可以使⽤下⾯的查询语句来查找这样的对象.(givenName=Jo*)This would apply to all objects whose first name starts with Jo.这项查询会在所有以Jo开始的first name的对象上.

下⾯的是⼀些LDAP语法的更加⾼级些的例⼦:你需要⼀个filter来找到所有在Dallas或者Austin的对象, 并且first name还得是John. 这个filter应该是:(&(givenName=John)(|(l=Dallas)(l=Austin)))

你已经在Application log中收到了9548个event, 你需要找到所有的引发这个⽇志事件的对象. 在这种情况下, 你需要找到所有的被disable的user, (msExchUserAccountControl=2) 并且在msExchMasterAccountSID这个属性上没有值.

(&(msExchUserAccountControl=2)(!msExchMasterAccountSID=*))

The ! operator in conjunction with the wildcard operator will look for objects where that attribute is not set to anything.注意. ! 操作符跟通配符连接在⼀起, 会寻找属性没被设为任何值的对象.

在哪⾥使⽤LDAP查询呢?==============在与Exchange服务器⼀起⼯作的时候, 你会遇到使⽤LDAP字符串的情形. ⽐如说, ⽆论何时, 你设置⼀个接受者, 或者mailbox管理者策略, 或者⼀个地址列表filter, 或者当你搜索Active Directroy⽬录服务的时候, Exchange服务器都会使⽤LDAP. 在下⼀个部分, 会讨论⼀些你可以使⽤LDAP语句的⽅法:

⼀. 使⽤Active Directory Users and Computers进⾏搜索下⾯的例⼦描述了如何使⽤Active Directory Users and Computers进⾏搜索:1. 打开Active Directory Users and Computers (Win + R, )2. 右键点击域对象(domain object), 然后选择Find.3. 点击紧挨着Find的下拉列表, 然后选择Custom Search.4. 在下⾯的屏幕中, 选择Advanced 选项卡.5. 在Enter LDAP query下⾯输⼊恰当的LDAP语句.

这个例⼦会试图寻找所有满⾜下⾯条件的对象: 他们的title属性是prez, 或者他们的名字由test开始. 如果你点击Find Now, 你应该看到恰当的输出结果, 请看下⾯的截屏:

⼆. 使⽤LDP进⾏搜索你还可以使⽤LDP进⾏搜索, 这个⼯具包含在Microsoft Windows Server™ 2003 和 Windows® 2000 Server support tools中. 这个⼯具给予你不仅仅是搜索domain container的能⼒, 还有搜索Configuration container的能⼒. 你⾸先会打开LDP, 然后连接到合法的domain controller上.然后你需要使⽤合适的credential来绑定. 选择View, 然后选择Tree. 保持Base DN栏为空, 点击OK.导航到你想要搜索的container上, 右击container, 然后选择Search.⼀个对话框会tanchu,其中会包含Base DN的正确值. 输⼊你想要filter过滤的LDAP语句, 像前⾯那样的结果应该会出现的.如果你想要搜索你当前所在的containter下⾯的所有层次, 你需要确保选项Subtree被选中. 点击Run, 它应该会找到所有满⾜条件的结果.

三. 使⽤LDIFDE搜索你需要为获取⼀个满⾜某种条件的对象集⽽进⾏查询, ⽽且你希望建⽴⼀个这些user的列表. LDIFDE是⼀个命令⾏⼯具, 它可以帮助你建⽴这样的列表. 假设你希望找到并保存所有mailNickName 以Jeff开始的⽤户列表. 你可以运⾏下⾯的命令.C:>ldifde -d "DC=witaylorroot,DC=com" -f c: -r "(&(objectClass=user)(mailNickName=jeff*))

在这个例⼦⾥, 你将会找到三个对象, 然后输出所有他们的属性到⽂件中. 如果你输出近千个⽤户的结果, 输出结果⽂件就会变得⾮常庞⼤. 还有另⼀个不同的选项. 你可以使⽤-l(⼩写的L)开关来指定要输出那些属性. distinguished name 永远会被输出的, 但是如果你不想要其他的东西, 你可以添加-l nothing到字符串后⾯, 这会streamline 输出结果. 下⾯就是⼀个例⼦:C:>ldifde -d "DC=witaylorroot,DC=com" -f c: -l nothing -r "(&(objectClass=user)(mailNickName=jeff*))

Consider that in this case, you want to use the LDIFDE export referenced earlier, but only want to include the homeMDB attribute in theoutput. You must use the following command.如果你想要包括homeMDB 属性到结果中. 那么你就要运⾏下⾯的命令:C:>ldifde -d "DC=witaylorroot,DC=com" -f c: -l "homeMDB" -r "(&(objectClass=user)(mailNickName=jeff*))

dn: CN=jeff,OU=55Users,DC=witaylorroot,DC=com changetype: add homeMDB: CN=Private Information Store (WITAYLORNT4EX55),CN=First StorageGroup,CN=InformationStore,CN=WITAYLORNT4EX55,CN=Servers,CN=WITAYLORMIXEDSITE,CN=AdministrativeGroups,CN=WITAYLORORG,CN=Microsoft Exchange,CN=Services,CN=Configuration,DC=witaylorroot,DC=com

dn: CN=jeff2,CN=Users,DC=witaylorroot,DC=com changetype: add homeMDB: CN=Private Information Store (WITAYLORNT4EX55),CN=First StorageGroup,CN=InformationStore,CN=WITAYLORNT4EX55,CN=Servers,CN=WITAYLORMIXEDSITE,CN=AdministrativeGroups,CN=WITAYLORORG,CN=Microsoft Exchange,CN=Services,CN=Configuration,DC=witaylorroot,DC=com

dn: CN=jeff3,CN=Users,DC=witaylorroot,DC=com changetype: add homeMDB: CN=Private Information Store (WITAYLORNT4EX55),CN=First StorageGroup,CN=InformationStore,CN=WITAYLORNT4EX55,CN=Servers,CN=WITAYLORMIXEDSITE,CN=AdministrativeGroups,CN=WITAYLORORG,CN=Microsoft Exchange,CN=Services,CN=Configuration,DC=witaylorroot,DC=com

如果你进⾏⼀个输出操作⽽不指定哪些属性被输出, 你就会想要使⽤-n开关来去掉普通情形下被包括进来的值. 这会帮助防⽌输出⽂件变得过⼤.

四, 在ADModify中使⽤LDAP查询译注: 这个⼯具不常⽤, 还得上case才能拿到这个⼯具, 就不翻译了. 列在这⾥.To obtain the ADModify tool, contact Microsoft Product Support Services. For more information about how to contact Microsoft ProductSupport Services, see the .To obtain the ADModify tool from a third-party Web site, see the following GotDotNet Web site: .ADModify is a tool that Microsoft Product Support Services uses on a daily basis. With large Active Directory environments, it is not alwayseasy to add an entire organizational unit (OU) that could have thousands of users in it to the list on the right side, and then parse through allof them to find the users you need to change. There is an alternative. On the first screen, choose Modify Existing User Attributes and clickNext. On the Modify Active Directory Users screen, there is an Advanced you click the Advanced button, the Custom LDAP Filter dialog box appears. In this dialog box, you type the LDAP filter that you want touse. In this example, you only want to list the groups that are mail-enabled. It would look like the , click OK. Select the OU or the domain where you want ADModify to search. If you want it to look in lower-level containers within theone you selected, make sure to select Traverse Subcontainers when Enumerating Users. Click Add to List, and then click Yes whenwarned about how long it could take. The objects that meet the criteria you specified should now appear in the right here, highlight the objects that you want to modify, and continue with the wizard.

译⾃:LDAP Query Basics链接:LDIFDE - Export / Import data from Active Directory - LDIFDE commands

2023年6月21日发(作者:)

LDAP查询基础这篇⽂章讨论了轻量级⽬录访问协议(Lightweight Directory Access Protocol - LDAP)的查询. 在排查Microsoft® Exchange Server 和它与⽬录之间关系的时候这项技术⾮常有⽤, 但是确有点不容易搞清楚. 这篇⽂章提供了关于LDAP查询的基础知识.

LDAP语法基础============== (EQUAL TO)这个LDAP参数的意思是某项属性与某个值相等这项条件必须为真. ⽐如说, 如果你想要找到所有名(firstname)为John的对象, 你应该使⽤:(givenName=John)这会返回所有first name是John的对象. 这⾥的括号⽤来强调LDAP语句的开始与结束的位置.

& (logical AND)当你有多个条件都需要满⾜的时候, 你可以使⽤这项语法. ⽐如说, 如果你想要找到所有的first name是John, 并且住在Dallas的⼈, 你应该这样⽤:(&(givenName=John)(l=Dallas))注意每⼀个参数都有⾃⼰的⼀套括号. 整个LDAP语句必须被⼀套主括号包围起来. 这⾥的 & 操作符意味着对于你查询的对象, 对于您指定的filter中的每⼀个参数条件都必须为真.

! (logical NOT)这个操作符被⽤来排除满⾜某项条件的对象. 假设你需要找到除去那些first name是John的所有对象. 你需要使⽤下⾯的语句:(!givenName=John)这个语句会找到所有first name不是John的对象. 注意 ! 操作符直接放在参数的前⾯, 并且处在括号对⼉的⾥⾯. 因为这个语句⾥只有⼀个参数,为了说明问题把它⽤括号包围起来了.

* (wildcard)You use the wildcard operator to represent a value that could be equal to anything. One such situation might be if you wanted to find allobjects that have a value for title. You would then use:你可以使⽤通配符操作符来表⽰⼀个可以等于任何值的数值. ⼀个可能的情形是你想要找到所有title被设置了值的(有值的)对象. 你应该这样⽤:(title=*)这会返回所有title属性有值的对象. 另⼀个例⼦, 如果你知道⼀个对象的first name由Jo开始. 那么, 你可以使⽤下⾯的查询语句来查找这样的对象.(givenName=Jo*)This would apply to all objects whose first name starts with Jo.这项查询会在所有以Jo开始的first name的对象上.

下⾯的是⼀些LDAP语法的更加⾼级些的例⼦:你需要⼀个filter来找到所有在Dallas或者Austin的对象, 并且first name还得是John. 这个filter应该是:(&(givenName=John)(|(l=Dallas)(l=Austin)))

你已经在Application log中收到了9548个event, 你需要找到所有的引发这个⽇志事件的对象. 在这种情况下, 你需要找到所有的被disable的user, (msExchUserAccountControl=2) 并且在msExchMasterAccountSID这个属性上没有值.

(&(msExchUserAccountControl=2)(!msExchMasterAccountSID=*))

The ! operator in conjunction with the wildcard operator will look for objects where that attribute is not set to anything.注意. ! 操作符跟通配符连接在⼀起, 会寻找属性没被设为任何值的对象.

在哪⾥使⽤LDAP查询呢?==============在与Exchange服务器⼀起⼯作的时候, 你会遇到使⽤LDAP字符串的情形. ⽐如说, ⽆论何时, 你设置⼀个接受者, 或者mailbox管理者策略, 或者⼀个地址列表filter, 或者当你搜索Active Directroy⽬录服务的时候, Exchange服务器都会使⽤LDAP. 在下⼀个部分, 会讨论⼀些你可以使⽤LDAP语句的⽅法:

⼀. 使⽤Active Directory Users and Computers进⾏搜索下⾯的例⼦描述了如何使⽤Active Directory Users and Computers进⾏搜索:1. 打开Active Directory Users and Computers (Win + R, )2. 右键点击域对象(domain object), 然后选择Find.3. 点击紧挨着Find的下拉列表, 然后选择Custom Search.4. 在下⾯的屏幕中, 选择Advanced 选项卡.5. 在Enter LDAP query下⾯输⼊恰当的LDAP语句.

这个例⼦会试图寻找所有满⾜下⾯条件的对象: 他们的title属性是prez, 或者他们的名字由test开始. 如果你点击Find Now, 你应该看到恰当的输出结果, 请看下⾯的截屏:

⼆. 使⽤LDP进⾏搜索你还可以使⽤LDP进⾏搜索, 这个⼯具包含在Microsoft Windows Server™ 2003 和 Windows® 2000 Server support tools中. 这个⼯具给予你不仅仅是搜索domain container的能⼒, 还有搜索Configuration container的能⼒. 你⾸先会打开LDP, 然后连接到合法的domain controller上.然后你需要使⽤合适的credential来绑定. 选择View, 然后选择Tree. 保持Base DN栏为空, 点击OK.导航到你想要搜索的container上, 右击container, 然后选择Search.⼀个对话框会tanchu,其中会包含Base DN的正确值. 输⼊你想要filter过滤的LDAP语句, 像前⾯那样的结果应该会出现的.如果你想要搜索你当前所在的containter下⾯的所有层次, 你需要确保选项Subtree被选中. 点击Run, 它应该会找到所有满⾜条件的结果.

三. 使⽤LDIFDE搜索你需要为获取⼀个满⾜某种条件的对象集⽽进⾏查询, ⽽且你希望建⽴⼀个这些user的列表. LDIFDE是⼀个命令⾏⼯具, 它可以帮助你建⽴这样的列表. 假设你希望找到并保存所有mailNickName 以Jeff开始的⽤户列表. 你可以运⾏下⾯的命令.C:>ldifde -d "DC=witaylorroot,DC=com" -f c: -r "(&(objectClass=user)(mailNickName=jeff*))

在这个例⼦⾥, 你将会找到三个对象, 然后输出所有他们的属性到⽂件中. 如果你输出近千个⽤户的结果, 输出结果⽂件就会变得⾮常庞⼤. 还有另⼀个不同的选项. 你可以使⽤-l(⼩写的L)开关来指定要输出那些属性. distinguished name 永远会被输出的, 但是如果你不想要其他的东西, 你可以添加-l nothing到字符串后⾯, 这会streamline 输出结果. 下⾯就是⼀个例⼦:C:>ldifde -d "DC=witaylorroot,DC=com" -f c: -l nothing -r "(&(objectClass=user)(mailNickName=jeff*))

Consider that in this case, you want to use the LDIFDE export referenced earlier, but only want to include the homeMDB attribute in theoutput. You must use the following command.如果你想要包括homeMDB 属性到结果中. 那么你就要运⾏下⾯的命令:C:>ldifde -d "DC=witaylorroot,DC=com" -f c: -l "homeMDB" -r "(&(objectClass=user)(mailNickName=jeff*))

dn: CN=jeff,OU=55Users,DC=witaylorroot,DC=com changetype: add homeMDB: CN=Private Information Store (WITAYLORNT4EX55),CN=First StorageGroup,CN=InformationStore,CN=WITAYLORNT4EX55,CN=Servers,CN=WITAYLORMIXEDSITE,CN=AdministrativeGroups,CN=WITAYLORORG,CN=Microsoft Exchange,CN=Services,CN=Configuration,DC=witaylorroot,DC=com

dn: CN=jeff2,CN=Users,DC=witaylorroot,DC=com changetype: add homeMDB: CN=Private Information Store (WITAYLORNT4EX55),CN=First StorageGroup,CN=InformationStore,CN=WITAYLORNT4EX55,CN=Servers,CN=WITAYLORMIXEDSITE,CN=AdministrativeGroups,CN=WITAYLORORG,CN=Microsoft Exchange,CN=Services,CN=Configuration,DC=witaylorroot,DC=com

dn: CN=jeff3,CN=Users,DC=witaylorroot,DC=com changetype: add homeMDB: CN=Private Information Store (WITAYLORNT4EX55),CN=First StorageGroup,CN=InformationStore,CN=WITAYLORNT4EX55,CN=Servers,CN=WITAYLORMIXEDSITE,CN=AdministrativeGroups,CN=WITAYLORORG,CN=Microsoft Exchange,CN=Services,CN=Configuration,DC=witaylorroot,DC=com

如果你进⾏⼀个输出操作⽽不指定哪些属性被输出, 你就会想要使⽤-n开关来去掉普通情形下被包括进来的值. 这会帮助防⽌输出⽂件变得过⼤.

四, 在ADModify中使⽤LDAP查询译注: 这个⼯具不常⽤, 还得上case才能拿到这个⼯具, 就不翻译了. 列在这⾥.To obtain the ADModify tool, contact Microsoft Product Support Services. For more information about how to contact Microsoft ProductSupport Services, see the .To obtain the ADModify tool from a third-party Web site, see the following GotDotNet Web site: .ADModify is a tool that Microsoft Product Support Services uses on a daily basis. With large Active Directory environments, it is not alwayseasy to add an entire organizational unit (OU) that could have thousands of users in it to the list on the right side, and then parse through allof them to find the users you need to change. There is an alternative. On the first screen, choose Modify Existing User Attributes and clickNext. On the Modify Active Directory Users screen, there is an Advanced you click the Advanced button, the Custom LDAP Filter dialog box appears. In this dialog box, you type the LDAP filter that you want touse. In this example, you only want to list the groups that are mail-enabled. It would look like the , click OK. Select the OU or the domain where you want ADModify to search. If you want it to look in lower-level containers within theone you selected, make sure to select Traverse Subcontainers when Enumerating Users. Click Add to List, and then click Yes whenwarned about how long it could take. The objects that meet the criteria you specified should now appear in the right here, highlight the objects that you want to modify, and continue with the wizard.

译⾃:LDAP Query Basics链接:LDIFDE - Export / Import data from Active Directory - LDIFDE commands