Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista - find a user in AD

Reply
 
Old 11-30-2008   #1 (permalink)
Gunna


 
 

find a user in AD

HI all, im not much of a scripting person so please help. Im trying to write
a simple enough script to ask for a logon name, then search through AD and
pull out some info on that user like email address etc. I've tried a few
things and just cant get it to work. Here is what ive got so far:

Const ADS_SCOPE_SUBTREE = 2

strNTName=InputBox("Please enter a username to search")

Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = ("ADsDSOObject")
objConnection.Open "Active Directory Provider"
objCommand.ActiveConnection = objConnection
objCommand.CommandText = "SELECT samAccountName (same line)FROM
'LDAP://dc=domainname,dc=com' WHERE samAccountName = "'strNTName'"
objCommand.Properties("SearchScope") = ADS_SCOPE_SUBTREE
Set objRecordSet = objCommand.Execute

My problem in in the Select statement putting the users input into the
statement. it doesnt like it. Can anyone help?

My System SpecsSystem Spec
Old 11-30-2008   #2 (permalink)
Richard Mueller [MVP]


 
 

Re: find a user in AD


"Gunna" <Gunna@xxxxxx> wrote in message
news:FA79AA37-4836-4FF0-A442-B94143C5CF20@xxxxxx
Quote:

> HI all, im not much of a scripting person so please help. Im trying to
> write
> a simple enough script to ask for a logon name, then search through AD and
> pull out some info on that user like email address etc. I've tried a few
> things and just cant get it to work. Here is what ive got so far:
>
> Const ADS_SCOPE_SUBTREE = 2
>
> strNTName=InputBox("Please enter a username to search")
>
> Set objConnection = CreateObject("ADODB.Connection")
> Set objCommand = CreateObject("ADODB.Command")
> objConnection.Provider = ("ADsDSOObject")
> objConnection.Open "Active Directory Provider"
> objCommand.ActiveConnection = objConnection
> objCommand.CommandText = "SELECT samAccountName (same line)FROM
> 'LDAP://dc=domainname,dc=com' WHERE samAccountName = "'strNTName'"
> objCommand.Properties("SearchScope") = ADS_SCOPE_SUBTREE
> Set objRecordSet = objCommand.Execute
>
> My problem in in the Select statement putting the users input into the
> statement. it doesnt like it. Can anyone help?
Try:

objCommand.CommandText = "SELECT sAMAccountName " _
& "FROM 'LDAP://dc=domainname,dc=com' " _
& "WHERE sAMAccountName = '" & strNTName & "'"

Also, it would be more efficient to use the NameTranslate object. See this
link:

http://www.rlmueller.net/NameTranslateFAQ.htm

--
Richard Mueller
MVP Directory Services
Hilltop Lab - http://www.rlmueller.net
--


My System SpecsSystem Spec
Old 12-03-2008   #3 (permalink)
Reventlov


 
 

Re: find a user in AD

Il giorno Sun, 30 Nov 2008 20:07:01 -0800, =?Utf-8?B?R3VubmE=?=
<Gunna@xxxxxx> ha scritto:
Quote:

>HI all, im not much of a scripting person so please help. Im trying to write
>a simple enough script to ask for a logon name, then search through AD and
>pull out some info on that user like email address etc. I've tried a few
>things and just cant get it to work. Here is what ive got so far:
This uses adsi.

'************************************************
' File: Visualizza dati di un utente.vbs
' Autore: Giovanni Cenati reventlov@xxxxxx
'
'************************************************
Set WshShell = WScript.CreateObject("WScript.Shell")

' Ottiene il nome del dominio e dell'utente
strDomain = WshShell.ExpandEnvironmentStrings("%USERDOMAIN%")
strUser= WshShell.ExpandEnvironmentStrings("%USERNAME%")

' Chiede la userid da cercare (default: utente corrente)
strUser = inputbox ("Quale utente vuoi cercare?", "Visualizza Dati Utente",strUser)
if struser ="" then wscript.quit 'Se premuto annulla

' Si collega al server e richiede l'utente
Set objDomain = GetObject("WinNT://" & strDomain)
Set objUser = objDomain.GetObject("user", strUser)

' Estrae i dati dell'utente e li mette nella variabile Dati.
Dati= "Userid: "& vbtab & objUser.Name & vbcrlf
Dati= Dati & "Nome: " & Vbtab & objUser.FullName & vbcrlf
Dati= Dati & "Descriz: " & vbtab & objUser.Description & vbcrlf
Dati= Dati & "Ult. Accesso: " & vbtab & objUser.LastLogin & vbcrlf
Dati= Dati & vbcrlf & "Gruppi di appartenenza:" & vbcrlf & vbcrlf

' Estrae i gruppi di appartenenza dell'utente
For Each objGroup In objUser.Groups
Dati= Dati & objGroup.Name & vbtab
next

' Output dati.
msgbox Dati,, "Dati dell'utente " & strUser & " (Dominio " & strDomain & ")"
--
Giovanni Cenati (Bergamo, Italy)
Write to "Reventlov" at katamail com
http://digilander.libero.it/Cenati (Esempi e programmi in VbScript)
--
My System SpecsSystem Spec
Old 12-04-2008   #4 (permalink)
Richard Mueller [MVP]


 
 

