ユーザー名のコマンドラインActive Directoryクエリ電子メールアドレス


17

Active Directory環境のWindows XPの場合-コマンドラインでユーザー名を指定すると、ADからユーザーのメールアドレスを照会する最も簡単な方法は何ですか。

(ツリー内の通常の場所を知っていると仮定します)。

(net user loginname / domain については知っていますが、メールアドレス要素を戻したいだけです。)


2
複雑なExchangeを使用している場合、「メール」属性は探しているメールアドレスではない可能性があることに注意してください。また、ユーザーオブジェクトの複数値属性「proxyAddresses」を確認する必要があります。
ライアンフィッシャー

回答:


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

それだけ見逃した... :)
TheCleaner 09年

1
わかりました-たぶん私ははっきりしていませんでした-しかし、あなたは十分に近いです-私が望んでいたと思うことは、dsquery user -samid "loginname" | dsget user -email
Hawkeye


5

このdsqueryのようなものが機能する可能性があります。

ユーザー名によるクエリメールdsquery.exe * -filter "(&(objectClass = user)(!(objectClass = computer)(sAMAccountName = username)))" | dsget user -email

最初に投稿を読み違え、メール名からユーザー名が欲しいと思った。それが私がこれを投稿した理由です。dsquery.exe * -filter "(&(objectClass = user)(!(objectClass = computer)(mail=user@domain.com)))" -attr username

作業中のいくつかのスクリプトと、csvde.exeの使用に関するhttp://www.petri.co.il/forums/showthread.php?t=18464の他のアイデアがあるこのサイトに基づいています



4

必要なメールがユーザープリンシパル名でもある場合は、次の方法で取得できます。

whoami /upn

ただし、これは現在のユーザーのメールを取得するためだけに機能し、質問が当初想定されていたユーザーを取得するためではありません。


cmdをターゲットユーザーとして実行することにより、この方法を使用しました。魔法のように働いた
ダニエル

また、これはUPNのみを返します。これは、特にADドメインが.localまたは登録済みパブリックドメインではなく類似のものである場合、ユーザーのデフォルトのパブリックメールアドレスと必ずしも同じではありません。
クレイグ

1
私の答えの最初の文はそれを言う@Craig ...
クリスピー

2

PowershellおよびQuestADアドオンパックをインストールします。それは次のようなものです:

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

2

LDAPを介してクエリを実行する簡単なVBScriptを作成できます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クエリ文字列に入力し、VBSファイルを実行して楽しんでください:)

LDAPを初めて使用する場合、LDAPクエリを記述するのは少し複雑になる可能性がありますユーザーへのLDAPパス(LDAP://の後に置く必要があるもの)を認識するために、Active Directoryをダウンロードできます。Microsoft Run エクスプローラーのエクスプローラー、ユーザーに移動し、[パス]テキストボックスに表示される内容を確認します。

私の場合、CN = [ユーザー名]、CN = Users、DC = [都市名]、DC = [会社名]、DC = comのようなものでした。


2

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();

2

欲しいものを手に入れるのに役立つこのスレッドを見つけました。ADユーザー属性を環境変数に取得します。このスクリプトは、ログインしているユーザーから必要なすべての属性を取得し、対応する環境変数を設定します。変数に接頭辞を付けましたが、これはオプションなので、変数名は「AD [属性名]」になります。属性は任意です。-attrの後に属性を追加または削除します。ただし、複数値属性にはあまり役立ちません。最後の(1)値は環境変数に入ります。

このスクリプトは現在の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でグローバル環境変数を取得するには、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

:EDIT:例2のset-statementの最後のスペース文字により、値が空のスペースで終了しました。修正するために削除しました。(Set %% A = %% B&setx ...)また、スクリプトが適切に機能するには少なくとも2つの属性をエクスポートする必要があることがわかりました。

遅い応答ですが、それがそこにいる誰かを助けることができるなら、私は幸せです。


1

スレッドスターターの平均値と一致するかどうかはわかりません。しかし、このスレッドを参照した後、すでに解決された既存の問題の解決策を見つけました。KNOWN MAIL ADDRESSに基づいてユーザーログインIDを検索しています。:)

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 ]には「完全なCNとパス」が含まれています。ファイル[ salesforce-uid-samid.txt ]には、「found SAMID」エイリアス「user login name」が含まれています。それはすべての人々です。改善のためのアイデアは大歓迎です。:)


-1

以下は他の目的で作成したバッチスクリプトですが、あまり問題なく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 e.g. "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


そこには多くの作業があり、スクリプトはおそらくそれが書かれた環境で役に立つでしょうが、IPの入力を必要とせず、アップされている他のはるかに短い答えよりも質問にどのように答えるのかわかりませんここで何年も(受け入れられたものの場合は7つ以上)。あなたからの訪問に感謝するより多くの最近の未回答の質問があります!
法律
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.