Solved

Remove Parts of the Computer's Distinguished Name

Posted on 2011-09-20
7
825 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
  • 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Disabling the Directory Sync Service Account in Office 365 will stop directory synchronization from working.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …

856 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