Solved

LDAP Query - get Distinguishedname and remove CN

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

RHCE - Red Hat OpenStack Prep Course

This course will provide in-depth training so that students who currently hold the EX200 & EX210 certifications can sit for the EX310 exam. Students will learn how to deploy & manage a full Red Hat environment with Ceph block storage, & integrate Ceph into other OpenStack service

Question has a verified solution.

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

Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
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.
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…

624 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