命令行活动目录查询用户名的电子邮件地址

活动目录环境中的Windows XP中 – 在命令行中给出用户名的情况下,从AD中查询用户的电子邮件地址最简单的方法是什么?

(假设我知道它在树中的正常保存位置)。

(我知道关于净用户login名 /域名,但我只是想要的电子邮件地址元素回来。)

dsquery user -name "user name"|dsget user -samid -email -display 

dsquery用户名“名字姓氏”| dsget用户 – 电子邮件

像这样的dsquery可能工作。

通过用户名查询电子邮件dsquery.exe * -filter“(&(objectClass = user)(!(objectClass = computer)(sAMAccountName = username)))”| dsget用户 – 电子邮件

我误解了post,并认为你想从电子邮件名称的用户名。 这就是为什么我发布这一个。 dsquery.exe * -filter“(&(objectClass = user)(!(objectClass = computer)([email protected])))”-attr username

基于一些脚本在工作和这个网站有一些其他的想法http://www.petri.co.il/forums/showthread.php?t=18464有关使用csvde.exe

adfind -sc u:“用户名”邮件

如果你想要的电子邮件也是用户主体名称,你可以得到它

 whoami /upn 

但是,这只能用于获取当前用户的电子邮件,而不是任何用户原来的问题。

安装Powershell和QuestAD插件包。 然后是这样的:

 connect-qadservice (get-qaduser 'bobsusername').emailAddress 

您可以编写简单的VBScript来查询通过LDAP创build一个VBS扩展名的文件

放入这样的东西

 On Error Resume Next Set objUser = GetObject _ ("LDAP://CN=USER NAME,DC=DOMAIN_NAME,DC=com") objUser.GetInfo strMail = objUser.Get("mail") WScript.echo "mail: " & strMail 

将正确的用户名放入LDAP查询string,运行VBS文件并享受:)

