Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


Assign env variables from ldap search

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,2850
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Assign env variables from ldap search
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 Assign env variables from ldap search - 5/1/2005 7:21:39 AM   
  bradu

 

Posts: 10
Score: 0
Joined: 4/30/2005
From:
Status: offline
Hi,

I'm really new to vbs and looked for some time now for such a script and couldn't find any. All I could find was how to search in ldap by providing dn.
What I want to do is to search for email and display details in Windows 2003 ldap using the windows environment system variable username and assign the results to other system env variables.
Can any one help with that?

Many thanks in advance
 
 
Post #: 1
 
 Re: Assign env variables from ldap search - 5/1/2005 8:07:14 AM   
  token

 

Posts: 1917
Score: 0
Joined: 1/14/2005
From:
Status: offline
What do you mean by "display details in Windows 2003 ldap" ?

(in reply to bradu)
 
 
Post #: 2
 
 Re: Assign env variables from ldap search - 5/1/2005 8:50:05 AM   
  bradu

 

Posts: 10
Score: 0
Joined: 4/30/2005
From:
Status: offline
Sorry. I was not descriptive enough.
I want to use "samAccountName" which I want to get from Windows env user variable, search for "email" and "displayname" attributes for that user and set them as env variables as well.

(in reply to bradu)
 
 
Post #: 3
 
 Re: Assign env variables from ldap search - 5/2/2005 6:57:49 AM   
  token

 

Posts: 1917
Score: 0
Joined: 1/14/2005
From:
Status: offline
Try the following.
=====================================================================================

Option Explicit
Dim shell, domain, userID, network, email, displayName, conn, cmd, rs, envUser
Set shell = CreateObject("WScript.Shell")
Set network = CreateObject("WScript.Network")
Set envUser = shell.Environment("USER")
Set conn = CreateObject("ADODB.Connection")
Set cmd = CreateObject("ADODB.Command")
conn.provider = "adsdsoobject"
conn.open "active directory provider"
cmd.activeconnection = conn
userID = shell.ExpandEnvironmentStrings("%USERNAME%")
cmd.commandtext = "<LDAP://space/dc=continuium,dc=org>;(&(objectclass=user)(samaccountname=" & userID & "));mail,displayname;subtree"
Set rs = cmd.Execute
If IsNull(email) Then
email = ""
Else
email = rs.fields("mail")
End If
If IsNull(displayName) Then
displayName = ""
Else
displayName = rs.fields("displayname")
End If
envUser("email") = email
envUser("displayname") = displayName

(in reply to bradu)
 
 
Post #: 4
 
 Re: Assign env variables from ldap search - 5/3/2005 6:39:46 AM   
  bradu

 

Posts: 10
Score: 0
Joined: 4/30/2005
From:
Status: offline
Token, many thanks indeed for your help. The script is running fine on Win2K/XP. However, on some machines the script returns an error.
The live script is:
------------------------
Option Explicit
Dim shell, domain, userID, network, EMAIL, DISPLAYNAME, conn, cmd, rs, envUser
Set shell = CreateObject("WScript.Shell")
Set network = CreateObject("WScript.Network")
Set envUser = shell.Environment("USER")
Set conn = CreateObject("ADODB.Connection")
Set cmd = CreateObject("ADODB.Command")
conn.provider = "adsdsoobject"
conn.open "active directory provider"
cmd.activeconnection = conn
userID = shell.ExpandEnvironmentStrings("%USERNAME%")

cmd.commandtext = "<LDAP://myserver/dc=ho,dc=location-co,dc=country,dc=company,dc=com>;(&(objectclass=user)(samaccountname=" & userID & "));mail,displayname;subtree"

Set rs = cmd.Execute
If IsNull(email) Then
EMAIL = ""
Else
EMAIL = rs.fields("mail")
End If

If IsNull(displayName) Then
DISPLAYNAME = ""
Else
DISPLAYNAME = rs.fields("displayname")
End If

envUser("EMAIL") = EMAIL
envUser("DISPLAYNAME") = DISPLAYNAME

------------------------------------------
The following error occurs on some computers no matter what user logs on:
Line 28
Char 1
Error: Type mismatch
Code: 800A000D
Source: Microsoft VBscript runtime error

The line 28 contains

envUser("EMAIL") = EMAIL

Could you please advice on this error?

Many thanks in advance.

(in reply to bradu)
 
 
Post #: 5
 
 Re: Assign env variables from ldap search - 5/3/2005 6:49:48 AM   
  token

 

Posts: 1917
Score: 0
Joined: 1/14/2005
From:
Status: offline
Replace:

envUser("EMAIL") = EMAIL

with:

envUser("EMAIL") = join(EMAIL)

and check the value of the %EMAIL% to see what you have. You get this error because the email attribute within AD isn't simply a string. My guess is that it is an array and therefore generates the error. What that means is that the user might have multiple email entries. What you need to do is determine what the %EMAIL% contains and see how we should go about it.

(in reply to bradu)
 
 
Post #: 6
 
 Re: Assign env variables from ldap search - 5/3/2005 6:20:12 PM   
  bradu

 

Posts: 10
Score: 0
Joined: 4/30/2005
From:
Status: offline
I replaced the last 2 lines as above but I still get the error message:

Line 28
Char 1
Error: Type mismatch: 'join'
Code: 800A000D
Source: Microsoft VBscript runtime error

Any ideea?

(in reply to bradu)
 
 
Post #: 7
 
 Re: Assign env variables from ldap search - 5/3/2005 6:55:10 PM   
  token

 

Posts: 1917
Score: 0
Joined: 1/14/2005
From:
Status: offline
Could you add the following line:

wscript.echo vartype(email)

above the following 2 lines:

envUser("EMAIL") = EMAIL
envUser("DISPLAYNAME") = DISPLAYNAME

and let me know the result ?

(in reply to bradu)
 
 
Post #: 8
 
 Re: Assign env variables from ldap search - 5/4/2005 12:42:07 AM   
  bradu

 

Posts: 10
Score: 0
Joined: 4/30/2005
From:
Status: offline
The result is 8.

(in reply to bradu)
 
 
Post #: 9
 
 Re: Assign env variables from ldap search - 5/4/2005 8:28:42 AM   
  token

 

Posts: 1917
Score: 0
Joined: 1/14/2005
From:
Status: offline
8 means "string" which shouldn't give you a type mismatched error. Did that come from the workstation that you said will produce the error ?

Also, add the following 2 lines:

email = ""
displayname = ""

After this line:

userID = shell.ExpandEnvironmentStrings("%USERNAME%")

and run the script again on those computers that gave you the error, and see what happens.

(in reply to bradu)
 
 
Post #: 10
 
 Re: Assign env variables from ldap search - 5/5/2005 7:03:27 AM   
  bradu

 

Posts: 10
Score: 0
Joined: 4/30/2005
From:
Status: offline
I did add the two lines and it's working. Actually after a closer look I realized that if the mail and displayname attributes are blank in ldap then the script will return an error (that's why it was not working on diff machines) but as soon as I updated those fields for all users the script runs with no errors.
Many thanks for your help.

(in reply to bradu)
 
 
Post #: 11
 
 Re: Assign env variables from ldap search - 5/5/2005 7:58:07 AM   
  token

 

Posts: 1917
Score: 0
Joined: 1/14/2005
From:
Status: offline
You are welcome =) Glad you got it working.

(in reply to bradu)
 
 
Post #: 12
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Assign env variables from ldap search Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts