Solved

Remove Parts of the Computer's Distinguished Name

Posted on 2011-09-20
7
811 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

Is Your AD Toolbox Looking More Like a Toybox?

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

Suggested Solutions

Title # Comments Views Activity
schema master 5 26
Outlook keeps asking for credentials, and then does not except. 10 63
Run Secure WMI query from CentOS 5 27
AD Sites/AD Replication 11 32
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
This article outlines the process to identify and resolve account lockout in an Active Directory environment.
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 Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

776 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