如果这是您第一次使用LDAP,那么编写LDAP查询可能会有点复杂为了识别用户的LDAPpath(即,您需要在LDAP://之后放置什么),可以下载Active Directory从Microsoft运行资源pipe理器的资源pipe理器 ,导航到该用户,看看它显示在path文本框中

在我的情况下,它是像CN = [用户名],CN =用户,DC = [city_name],DC = [company_name],DC = com,

LINQ的一切 ! 为了方便:

1)在LinqPad的查询属性中,添加对System.DirectoryServices.AccountManagement.dll的引用。 2)额外命名空间导入:System.DirectoryServices.AccountManagement

 using(PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "MyDomain)) using(UserPrincipal usr = UserPrincipal.FindByIdentity(ctx, IdentityType.SamAccountName, "MyUserID")) usr.Dump(); 

find这个线程,帮助我得到我想要的。 获取任何AD用户属性到环境variables。 该脚本从login用户获取所有需要的属性并设置相应的环境variables。 我在variables的前面加上了前缀,但这是可选的,所以variables名变成“AD [属性名称]”。 您可以select属性,只需在-attr之后添加或删除属性即可。 虽然对于多值属性不是很有用。 最后一个值进入环境variables。

该脚本是当前cmd.exe的本地文件

 for /F "tokens=1,* delims=: " %%A in ('dsquery * domainroot -l -filter "(&(objectCategory=Person)(objectClass=User)(sAMAccountName=%USERNAME%))" -attr adminDescription employeetype company department physicalDeliveryOfficeName street title mail') do set AD%%A=%%B 

要在Windows中获取全局环境variables,我们可以在Windows 7中使用“setx”(对于loginscript,也许…但速度要慢得多)。

 for /F "tokens=1,* delims=: " %%A in ('dsquery * domainroot -l -filter "(&(objectCategory=Person)(objectClass=User)(sAMAccountName=%USERNAME%))" -attr adminDescription employeetype company department physicalDeliveryOfficeName street title mail') do set AD%%A=%%B& setx AD%%A "%%~B" > NUL 

:编辑:例2中的set语句结束时的空格字符导致值以空格结束。 删除它来纠正。 (设置%% A = %% B&setx …)还发现你必须导出至less两个属性脚本才能正常工作。

迟到的回应,但如果它能帮助任何人在那里我很高兴。

我不知道这是匹配的线程启动意思或不。 但是我只是在浏览这个线程之后find了解决已经解决的问题。 根据已知的邮件地址查找用户loginID 。 🙂

 C:\Users\MrCMD>for /f "delims=" %u in ('type salesforce-uid-mail-address.txt') do @dsquery.exe * -filter "(&(objectClass=user)(!(objectClass=computer)(mail=%u)))">>"salesforce-uid-cn.txt" ┌─────────────────────────────────────┐ │ Executed Wed 07/10/2013 8:29:55.05 │ As [MrCMD] └─────────────────────────────────────┘ C:\Users\MrCMD>for /f "delims=" %u in ('type salesforce-uid-cn.txt') do @dsget.exe user %u -samid -l|find "samid" /i>>"salesforce-uid-samid.txt" ┌─────────────────────────────────────┐ │ Executed Wed 07/10/2013 8:31:56.40 │ As [MrCMD] └─────────────────────────────────────┘ 

文件[ salesforce-uid-mail-address.txt ]包含电子邮件地址列表。 文件[ salesforce-uid-cn.txt ]包含“带path的完整CN”。 并且文件[ salesforce-uid-samid.txt ]包含“find的SAMID”别名“用户login名”。 这就是所有人。 任何改进的想法是受欢迎的。 🙂

以下是我为其他内容编写的批处理脚本,但可用于在CN中查找电子邮件属性,而不会出现太多问题。


 :: CN Attribute Lookup Tool :: Written by Turbo Dog :: :: -- Purpose: A simple lookup batch script using the ldifde command. :: :: -- It was written to translate a hashed CN with it's more human readable attribute. :: :: -- Multi environment version :: :: -- anything in <brackets> is something you need to fill eg "set servip=10.0.0.5" :: :: -- Generic ID Version: :: -- <ID with read access to CN and it's target attribute> will have to be made, :: -- careful with this as it'll need to be a generic account with a non-expiring password :: :: :BEGIN @echo off :: - Grey background with black font - color 70 :RESTART cls :: Environment choice :: default choice (1 preproduction 2 test 3 production) set ENVCH=3 setlocal enableextensions enabledelayedexpansion echo ÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜ echo Û CN Attribute Lookup Tool V1.0 Û echo ßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßß echo. echo. echo 1. PreProduction echo 2. Test echo 3. Production echo. echo Please enter the number of the environment you wish to search and press enter or type q and press enter to quit: (3) set /p ENVCH= IF %ENVCH%==1 GOTO PPRODU IF %ENVCH%==2 GOTO TESTEN IF %ENVCH%==3 GOTO PRODUC IF %ENVCH%==q GOTO FINISH IF %ENVCH%==Q GOTO FINISH IF %ENVCH%==[%1]==[] GOTO FINISH :: PreProduction settings :PPRODU set envtype=PreProduction set servip=<IP or hostname of preproduction AD server> set servpt=<port number of preproduction AD server> GOTO GATHER :: Test settings :TESTEN set envtype=Test set servip=<IP or hostname of test AD server> set servpt=<port number of test AD server> GOTO GATHER :: Production settings :PRODUC set envtype=Production set servip=<IP or hostname of production AD server> set servpt=<port number of production AD server> GOTO GATHER :GATHER :: - Gather information for job - cls :: - Grey background with black font - color 70 echo ÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜ echo Û CN Attribute Lookup Tool V1.0 Û echo ßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßß echo Environment - !envtype! echo. echo Copy and paste the CN and press enter (or type q and enter to quit): set /p resource="" IF "%resource%"=="q" GOTO FINISH IF "%resource%"=="Q" GOTO FINISH set resourcein=!resource! cls :: - Process action - ldifde -s %servip% -t %servpt% -a <ID with read access to CN and it's target attribute> <password for ID> -d "<the container that holds the CN's to search through cn=Container,ou=DOMAIN,o=ORG>" -f output.txt -l "<target attribute to read>" -r "(cn=%resource%)" :: pause :: only have this line active (start colons missing) during troubleshooting to see if anything is written to the output.txt file cls :: - Extraction of the attribute from the output file - set resource= for /f "delims=" %%a in (output.txt) do ( set line=%%a if "x!line:~0,22!"=="<target attribute to read>: " ( set resource="!line:~22!" ) ) :: - Check to see if it has worked? - IF NOT %resource%==[%1]==[] GOTO RESULT :: Resource value has something then send to the result step otherwise default to error :: - The error message - :: - Black background with red font (amiga guru looking error) - color 0C cls echo ÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜ echo Û CN Attribute Lookup Tool V1.0 Û echo ßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßß echo Environment - !envtype! echo. echo Sorry, it appears you've entered an CN that's either not for echo !envtype!, has not got anything in it's attribute or has been copied incorrectly! echo. echo Press any key to retry. :: - Cleanup errored output file - del output.txt pause >nul GOTO GATHER :: - The result - :RESULT :: - Copy result to clipboard - echo|set/p=%resource%|clip :: - Grey background with black font - color 70 cls echo ÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜ echo Û CN Attribute Lookup Tool V1.0 Û echo ßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßß echo Environment - !envtype! echo. echo. Your submission was: "!resourcein!" echo The attribute is: !resource! echo. echo !resource! has been copied to the clipboard and is ready to paste. echo. :: - Cleanup output file - del output.txt :: - default to exit - set fn=n echo Do you have additional resources to look up (y for yes, n for no and c to change environment)? (n): set /p fn="" IF %fn%==y GOTO GATHER IF %fn%==Y GOTO GATHER IF %fn%==c GOTO RESTART IF %fn%==C GOTO RESTART :FINISH echo. echo Thank you, press any key to exit. pause >nul :: - Set CMD Shell colours back to default - color 07 :: - The end - @echo off :EOF