Jump to content

Extracting first + last & full username from AD


Recommended Posts

Hi People,

I would like to know if it is possible to extract information from the Active Directory and use the data for installation purposes?

At the moment i have a VBS script which extracts first name, last name, full name and username.

I know autoit can read the username with @UserName but that isn't enough :wacko:

'--- get the name of the current domain to target --- 
set oRoot = GetObject("LDAP://RootDSE") 
strDomain = oRoot.Get("defaultNamingContext") 
Set oDomain = GetObject("LDAP://" & oRoot.Get ("defaultNamingContext")) 

'--- get user logon name --- 
Set WshShell  = WScript.CreateObject("WScript.Shell") 
Set wEnv = WshShell.Environment("PROCESS") 
strUser = wEnv("USERNAME") 

'--- search for object in AD --- 
strLDAPQuery = "(&(objectCategory=person)(objectClass=user)(samaccountname=" & strUser & "))" 

Set oConnect = CreateObject("ADODB.Connection") 
oConnect.Provider = "ADsDSOObject" 
oConnect.Open "DS Query" 

Set command = CreateObject("ADODB.Command") 
Set command.ActiveConnection = oConnect 

strSQL = "SELECT cn, givenName, sn FROM 'LDAP://" & strDomain & "' " + _ 
"WHERE objectCategory ='person' AND objectClass='user' AND SAMaccountName='" & strUser & "'" 

set net = createObject("wscript.network") 

command.CommandText = strSQL 

'--- returns a recordset of matching objects --- 
set rs = command.execute 
Set oShell = WScript.CreateObject("WScript.Shell")

'--- this just spews the data, but you can modify to suit your needs --- 
While Not rs.EOF 

strHelenaam = rs.Fields("cn")
strVoornaam = rs.Fields ("givenName")
strAchternaam = rs.Fields("sn")
strUsername = net.username

rs.MoveNext 
Wend

Example: strHelenaam gives the full username like Rick Ruigrok.

Is it possible to have a autoit schript which does the same thing only better ? :D

This would come in usefull, so i can fill in the currect full username into installation scripts.

Thanks in advance

ps.. i didn't wrote the vbs script, just lightly adjusted it, it's properly full of useless data :D

i never learned vbs so i stopped deleting lines after it worked for the first time

Link to comment
Share on other sites

take a look at ObjGet() & ObjCreate() in the current beta.

Hi Wouter,

I just installed the Autoit Beta and checked the help file about ObjGet() & ObjCreate().

Only i have no clue where to begin now, the vbs script is already a mystery to me.

Could you give me a hand with it?

Greetings

Rick

Link to comment
Share on other sites

set oRoot = GetObject("LDAP://RootDSE")

would become like: $oRoot = ObjGet("LDAP://RootDSE")

Set oConnect = CreateObject("ADODB.Connection")

would be like: $oConnect = ObjCreate("ADODB.Connection")

'--- get user logon name ---
Set WshShell  = WScript.CreateObject("WScript.Shell")
Set wEnv = WshShell.Environment("PROCESS")
strUser = wEnv("USERNAME")

Would be like: $strUser = @username

And so on, Goodluck.

My UDF's:;mem stuff_Mem;ftp stuff_FTP ( OLD );inet stuff_INetGetSource ( OLD )_INetGetImage _INetBrowse ( Collection )_EncodeUrl_NetStat_Google;random stuff_iPixelSearch_DiceRoll

Link to comment
Share on other sites

Here's what I put in my scripts:

*edit: added constants, but check out the post link below to get the full picture

**Edit: Added the first name and last name code

***Edit: posted a more complete code to correct errors

