Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using variable in LDAP string

Status
Not open for further replies.

volk125

Technical User
Jul 10, 2003
76
CA
I have this script:
------------------------------------------------------
Option Explicit
On Error Resume Next

Dim wshNetwork, wshFileSystem, wshShell
Dim strUserName

Set wshNetwork = Wscript.CreateObject("Wscript.Network")

strUserName = wshNetwork.UserName

Set objUser = GetObject("LDAP://cn=userA ,ou=Users,ou=Branch,ou=Company,dc=somedomain,dc=com")
Wscript.Echo "Department: " & objUser.Department
---------------------------------------------------------

Instead of userA in the LDAP string I need to use the variable strUserName so I can get the department name based on the user logging in.

I tried this:
1. Set objUser = GetObject("LDAP://cn=strUserName ,ou=Users,ou=Branch,ou=Company,dc=somedomain,dc=com")
2. Set objUser = GetObject("LDAP://cn=" & strUserName & " ,ou=Users,ou=Branch,ou=Company,dc=somedomain,dc=com")

both didn't work. However when I put the actual username then everything works fine. so I just need to insert a variable there. I am not a huge VB scripter therefore I need some assisstance. Please help me out.
 
option 2 should have worked

Set objUser = GetObject("LDAP://cn=" & strUserName & " ,ou=Users,ou=Branch,ou=Company,dc=somedomain,dc=com")

r you sure that the user in question is in the 3 ou's etc in moniker string (is that the right work?)
 
The correct syntax is 2.
Just to be sure you may debug like this (before the AD call):
MsgBox "strUserName='" & strUserName & "'"

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
all depends where you are getting strUserName from i guess and if this is really in the ADsPath that you are building..how about doing this... i know it is slower but it might highlight an issue, infact it might be rather arse about face but i am eating my lunch ;-)

Set objUser = GetObject("WinNT://yourdomain/" & strUserName)
Set objLDAPUser = GetObject(objUser.ADsPath)
Wscript.Echo "Department: " & objLDAPUser.Department


 
nope sorry i shouldnt think out loud that was a total load of rubbish i have just posted, sorry
 
The point is to see for yourself what you get strUsername. There is no guarantee that it coincides the relevant data in the ldap's rdn of the user. So, even though syntax line 2 is the correct line, there is no apriori guarantee.

[1] For win9x logging, you have to wait until you get anything. Hence, you've to add this before the getobject line.
[tt]
strUsername=""
do while strUsername=""
wscript.sleep 50
strUsername=wshnetwork.username
loop
[/tt]
[2] Even then, you have to translate the samaccount name with domain name to get the rdn.
[tt]
'add all your dims
strUsername=""
do while strUsername="" 'for win9x compat only
wscript.sleep 50
strUsername=wshnetwork.username
loop
strDomain=wshnetwork.domainname
set otrans=createobject("nametranslate")
with otrans
.init 3,""
.set 3,strDomain & "\" & strUsername
end with
sdn=otrans.get(1)
set ouser=getobject("LDAP://" & sdn)
[/tt]
 
'this should do it
Set objTrans = CreateObject("NameTranslate")
objTrans.Set 3, "domain_name_here\" & strUserName
strUserDN = objTrans.Get(1)
Msgbox strUserDN
Set objUser = GetObject("LDAP://" & strUserDN)
 
>a ha, tsuji beat me to it...
The secret is let the other guys put all those dims for themselves.
 
thanks for all responses. I haven't followed up yet.
I did fix it, the problem was that I was looking at the damn code and not seeing... I forgot to declare objUser :eek:))
stupid me. oh well...

but now, can somebody tell me where I can find a good reference to object properties. those things like
onjUser.MemberOf/username/department and so on
 
>I did fix it, the problem was that I was looking at the damn code and not seeing... I forgot to declare objUser
Let the forum understand. Are you saying once you dim the objUser, you think the script is correct (including with line 2 as members pointed out)?
 
Furthermore,
>However when I put the actual username then everything works fine.
How come without dimming objUser (with option explicit) puting actually username works?

 
Vanishingly small point, but considering Basic's [tt]Dim[/tt] has its origin in Fortran's [tt]DIMENSION[/tt] keyword, the proper way to refer to this would be "dimensioning objUser."

Fortran used this word in reference to declaring arrays and their dimensions.

Of course that seldom keeps me from typing "dimming" myself.
 
Thanks mate for the notes. It sure seems to start as an abuse of mathematical term and eventually made legitime and a life of its own.
 
it works. thanks. nothing to discuss anymore.
 
>nothing to discuss anymore
I don't want to discuss. I just want the forum to register candid note on the resolution of a problem, not ambiguous in word or in deed.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top