Ruigerock Posted July 13, 2006 Posted July 13, 2006 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 expandcollapse popup'--- 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 ? 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 i never learned vbs so i stopped deleting lines after it worked for the first time
w0uter Posted July 13, 2006 Posted July 13, 2006 take a look at ObjGet() & ObjCreate() in the current beta. My UDF's:;mem stuff_Mem;ftp stuff_FTP ( OLD );inet stuff_INetGetSource ( OLD )_INetGetImage _INetBrowse ( Collection )_EncodeUrl_NetStat_Google;random stuff_iPixelSearch_DiceRoll
Ruigerock Posted July 13, 2006 Author Posted July 13, 2006 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?GreetingsRick
w0uter Posted July 13, 2006 Posted July 13, 2006 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
joshiieeii Posted July 13, 2006 Posted July 13, 2006 (edited) 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 errorsexpandcollapse popup#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 EndFuncCheck out my post here, it has the full codeAD Helper Edited July 14, 2006 by joshiieeii Projects:Vista Gui ImageX Deployment Tool - CompletedActive Directory Helper - CompletedGlobalized Outlook and OWA Signature Project - Completed
w0uter Posted July 13, 2006 Posted July 13, 2006 it would be more usefull if you also posted the constants for ex. $ADS_NAME_INITTYPE_GC My UDF's:;mem stuff_Mem;ftp stuff_FTP ( OLD );inet stuff_INetGetSource ( OLD )_INetGetImage _INetBrowse ( Collection )_EncodeUrl_NetStat_Google;random stuff_iPixelSearch_DiceRoll
Ruigerock Posted July 13, 2006 Author Posted July 13, 2006 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
joshiieeii Posted July 13, 2006 Posted July 13, 2006 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. Projects:Vista Gui ImageX Deployment Tool - CompletedActive Directory Helper - CompletedGlobalized Outlook and OWA Signature Project - Completed
Ruigerock Posted July 14, 2006 Author Posted July 14, 2006 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
Developers Jos Posted July 14, 2006 Developers Posted July 14, 2006 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.Â
MHz Posted July 14, 2006 Posted July 14, 2006 Hopefully you understand the error?The posted script above is missing EndFunc at the end. Hope you noticed that.
Ruigerock Posted July 14, 2006 Author Posted July 14, 2006 Ehhhhh :"> Who removed the EndFunc line on my computer Thanks Mhz! for pointing out the error and thanks joshiieeii for the great script Till my next post
joshiieeii Posted July 14, 2006 Posted July 14, 2006 What missing endfunc?? Projects:Vista Gui ImageX Deployment Tool - CompletedActive Directory Helper - CompletedGlobalized Outlook and OWA Signature Project - Completed
Ruigerock Posted July 17, 2006 Author Posted July 17, 2006 During my hasty copy,paste i accidently didn't copy the line Endfunc
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now