#include <GUIConstants.au3>
#include <Misc.au3>
Const $ADS_NAME_INITTYPE_GC = 3
Const $ADS_NAME_TYPE_NT4 = 3
Const $ADS_NAME_TYPE_1779 = 1
; DNS domain name.
$oMyError = ObjEvent("AutoIt.Error", "ComError")
$objRootDSE = ObjGet("LDAP://RootDSE")
    $objTrans = ObjCreate("NameTranslate")
    $objTrans.Init ($ADS_NAME_INITTYPE_GC, "")
    $objTrans.Set ($ADS_NAME_TYPE_1779, @LogonDomain)
    $objTrans.Set ($ADS_NAME_TYPE_NT4, @LogonDomain & "\" & @UserName)
    $strUserDN = $objTrans.Get ($ADS_NAME_TYPE_1779)
    $UserObj = ObjGet("LDAP://" & $strUserDN)
DIM $firstname        = $UserObj.FirstName
DIM $lastname            = $UserObj.LastName
DIM $fullname            = $UserObj.FullName
DIM $department    = $UserObj.Department
DIM $streetaddress      = $UserObj.get("streetAddress")
DIM $city               = $UserObj.get("l")
DIM $state          = $UserObj.get("st")
DIM $zipcode                = $UserObj.PostalCodes
DIM $country                = $UserObj.get("c")
DIM $emailaddress         = $UserObj.EmailAddress
DIM $officenumber         = $UserObj.TelephoneNumber
DIM $mobilenumber         = $UserObj.TelephoneMobile
DIM $faxnumber        = $UserObj.FaxNumber
DIM $homeMDB                = $UserObj.get("homeMDB")
Dim $homeMDBtext            = StringSplit($homeMDB, ",")
Dim $mailboxname            = StringTrimLeft($homeMDBtext[4], 3)


MsgBox (0, "", "Test: "& $fullname)



$UserObj = ""
$oMyError = ObjEvent("AutoIt.Error", "")
;COM Error function
Func ComError()
    If IsObj($oMyError) Then
        $HexNumber = Hex($oMyError.number, 8)
        SetError($HexNumber)
    Else
        SetError(1)
    EndIf
    Return 0
EndFunc

Check out my post here, it has the full code

AD Helper

Edited by joshiieeii
Link to comment
Share on other sites

Hi joshiieeii,

Thanks for posting the script .. when Beta running it, it gives this error (like Wouter said):

..\active directory test.au3 (10) : ==> The requested action with this object has failed.: 
$objTrans.Set ($ADS_NAME_TYPE_1779, @LogonDomain) 
$objTrans.Set ($ADS_NAME_TYPE_1779, @LogonDomain)^ ERROR

But your script has more features than i dreamed of, if you good make it work it would be awesome.

Thanks,

Rick

Link to comment
Share on other sites

Hi joshiieeii,

Thanks for posting the script .. when Beta running it, it gives this error (like Wouter said):

..\active directory test.au3 (10) : ==> The requested action with this object has failed.: 
$objTrans.Set ($ADS_NAME_TYPE_1779, @LogonDomain) 
$objTrans.Set ($ADS_NAME_TYPE_1779, @LogonDomain)^ ERROR

But your script has more features than i dreamed of, if you good make it work it would be awesome.

Thanks,

Rick

Sorry about that, I was posting kinda quickly and left some stuff out, I tested it before I posted the above corrected code. Try it now, it should work fine.
Link to comment
Share on other sites

Arghhhh .. almost !

I'm getting this error.

Just to be sure, i uninstalled al my autoit.

Downloaded autoit + latest beta + newest Scite but no luck

>"C:\Program Files\AutoIt3\SciTe\CompileAU3\CompileAU3.exe" /run /beta /ErrorStdOut /in "ad test.au3" /autoit3dir "C:\Program Files\AutoIt3\beta" /UserParams   
>Running AU3Check...C:\Program Files\AutoIt3\SciTe\Defs\Unstable\Au3Check\au3check.dat
..\ad test.au3(47,12) : ERROR: syntax error
    Return 0
~~~~~~~~~~~^
..\ad test.au3 - 1 error(s), 0 warning(s)

Hopefully you understand the error?

ps.. why do define $UserObj & $oMyError 2 times?

When i removed the code after MsgBox i got this error

..\ad test.au3 (11) : ==> The requested action with this object has failed.: 
$objTrans.Set ($ADS_NAME_TYPE_1779, @LogonDomain) 
$objTrans.Set ($ADS_NAME_TYPE_1779, @LogonDomain)^ ERROR
+>AutoIT3.exe ended.rc:0
Link to comment
Share on other sites

  • Developers

Arghhhh .. almost !

I'm getting this error.

Just to be sure, i uninstalled al my autoit.

Downloaded autoit + latest beta + newest Scite but no luck

>"C:\Program Files\AutoIt3\SciTe\CompileAU3\CompileAU3.exe" /run /beta /ErrorStdOut /in "ad test.au3" /autoit3dir "C:\Program Files\AutoIt3\beta" /UserParams   
>Running AU3Check...C:\Program Files\AutoIt3\SciTe\Defs\Unstable\Au3Check\au3check.dat
..\ad test.au3(47,12) : ERROR: syntax error
    Return 0
~~~~~~~~~~~^
..\ad test.au3 - 1 error(s), 0 warning(s)
You are for sure not running the latest SciTE4AutoIt3 version.!!

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...