Reading a text file of DNs

Hi guys, hope you are all well and can assist.

Guys

I have a text file that contains a list of DNs for users

========================================= DN.txt
CN=Millie Waller,OU=Americas,OU=Migrated Objects,DC=test,DC=net
CN=Atea Konsult ORI,OU=Users,OU=EU,OU=Migrated Objects,DC=test,DC=net
CN=Santos Diaz,OU=Users,OU=EU,OU=Migrated Objects,DC=test,DC=net
CN=Cornwell Mulubwa,OU=Users,OU=EU,OU=Migrated Objects,DC=test,DC=net
CN=Stuart Wickie,OU=Users,OU=EU,OU=Migrated Objects,DC=test,DC=net
CN=Mark Waugh,OU=Users,OU=EU,OU=Migrated Objects,DC=test,DC=net

What I want to do is the following:

1) Read the text file
2) For each entry, return the description attribute
3) Create a new text file eg.NewFile.txt, containing the DN, and the description for that DN

The following query gets the description I believe..

dsquery user "<Full DN Path>" | dsget user -desc

So here is what Im looking for:

========================================= NewFile.txt
CN=Millie Waller,OU=Americas,OU=Migrated Objects,DC=test,DC=net, Millie Walker
CN=Atea Konsult ORI,OU=Users,OU=EU,OU=Migrated Objects,DC=test,DC=net, Atea Konsult
CN=Santos Diaz,OU=Users,OU=EU,OU=Migrated Objects,DC=test,DC=net, Santos Diaz
CN=Cornwell Mulubwa,OU=Users,OU=EU,OU=Migrated Objects,DC=test,DC=net, Conrwell Mulubwa
CN=Stuart Wickie,OU=Users,OU=EU,OU=Migrated Objects,DC=test,DC=net, Stuart Wickie
CN=Mark Waugh,OU=Users,OU=EU,OU=Migrated Objects,DC=test,DC=net, Mark Waugh


Any help greatly appreciated.
LVL 1
Simon336697Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

pritamduttCommented:
Here is a script code to help you achieve the desired results.

Dim strL 
Const ForReading = 1 
Set objRoot = GetObject("LDAP://RootDSE")  
varDomainNC = objRoot.Get("DefaultNamingContext")  
Set objDomain = GetObject("LDAP://" & varDomainNC)  
Set objFSO = CreateObject("Scripting.FileSystemObject") 
Set outFile = objFSO.CreateTextFile("DN-Info.txt", True)
Set inFile = objFSO.OpenTextFile("DN.txt", ForReading)
Do until inFile.AtEndOfStream 
	strL = inFile.ReadLine 
	  
	Set objOU = GetObject("LDAP://" & strL) 
	strDescription = objOU.Get("description")	  
	outFile.WriteLine strL & "," & strDescription
Loop 

inFile.Close
outFile.Close

Open in new window


Hope this helps.
0
Simon336697Author Commented:
Hi pritamdutt,
Thanks so much for your help on that.
What I found is, is that your script works for every user account that has a description set.
If the description is blank however, the script will fail on that line in the text file, and abort.

The error is:

Line: 13
Char 2
Error: The directory property cannot be found in the cache.
Code: 8000500d
Source: Active Directory

So, it fails on:

      strDescription = objOU.Get("description")

As there is no value present for Description.

Is there a way to continue on if there is no value present for the description field, something like....
"If description is blank, then populate with "No value", then continue on.

Thanks so much for your help.
0
pritamduttCommented:
I have modified code to address your need.

On Error Resume Next
Dim strL 
Const ForReading = 1 
Set objRoot = GetObject("LDAP://RootDSE")  
varDomainNC = objRoot.Get("DefaultNamingContext")  
Set objDomain = GetObject("LDAP://" & varDomainNC)  
Set objFSO = CreateObject("Scripting.FileSystemObject") 
Set outFile = objFSO.CreateTextFile("DN-Info.txt", True)
Set inFile = objFSO.OpenTextFile("DN.txt", ForReading)
Do until inFile.AtEndOfStream 
        strL = inFile.ReadLine 
          
        Set objOU = GetObject("LDAP://" & strL) 
        strDescription = "No Description"
        strDescription = objOU.Get("description")         
        outFile.WriteLine strL & "," & strDescription
Loop 

inFile.Close
outFile.Close

Open in new window


Hope this helps!
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Simon336697Author Commented:
Hi pritamdutt,
Thanks so much, that works perfectly
Can I please ask just one more thing?

We have several domains in our environment.

For example:

parentdomain
  |_____ subdomainA
  |_____ subdomainB
  |_____ subdomainC

The domain from which I am logged on to is the subdomainB

Is it true that this is

Set objRoot = GetObject("LDAP://RootDSE")  

Is it the above command that says to query the domain in which you are a member of/logged on to?

The reason I ask is what if I wanted to run your script against another domain eg.subdomainC?

How would I change

Set objRoot = GetObject("LDAP://RootDSE")  

to point to subdomainC?

Any help greatly appreciated.
0
pritamduttCommented:
In this case you could skip the following 2 lines
Set objRoot = GetObject("LDAP://RootDSE")  
varDomainNC = objRoot.Get("DefaultNamingContext") 

Open in new window


with something like

varDomainNC = "DC=subDomainA,DC=parentDomain,DC=com"

Open in new window


You should be able to get this information from users DN String
for example the following string has

CN=Millie Waller,OU=Americas,OU=Migrated Objects,DC=test,DC=net

DC=test, DC=net as domain name

Hope this helps.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
pritamduttCommented:
Any updates? Is your query resolved?
0
Simon336697Author Commented:
Thanks pritamdutt, that is brilliant.
Much appreciated and sorry about the delay.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Scripting Languages

From novice to tech pro — start learning today.