Please help! I have been trying to get the following code to work,
which should loop through two OU's and delete any duplicates from the
2nd OU. It is failing on the objOU2.Delete line with "An Invalid dn
syntax has been specified". This error normally indicates an invalid
LDAP string has been used, but I can echo the contents of
objContact1.cn and objContact2.cn as it iterates through the loops just
fine. Can anyone help me understand why the delete will not work, and
suggest another way I might can make this work. Thanks!


Code starts here:

Set objOU1 =
GetObject("LDAP://OU=Pager,OU=Users,OU=Divisions,DC=MyDomain,DC=com")
objOU1.Filter = Array("contact")

Set objOU2 =
GetObject("LDAP://OU=PagerOther,OU=Users,OU=Divisions,DC=MyDomain,DC=com")
objOU2.Filter = Array("contact")

For Each objContact1 In objOU1
For Each objContact2 In objOU2
If objContact1.cn = objContact2.cn then
objOU2.Delete "contact", "cn=" & objContact2.cn
Exit For
End If
Next
Next

Re: Deleting a Contact from AD by jacksneed2000

jacksneed2000
Mon Sep 12 11:46:58 CDT 2005

Never mind - I finally figured it out. All I needed to do is insert a
backslash before the comma, to escape the comma between a user's
lastname and firstname. Here's my updated code, for anyone else who
might benefit from this post.

Replace the objOU2.Delete line in my first post with these two lines:

conTemp = replace(objContact2.cn,",","\,")
objOU2 Delete "contact","cn=" & conTemp


Re: Deleting a Contact from AD by mdowdy

mdowdy
Mon Sep 12 12:00:53 CDT 2005

The issue here is almost certainly due to the difference between a "cn"
and "relative distinguished name", which is what you are actually
using for the second parameter of the delete line. See
http://www.microsoft.com/technet/scriptcenter/guide/sas_usr_xdqd.mspx
for the description of the delete method and parameters.

In the "relative distinguished name" (RDN), just as in a full
distinguished name, commas must be preceded by a backslash. in other
words, if the cn equals "Smith, John" the RDN equals "cn=Smith\, John".


Replace your objOU2.Delete line with the following:

Code:

strToDelete = ""
if instr(objContact2.cn, ",") > 0 then

arrcn = split(objContact2.cn, ",")
strToDelete = arrcn(0)

'in case there is more than one comma,
'loop through the rest of the array
'instead of simply adding & "\," & arrcn(1)

for counter = 1 to ubound(arrcn)
strToDelete & "\," & arrcn(counter)
next

else
strToDelete = objContact2.cn
end if
objOU2.Delete "Contact", "cn=" & strToDelete



End Code