1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ADからユーザー情報を抽出するVBS

Posted at

これを使えばAD内のユーザーがどこに所属しているかがわかるよってメモ

Search.vbs
Option Explicit
Const SearchUser = "testusera"
Dim baseDN, objRootDSE
Dim objConnection, objCommand, strCommandText
Dim objRecordSet, strUserDN
baseDN = ""

' ベースDNの取得
On Error Resume Next
Set objRootDSE = GetObject("LDAP://rootDSE")
If Err.Number <> 0 Then
  WScript.Echo "ドメイン接続に失敗しました。終了します。"
  WScript.Quit
Else
  baseDN = objRootDSE.Get("defaultNamingContext")
End If
On Error Goto 0

' DCに接続して検索
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
objCommand.ActiveConnection = objConnection
strCommandText = "<LDAP://" & baseDN & ">;" & _
	"(&(objectClass=user)(sAMAccountName=" & SearchUser & "));" & _
	"distinguishedName;" & _
	"subtree"
objCommand.CommandText = strCommandText
Set objRecordSet = objCommand.Execute

' 検索結果を表示
If objRecordset.EOF Then
  strUserDN = "ログオンアカウント " & SearchUser & " は見つかりませんでした。"
Else
  strUserDN = objRecordset.Fields("distinguishedName")
End If
WScript.Echo strUserDN
objConnection.Close
Set objCommand = Nothing
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?