Re: export ad querie to txt
- From: "Richard Mueller [MVP]" <rlmueller-nospam@xxxxxxxxxxxxxxxxxxxx>
- Date: Thu, 26 Jun 2008 06:01:37 -0500
Johan deheugden wrote:
all;, i got this script( received a few days ago on this forum) and i want
the output to be placed in a txt file. how should i change that?
thanks in advance.
'On Error Resume Next
Const ADS_SCOPE_SUBTREE = 2
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
objCommand.CommandText = _
"SELECT name, homeDirectory FROM
'LDAP://ou=ouname,ou=ouname,ou=ouname,dc=dcname,dc=dcname,dc=com' WHERE
objectClass='user' AND homeDirectory='*'"
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst
Do Until objRecordSet.EOF
Wscript.Echo objRecordSet.Fields("Name").Value
Wscript.Echo objRecordSet.Fields("homeDirectory").Value
objRecordSet.MoveNext
Loop
Just run the script at a command prompt with the cscript host and redirect
the output to a text file. For example, if the program is saved in
Example.vbs:
cscript //nologo Example.vbs > output.txt
If the file Example.vbs is not in the current directory you must include the
file path. The file output.txt is created in the current directory. The
optional "//nologo" suppresses WSH logo info. Since you probably want one
user per line, perhaps with the fields comma delimited, modify the loop as
follows:
========
Do Until objRecordSet.EOF
strName = objRecordSet.Fields("Name").Value
strHomeDir = objRecordSet.Fields("homeDirectory").Value
Wscript.Echo strName & "," & strHomeDir
objRecordSet.MoveNext
Loop
======
Or, to use the FileSystemObject you can use code similar to:
=========
Const ForWriting = 2
Const OpenAsASCII = 0
Const CreateIfNotExist = True
' Specify file path and name.
strFilePath = "c:\scripts\output.txt"
' Open the file for writing.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strFilePath, _
ForWriting, CreateIfNotExist, OpenAsASCII)
' Write a header line to the file.
objFile.WriteLine "User Name,Home Directory"
Do Until objRecordSet.EOF
strName = objRecordSet.Fields("Name").Value
strHomeDir = objRecordSet.Fields("homeDirectory").Value
objFile.WriteLine strName & "," & strHomeDir
objRecordSet.MoveNext
Loop
objFile.Close
objRecordset.Close
objConnection.Close
--
Richard Mueller
MVP Directory Services
Hilltop Lab - http://www.rlmueller.net
--
.
- Prev by Date: Re: How to fix corrupt active directory...
- Next by Date: Re: Group Policy Error.
- Previous by thread: Re: Testing
- Next by thread: Re: Run script automatcally after addin a new user in AD
- Index(es):
Relevant Pages
|