Re: find a user in AD


"Richard Mueller [MVP]" <rlmueller-nospam@xxxxxx> wrote in
message news:uQD0Ov2UJHA.4916@xxxxxx
Quote:

>
> "Gunna" <Gunna@xxxxxx> wrote in message
> news:FA79AA37-4836-4FF0-A442-B94143C5CF20@xxxxxx
Quote:

>> HI all, im not much of a scripting person so please help. Im trying to
>> write
>> a simple enough script to ask for a logon name, then search through AD
>> and
>> pull out some info on that user like email address etc. I've tried a few
>> things and just cant get it to work. Here is what ive got so far:
>>
>> Const ADS_SCOPE_SUBTREE = 2
>>
>> strNTName=InputBox("Please enter a username to search")
>>
>> Set objConnection = CreateObject("ADODB.Connection")
>> Set objCommand = CreateObject("ADODB.Command")
>> objConnection.Provider = ("ADsDSOObject")
>> objConnection.Open "Active Directory Provider"
>> objCommand.ActiveConnection = objConnection
>> objCommand.CommandText = "SELECT samAccountName (same line)FROM
>> 'LDAP://dc=domainname,dc=com' WHERE samAccountName = "'strNTName'"
>> objCommand.Properties("SearchScope") = ADS_SCOPE_SUBTREE
>> Set objRecordSet = objCommand.Execute
>>
>> My problem in in the Select statement putting the users input into the
>> statement. it doesnt like it. Can anyone help?
>
> Try:
>
> objCommand.CommandText = "SELECT sAMAccountName " _
> & "FROM 'LDAP://dc=domainname,dc=com' " _
> & "WHERE sAMAccountName = '" & strNTName & "'"
>
> Also, it would be more efficient to use the NameTranslate object. See this
> link:
>
> http://www.rlmueller.net/NameTranslateFAQ.htm
>
> --
> Richard Mueller
> MVP Directory Services
> Hilltop Lab - http://www.rlmueller.net
> --
>
>
A program that uses NameTranslate could be similar to below:
=============
' Constants for the NameTranslate object.
Const ADS_NAME_INITTYPE_GC = 3
Const ADS_NAME_TYPE_NT4 = 3
Const ADS_NAME_TYPE_1779 = 1

strNTName=InputBox("Please enter a username to search")

' Determine DNS name of domain from RootDSE.
Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("defaultNamingContext")

' Use the NameTranslate object to find the NetBIOS domain name from the
' DNS domain name.
Set objTrans = CreateObject("NameTranslate")

' Initialize NameTranslate by locating the Global Catalog.
objTrans.Init ADS_NAME_INITTYPE_GC, ""
' Use the Set method to specify the NT format of the object name.
objTrans.Set ADS_NAME_TYPE_1779, strDNSDomain
' Use the Get method to retrieve the RPC 1779 Distinguished Name.
strNetBIOSDomain = objTrans.Get(ADS_NAME_TYPE_NT4)
' Remove trailing backslash.
strNetBIOSDomain = Left(strNetBIOSDomain, Len(strNetBIOSDomain) - 1)

' Use the Set method to specify the NT format of the object name.
objTrans.Set ADS_NAME_TYPE_NT4, strNetBIOSDomain & "\" & strNTName

' Use the Get method to retrieve the RPC 1779 Distinguished Name.
strUserDN = objTrans.Get(ADS_NAME_TYPE_1779)

' Bind to the user object in Active Directory with the LDAP provider.
Set objUser = GetObject("LDAP://" & strUserDN)
==========
You could hard code the NetBIOS name of the domain, but the above retrieves
it programmatically. In my tests, NameTranslate is faster than using ADO to
search.

--
Richard Mueller
MVP Directory Services
Hilltop Lab - http://www.rlmueller.net
--


My System SpecsSystem Spec
Old 12-17-2008   #5 (permalink)
Gunna


 
 

Re: find a user in AD

Thanks that works but how do I list the users Distinguished Name?

"Reventlov" wrote:
Quote:

> Il giorno Sun, 30 Nov 2008 20:07:01 -0800, =?Utf-8?B?R3VubmE=?=
> <Gunna@xxxxxx> ha scritto:
Quote:

> >HI all, im not much of a scripting person so please help. Im trying to write
> >a simple enough script to ask for a logon name, then search through AD and
> >pull out some info on that user like email address etc. I've tried a few
> >things and just cant get it to work. Here is what ive got so far:
>
> This uses adsi.
>
> '************************************************
> ' File: Visualizza dati di un utente.vbs
> ' Autore: Giovanni Cenati reventlov@xxxxxx
> '
> '************************************************
> Set WshShell = WScript.CreateObject("WScript.Shell")
>
> ' Ottiene il nome del dominio e dell'utente
> strDomain = WshShell.ExpandEnvironmentStrings("%USERDOMAIN%")
> strUser= WshShell.ExpandEnvironmentStrings("%USERNAME%")
>
> ' Chiede la userid da cercare (default: utente corrente)
> strUser = inputbox ("Quale utente vuoi cercare?", "Visualizza Dati Utente",strUser)
> if struser ="" then wscript.quit 'Se premuto annulla
>
> ' Si collega al server e richiede l'utente
> Set objDomain = GetObject("WinNT://" & strDomain)
> Set objUser = objDomain.GetObject("user", strUser)
>
> ' Estrae i dati dell'utente e li mette nella variabile Dati.
> Dati= "Userid: "& vbtab & objUser.Name & vbcrlf
> Dati= Dati & "Nome: " & Vbtab & objUser.FullName & vbcrlf
> Dati= Dati & "Descriz: " & vbtab & objUser.Description & vbcrlf
> Dati= Dati & "Ult. Accesso: " & vbtab & objUser.LastLogin & vbcrlf
> Dati= Dati & vbcrlf & "Gruppi di appartenenza:" & vbcrlf & vbcrlf
>
> ' Estrae i gruppi di appartenenza dell'utente
> For Each objGroup In objUser.Groups
> Dati= Dati & objGroup.Name & vbtab
> next
>
> ' Output dati.
> msgbox Dati,, "Dati dell'utente " & strUser & " (Dominio " & strDomain & ")"
> --
> Giovanni Cenati (Bergamo, Italy)
> Write to "Reventlov" at katamail com
> http://digilander.libero.it/Cenati (Esempi e programmi in VbScript)
> --
>
My System SpecsSystem Spec
Old 12-17-2008   #6 (permalink)
Richard Mueller [MVP]


 
 

Re: find a user in AD

Since you have the user object reference objUser, you can retrieve the
Distinguished Name with:

objUser.distinguishedName

Using the syntax from the code supplied:

Dati= Dati & "DN: " & Vbtab & objUser.distinguishedName & vbcrlf

--
Richard Mueller
MVP Directory Services
Hilltop Lab - http://www.rlmueller.net
--

"Gunna" <Gunna@xxxxxx> wrote in message
news:6E0A12CD-5E73-4879-929B-B780E5AB2AAC@xxxxxx
Quote:

> Thanks that works but how do I list the users Distinguished Name?
>
> "Reventlov" wrote:
>
Quote:

>> Il giorno Sun, 30 Nov 2008 20:07:01 -0800, =?Utf-8?B?R3VubmE=?=
>> <Gunna@xxxxxx> ha scritto:
Quote:

>> >HI all, im not much of a scripting person so please help. Im trying to
>> >write
>> >a simple enough script to ask for a logon name, then search through AD
>> >and
>> >pull out some info on that user like email address etc. I've tried a
>> >few
>> >things and just cant get it to work. Here is what ive got so far:
>>
>> This uses adsi.
>>
>> '************************************************
>> ' File: Visualizza dati di un utente.vbs
>> ' Autore: Giovanni Cenati reventlov@xxxxxx
>> '
>> '************************************************
>> Set WshShell = WScript.CreateObject("WScript.Shell")
>>
>> ' Ottiene il nome del dominio e dell'utente
>> strDomain = WshShell.ExpandEnvironmentStrings("%USERDOMAIN%")
>> strUser= WshShell.ExpandEnvironmentStrings("%USERNAME%")
>>
>> ' Chiede la userid da cercare (default: utente corrente)
>> strUser = inputbox ("Quale utente vuoi cercare?", "Visualizza Dati
>> Utente",strUser)
>> if struser ="" then wscript.quit 'Se premuto annulla
>>
>> ' Si collega al server e richiede l'utente
>> Set objDomain = GetObject("WinNT://" & strDomain)
>> Set objUser = objDomain.GetObject("user", strUser)
>>
>> ' Estrae i dati dell'utente e li mette nella variabile Dati.
>> Dati= "Userid: "& vbtab & objUser.Name & vbcrlf
>> Dati= Dati & "Nome: " & Vbtab & objUser.FullName & vbcrlf
>> Dati= Dati & "Descriz: " & vbtab & objUser.Description & vbcrlf
>> Dati= Dati & "Ult. Accesso: " & vbtab & objUser.LastLogin & vbcrlf
>> Dati= Dati & vbcrlf & "Gruppi di appartenenza:" & vbcrlf & vbcrlf
>>
>> ' Estrae i gruppi di appartenenza dell'utente
>> For Each objGroup In objUser.Groups
>> Dati= Dati & objGroup.Name & vbtab
>> next
>>
>> ' Output dati.
>> msgbox Dati,, "Dati dell'utente " & strUser & " (Dominio " & strDomain &
>> ")"
>> --
>> Giovanni Cenati (Bergamo, Italy)
>> Write to "Reventlov" at katamail com
>> http://digilander.libero.it/Cenati (Esempi e programmi in VbScript)
>> --
>>

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Script to find all user and what PC they are logged into VB Script
can't find user after importing xp backup Vista file management
script to find a certain info about a user PowerShell
get-qaduser to find disabled user PowerShell
Find out where the user looged into Vista General


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46