Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Output AD information to CSV

Status
Not open for further replies.

kachbo1

IS-IT--Management
Nov 16, 2004
40
0
0
GB
Hi,

I have this script which outputs the username and email address of users from our AD into a txt file.

I would like the output to be in .csv format. Can anyone help me modify the script.

Thanks in advance.

Kachbo.


'Global variables
Dim oContainer
Dim OutPutFile
Dim FileSystem

'Initialize global variables
Set FileSystem = WScript.CreateObject("Scripting.FileSystemObject")
Set OutPutFile = FileSystem.CreateTextFile("c:\email.txt", True)
Set oContainer=GetObject("LDAP://ou=Departments,DC=ad,DC=hackney,DC=gov,DC=uk")

'Enumerate Container
EnumerateUsers oContainer

'Clean up
OutPutFile.Close
Set FileSystem = Nothing
Set oContainer = Nothing

WScript.Echo "Finished"
WScript.Quit(0)
Sub EnumerateUsers(oCont)
Dim oUser
For Each oUser In oCont
Select Case LCase(oUser.Class)
Case "user"

'If Not IsEmpty(oUser.distinguishedName) Then
'OutPutFile.WriteLine "dn: " & oUser.distinguishedName
'End If

If Not IsEmpty(oUser.name) Then
OutPutFile.WriteLine "name: " & oUser.Get ("name")
End If
'need to do this because oUser.name would get back the Relative Distinguished name (i.e. CN=Jo Brown)

'If Not IsEmpty(oUser.st) Then
'OutPutFile.WriteLine "st: " & oUser.st
'End If

If Not IsEmpty(oUser.mail) Then
OutPutFile.WriteLine "mail: " & oUser.mail
End If

Case "organizationalunit" , "container"

EnumerateUsers oUser
End Select
OutPutFile.WriteLine
Next
End Sub
 
Try changing email.txt to email.csv, i had a similiar problem and it was suggessted to me just change the file extension and in my case it worked.
 
The modification is something like this.
[tt]
>Set OutPutFile = FileSystem.CreateTextFile("c:\email.txt", True)
Set OutPutFile = FileSystem.CreateTextFile("c:\email.[red]csv[/red]", True)
[/tt]
Then the sub is something like this.
[tt]
Sub EnumerateUsers(oCont)
Dim oUser
For Each oUser In oCont
Select Case LCase(oUser.Class)
Case "user"

'If Not IsEmpty(oUser.distinguishedName) Then
' OutPutFile.Write "dn: " & oUser.distinguishedName
'else
' 'do nothing for the first column
'End If

If Not IsEmpty(oUser.name) Then
OutPutFile.Write "," & "name: " & oUser.Get ("name")
else
OutPutFile.Write ","
End If
'need to do this because oUser.name would get back the Relative Distinguished name (i.e. CN=Jo Brown)

'If Not IsEmpty(oUser.st) Then
' OutPutFile.Write "," & "st: " & oUser.st
'else
' OutPutFile.Write ","
'End If

If Not IsEmpty(oUser.mail) Then
OutPutFile.Write "," & "mail: " & oUser.mail
else
OutPutFile.Write ","
End If

OutPutFile.Write vbcrlf

Case "organizationalunit" , "container"

EnumerateUsers oUser
End Select

Next
End Sub
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top