?
Solved

LDAP Query - get Distinguishedname and remove CN

Posted on 2008-06-09
2
Medium Priority
?
2,680 Views
Last Modified: 2008-06-10
Hi All,

I'm trying to write a script that will delete Global Security groups in bulk. The script reads the group names from a file, finds it in the domain(could be in any OU, we've got multiple OUs) and deletes it.

So far this is what i got ... this is my problem ...
I'm now setting the objOU myself, i need the script to set this automatically depending on which OU the group is in the domain.
I can get the ADSPath but i can't remove the CN only, i end up with an array i need to join them back.
Could you suggest a solution or an alternative method?

Set objOU= GetObject("LDAP://q1-core.q1.aig.net/OU=Groups,OU=Livingston_NJ,OU=AIGT,OU=Q1,DC=q1-core,DC=q1,DC=aig,DC=net")

The script below so far .... (have not worked on the read from file yet, trying to get the delete running 1st)


strGroup = WScript.Arguments (0)
On Error Resume Next
Const ADS_SCOPE_SUBTREE = 2

Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection

objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
objCommandProperties("Sort On") = "Name"




Wscript.echo "check1" + strGroup

objCommand.CommandText = _
    "SELECT ADsPath, distinguishedName FROM 'LDAP://q1-core.q1.aig.net' WHERE cn='" & strGroup &"'"

Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst

Do Until objRecordSet.EOF
    strPath = objRecordSet.Fields("ADsPath").Value
    Set objGroup = GetObject(strPath)
    Set objOU= GetObject("LDAP://q1-core.q1.aig.net/OU=Groups,OU=Livingston_NJ,OU=AIGT,OU=Q1,DC=q1-core,DC=q1,DC=aig,DC=net")
    Wscript.Echo "Group: " & strPath

    strDN = objRecordSet.Fields("distinguishedName").Value
    arrTemp = Split(strDN, ",")
    strOU = arrTemp(0)    

    Wscript.echo strOU

    objOU.Delete "group", strOU

    objRecordSet.MoveNext
Loop
0
Comment
Question by:alexjayaprakash
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 71

Accepted Solution

by:
Chris Dent earned 2000 total points
ID: 21749316

Hey there,

You can get the parent of an object from the object itself. Here you have a connection to the group:

    Set objGroup = GetObject(strPath)

Therefore this gives you a connection to the OU:

    Set objOU = GetObject(objGroup.Parent)

It's perhaps not the fastest way because it relies on connections to two objects. But it does prevent worry about parsing strings.

HTH

Chris
0
 
LVL 3

Author Comment

by:alexjayaprakash
ID: 21752995
Thanks so much, works like a charm!
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently I have been answering a lot of questions like this in IT forums that I frequent. The question posed is usually something along the lines of "We have software X installed and need to uninstall it for reason Y" or some other variant of the sa…
I hope you'll find this tutorial useful and interesting. So let's try to extend Tcl with a new package.  For anyone more deeply interested please check out the book "Practical Programming in Tcl and Tk". It's really one of the best written books abo…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question