Aen Posted December 11, 2008 Posted December 11, 2008 Writing a script that basically does this:1. Ask user for a username2. Ask user for employee number3. script verifies if it is real username - if it is, continues - if not, it stops script4. (step i need help with) - Query AD through LDAP and pull DN5. Add employee number to 2 different fields in AD (employeeID, employeeNumber)Real simple script.. but having some issues getting the DN from AD.*DN = distinguishedNameHere is what i got: Set objFile = CreateObject("Scripting.FileSystemObject")Set objNetName = CreateObject("WScript.NetWork") DIM strEmpIDName = GetUserName()EmployeeID = GetEmployeeID()UserExists = CheckUser(Name)If UserExists = TRUE thenmsgbox strEmpIDmsgbox NameCall LoadEmployeeIDElseMsgbox "User does not exist, try again."wscript.quit(0)End If'===========================Function LoadEmployeeID()Set objUser = GetObjectobjUser.Put "employeeNumber", strEmpID'objUser.Put "employeeID", EmployeeIDobjUser.SetInfoEnd Function'===========================Function GetEmployeeID()EmployeeID = Inputbox ("Please enter in 5 digit employee number")strEmpID = EmployeeIDEnd Function'=======================================Function GetUserName()i = 0UserName = InputBox ("Ex. Firstname.LastName ", " Please Enter User Name")Do While Instr(UserName, ".") = 0 and i <= 5Username = InputBox ("Ex. Firstname.LastName ", " Please Enter User Name")i=i+1LoopIf Instr(UserName, ".") = 0 ThenWScript.Quit(0)ElseGetUserName = Trim(UserName)End IfEnd Function'===========================================Function CheckUser(strUserName)dtStart = TimeValue(Now())Set objConnection = CreateObject("ADODB.Connection")objConnection.Open "Provider=ADsDSOObject;"Set objCommand = CreateObject("ADODB.Command")objCommand.ActiveConnection = objConnectionobjCommand.CommandText = _ "<LDAP:/*removed*t>;(&(objectCategory=User)" & _ "(samAccountName=" & strUserName & "));samAccountName;distinguishedName;subtree"Set objRecordSet = objCommand.ExecuteIf objRecordset.RecordCount = 0 Then 'WScript.Echo "sAMAccountName: " & strUserName & " does not exist." CheckUser = FALSEElse objRecordset.MoveFirst Do while Not objRecordset.EOF wscript.echo objRecordset("samAccountName") & " | " & objRecordset("distinguishedName") objRecordset.MoveNext Loop 'WScript.Echo strUserName & " exists." CheckUser = TRUEEnd IfobjConnection.CloseEnd Function'====================================================Can anyone please give some advice or code that can help?I think if i can just pull the DN from LDAP query and put that into a variable..i can finish the rest.
coleoptere2007 Posted December 12, 2008 Posted December 12, 2008 Hi, I dont know vbscript but you can get more info with the use of a soft like LDP.exe or ldap browser.But by experience when you make a query you must first indicate the base DN, domain and account to connect. Be carefull the base DN is different if you query a Windows AD, Netware, Lotus Domino. try first to query a windows AD and you'll see the different objects and atttributes.Hope this will help you.
Aen Posted December 12, 2008 Author Posted December 12, 2008 Hi, I dont know vbscript but you can get more info with the use of a soft like LDP.exe or ldap browser.But by experience when you make a query you must first indicate the base DN, domain and account to connect. Be carefull the base DN is different if you query a Windows AD, Netware, Lotus Domino. try first to query a windows AD and you'll see the different objects and atttributes.Hope this will help you.i can find the DN by going through ADSIedit.msc. But, i am making a script that when a user will enter in a username and employee number, it will change the custom employee ID field. We dont really want to give them access to ADSIedit because it can be very dangerous. We are only using Windows AD.
IcemanND Posted December 12, 2008 Posted December 12, 2008 does your script return anything? will it return info if you query for only one item, instead of both samAccountName and distinguishedName?
Aen Posted December 16, 2008 Author Posted December 16, 2008 does your script return anything? will it return info if you query for only one item, instead of both samAccountName and distinguishedName?I was going to create a new function and stop the query on both of those..i was just fooling around.Really just need to figure out how to pull that. I am surprised no one else has really done that before..i couldnt find any code snippets.
sbuehler Posted February 3, 2009 Posted February 3, 2009 Did you ever find out how to pull the dn?I'm trying with VBA, I'm getting all the fields I want but dn comes back blank.
realslimshrimpy Posted February 25, 2009 Posted February 25, 2009 Did you ever find out how to pull the dn?I'm trying with VBA, I'm getting all the fields I want but dn comes back blank.you can check this code out...maybe it can helpoption explicitdim objRootDSE, strDNSDomain, user, username, strAD, descriptionSet objRootDSE = GetObject("LDAP://RootDSE")strDNSDomain = objRootDSE.Get("DefaultNamingContext")username = inputbox ("Please Enter user name")strAD = "LDAP://cn=" & username & ",cn=users, " & strDNSDomainset user = getobject(strAD)description = inputbox ("please enter the description you want to change")user.put "description", descriptionuser.setinfowscript.echo "All Done"
realslimshrimpy Posted February 25, 2009 Posted February 25, 2009 Did you ever find out how to pull the dn?I'm trying with VBA, I'm getting all the fields I want but dn comes back blank.you can check this code out...maybe it can helpoption explicitdim objRootDSE, strDNSDomain, user, username, strAD, descriptionSet objRootDSE = GetObject("LDAP://RootDSE")strDNSDomain = objRootDSE.Get("DefaultNamingContext")username = inputbox ("Please Enter user name")strAD = "LDAP://cn=" & username & ",cn=users, " & strDNSDomainset user = getobject(strAD)description = inputbox ("please enter the description you want to change")user.put "description", descriptionuser.setinfowscript.echo "All Done"oroption explicitdim cmd, cn, rs, objRoot, objFSO, objCSV, qconst FileName ="domaincomputers.csv"set cmd = createobject("ADODB.Command")set cn = createobject("ADODB.Connection")set rs = createobject("ADODB.Recordset")cn.open "Provider=ADsDSOObject;"cmd.activeconnection = cnset objRoot = getobject("LDAP://RootDSE")cmd.commandtext = "<LDAP://" & objRoot.get("defaultNamingContext") & ">;(objectCategory=Computer);" & _ "name,operatingsystem,operatingsystemservicepack, operatingsystemversion, dnsHostName;subtree"'**** Bypass 1000 record limitation ****cmd.properties("page size")=1000set rs = cmd.executeset objFSO = createobject("Scripting.FileSystemObject")set objCSV = objFSO.createtextfile(FileName)q = """"while rs.eof <> true and rs.bof <> true objcsv.writeline(q & rs("name") & q & "," & q & rs("operatingsystem") & q & _ "," & q & rs("operatingsystemservicepack") & _ q & "," & q & rs("operatingsystemversion") & _ q & "," & q & rs("dnsHostName") & _ q & "," & q & Date() & q) rs.movenextwendobjCSV.Closecn.closewscript.echo "Finished"
Recommended Posts
Please sign in to comment
You will be able to leave a comment after signing in
Sign In Now