Solved

LDAP Query - get Distinguishedname and remove CN

Posted on 2008-06-09
2
2,670 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
2 Comments
 
LVL 70

Accepted Solution

by:
Chris Dent earned 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This tutorial will discuss fancy secure registration forms, with AJAX technology support. In this article I assume you already know HTML and some JS. I will write the code using WhizBase Server Pages, so you need to know some basics in WBSP (you mig…
This article will show, step by step, how to integrate R code into a R Sweave document
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 look for a specific file type in a local or remote server directory using PHP.

929 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now