Solved

Remove Parts of the Computer's Distinguished Name

Posted on 2011-09-20
7
850 Views
Last Modified: 2012-05-12
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

0
Comment
Question by:JB4375
[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
  • 4
  • 3
7 Comments
 
LVL 65

Expert Comment

by:RobSampson
ID: 36570377
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.
0
 
LVL 1

Author Comment

by:JB4375
ID: 36573457
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!!
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
ID: 36573512
So something like
Excel.Cells(counter,6).Value = Mid(objComputer.DistinguishedName, InStrRev(objComputer.DistinguishedName, "OU=") + 3)

Rob.
0
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
LVL 1

Author Comment

by:JB4375
ID: 36575829
Rob,

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

Thanks!!
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 36577284
OK cool. Is there anything else you need?

Rob.
0
 
LVL 1

Author Closing Comment

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

Thanks again, Rob. :)
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 36580165
LOL!  No worries. Thanks for the grade.

Rob.
0

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

A company’s centralized system that manages user data, security, and distributed resources is often a focus of criminal attention. Active Directory (AD) is no exception. In truth, it’s even more likely to be targeted due to the number of companies …
Uncontrolled local administrators groups within any organization pose a huge security risk. Because these groups are locally managed it becomes difficult to audit and maintain them.
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

724 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