Solved

LDAP Query - get Distinguishedname and remove CN

Posted on 2008-06-09
2
2,668 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction This tutorial will give you a fast look what you can do with WhizBase. I expect you already know how to work with HTML at least, and that you understand the basics of the internet and how the internet works. WhizBase is a server-s…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

708 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

17 Experts available now in Live!

Get 1:1 Help Now