Re: export ad querie to txt



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
--


.



Relevant Pages

  • Re: Help Splitting a String
    ... Const ADS_PROPERTY_DELETE = 4 ... Set objConnection = CreateObject ... MsgBox homedir - This bit is OK ... MsgBox newservername ...
    (microsoft.public.scripting.vbscript)
  • Help Splitting a String
    ... Const ADS_PROPERTY_DELETE = 4 ... Set objConnection = CreateObject ... MsgBox homedir - This bit is OK ... MsgBox newservername ...
    (microsoft.public.scripting.vbscript)
  • VBScript,Getting the Managers Display Name
    ... I have a very basic script that I created to pull data from all AD user ... Const ADS_SCOPE_SUBTREE = 2 ... Set objConnection = CreateObject ... Set objRange = objExcel.Selection.EntireColumn ...
    (microsoft.public.windows.server.scripting)
  • RE: export account details from an OU
    ... Const ADS_SCOPE_SUBTREE = 2 ... Set objConnection = CreateObject ... Set objCommand = CreateObject ... objConnection.Provider = "ADsDSOObject" ...
    (microsoft.public.windows.server.active_directory)
  • Re: export account details from an OU
    ... Const ADS_SCOPE_SUBTREE = 2 ... Set objConnection = CreateObject ... Set objCommand = CreateObject ... objConnection.Provider = "ADsDSOObject" ...
    (microsoft.public.windows.server.active_directory)