Link to home
Start Free TrialLog in
Avatar of JB4375
JB4375Flag for United States of America

asked on

Remove Parts of the Computer's Distinguished Name

I have a script that queries an OU for all the computer and outputs the computer: CN, OS, Owner, When Created, and the distinguished name to an excel spread sheet. I'd like to remove the last two pieces of the DN so that whoever gets the spreadsheet could sort on the computer name, CN, or the OU in the event I was searching Active Directory aka DN.

Example DN: CN=PCNAME,OU=COMPUTERS,OU=FINANCE,OU=DEPT,OU=DOMAIN.
In this case I'd like to remove: CN=PCNAME,OU=COMPUTERS, and if possible the OU= before Finance as well.

Thanks!!

DoRecursive("OU=computers,OU=Finance,OU=Departments,DC=domain.com")

 
Function DoRecursive(strObjectDN)
Set objOU = GetObject("LDAP://" & strObjectDN)
objOU.Filter = Array("Computer")

Set objUser = GetObject("LDAP://" & strObjectDN)	
Set objFSO = CreateObject("Scripting.FileSystemObject")

For Each objComputer In objOU
Set objNtSecurityDescriptor = objComputer.Get("ntSecurityDescriptor")	

    Excel.Cells(counter,1).Value = objComputer.CN
    Excel.Cells(counter,2).Value = objComputer.OperatingSystem
    Excel.Cells(counter,3).Value = objNtSecurityDescriptor.owner
    Excel.Cells(counter,4).Value = objComputer.WhenCreated
    Excel.Cells(counter,5).Value = objComputer.DistinguishedName
    counter =counter +1    

Next
End Function

Open in new window

Avatar of RobSampson
RobSampson
Flag of Australia image

Hi, have a look at using objComputer.Parent or objComputer.Parent.Parent to return various elements of the string.  I'm not sure whether you mean from
Example DN: CN=PCNAME,OU=COMPUTERS,OU=FINANCE,OU=DEPT,OU=DOMAIN.
In this case I'd like to remove: CN=PCNAME,OU=COMPUTERS, and if possible the OU= before Finance as well.

that you want to display just
OU=DEPT,OU=DOMAIN

or
CN=PCNAME,OU=COMPUTERS,OU=FINANCE

Rob.
Avatar of JB4375

ASKER

Sorry about that.... just OU=Department,OU=Domain, and if possible remove the "OU=" off the front of department.

That way when I output it to a spreadsheet Col1 allows me to sort on Computer Name and Col4 allows me to sort by Department.

Thanks!!
ASKER CERTIFIED SOLUTION
Avatar of RobSampson
RobSampson
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of JB4375

ASKER

Rob,

That's close enough to get me where I want to be.

Thanks!!
OK cool. Is there anything else you need?

Rob.
Avatar of JB4375

ASKER

Yes, apparently I "need" to remember to close the question after I've been given a viable solution. LOL.

Thanks again, Rob. :)
LOL!  No worries. Thanks for the grade.

Rob.