Solved

LDAP Query - get Distinguishedname and remove CN

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to count json data 2 78
Need to Modify a Script I found 5 99
Python variable _ manually assigned 9 85
ROBOFTP UNZIP 1 45
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…
This article will show, step by step, how to integrate R code into a R Sweave document
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

778 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