We help IT Professionals succeed at work.

GetObject special characters

sdonaldson07
sdonaldson07 asked
on
I am trying to get a list of Distribution List members from AD. I have a script that will pull the members, but I ran into an issue with the name of a specific DL. The name has spaces and commas in it, which is causing the script to freak out. I have tried:

Set objGroup = GetObject("LDAP://CN=First Last, First Last, First Last,OU=NAME,OU=All Users,DC=DOMAIN,DC=COM")

Open in new window


Set objGroup = GetObject('"LDAP://CN=First Last, First Last, First Last,OU=NAME,OU=All Users,DC=DOMAIN,DC=COM"')

Open in new window



Set strOUContainter = "CN=First Last, First Last, First Last,OU=NAME"
Set objGroup = GetObject("LDAP://" & strOUContainer & "," & "OU=All Users,DC=DOMAIN,DC=COM")

Open in new window


Set strOUContainter = """CN=First Last, First Last, First Last,OU=NAME"""
Set objGroup = GetObject("LDAP://" & strOUContainer & "," & "OU=All Users,DC=DOMAIN,DC=COM")

Open in new window


I'm not sure how to get the script to read the name of the DL without freaking out over the commas. Any ideas?
Comment
Watch Question

I've never had a reason to use it because I stay away from using special characters in my directory.
The special characters are:
  • , (comma)
  • =  (equals)
  • + (plus)
  • <  (less than)
  • > (greater than)
  • #  (number sign)
  • ; (semicolon)
  • \ (backslash)
  • "  (quotation mark)
Try "escaping" the comma with a preceding backslash.  I think this is how you're supposed to do it.

For example, if the name of the LDAP object is a company called "Dewey, Cheatham, and Howe" then to do an LDAP lookup, the CN of that company would look like:

Set objGroup = GetObject("LDAP://CN=Dewey\, Cheatham\, and Howe,OU=NEWYORK,OU=USA,DC=DOMAIN,DC=COM")

Open in new window

Also, spaces are fine inside names except for if they are Leading and Trailing spaces.  Those have to be escaped also:
" Dewey, Cheatham, and Howe " would be:

CN=\ Dewey\, Cheatham\, and Howe\ ,OU=New York,OU=USA,DC=Domain,DC=com

Author

Commented:
CitizzenRon:

I tried to escape the commas like you suggested and got the following error message:
Microsoft VBScript runtime error: Object required: '[string: "CN=First Last\, Fir"]'

The error message did not finish the second person's first name.
That error code would indicate that there was something  wrong with the object syntax itself, likely an invalid character or  something like that right after the "r" in "First".

Is the object actually named "First Last, First Last, First Last" or are you editing it before showing it here?  Make sure you only escape the commas that are actually in the name, not the ones separating the objects.

I created a User in my directory named literally "Lastname, Firstname" and did a couple different things with it using the escaped comma (\,) in the LDAP query and it worked fine with it escaped by the backslash and, of course, it didn't work when it was not escaped.

Using your script examples above, here's how I would modify the first and third to work.  Only two backslashes were needed.



Set objGroup = GetObject("LDAP://CN=First Last\, First Last\, First Last,OU=NAME,OU=All Users,DC=DOMAIN,DC=COM")


------------------------------------------------------------------
Set strOUContainter = "First Last\, First Last\, First Last"
Set objGroup = GetObject("LDAP://CN=" & strOUContainer & ",OU=NAME,OU=All Users,DC=DOMAIN,DC=COM")

Open in new window

Author

Commented:
Yes I am editing the names in my example. I made sure to only escape the characters separating the names, not the ones separating the object. I have tried setting up the CN as a variable and inserting it into the GetObject and I have also tried keeping the names in the GetObject and escaping the commas there. Neither method worked for me.

Author

Commented:
Closing question.