• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 192
  • Last Modified:

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.
0
Simon336697
Asked:
Simon336697
  • 4
  • 3
1 Solution
 
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
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

 
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
 
pritamduttCommented:
Any updates? Is your query resolved?
0
 
Simon336697Author Commented:
Thanks pritamdutt, that is brilliant.
Much appreciated and sorry about the delay.
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now