Redirect output to a file with VBScript

I have this script below that retrieve  the computers in each OU of my domain
I would like to add filesystemobject code to put the output to the file instead of the screen.
any help?

Thanks


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 ADsPath FROM 'LDAP://dc=mydomain,dc=com' WHERE " & _
        "objectCategory='organizationalUnit'"  

Set objRecordSet = objCommand.Execute

objRecordSet.MoveFirst

Do Until objRecordSet.EOF
    Set objOU = GetObject(objRecordSet.Fields("ADsPath").Value)
    Wscript.Echo objOU.distinguishedName

    objOU.Filter = Array("Computer")
   
    For Each objItem in objOU
        Wscript.Echo "  " & objItem.CN
    Next

    Wscript.Echo
    Wscript.Echo
    objRecordSet.MoveNext
Loop
jskfanAsked:
Who is Participating?
 
sirbountyConnect With a Mentor Commented:
Sure...
On Error Resume Next
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objFile : Set objFile = objFSO.CreateTextFile("C:\MyOutput.txt")
 
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 ADsPath FROM 'LDAP://dc=mydomain,dc=com' WHERE " & _
        "objectCategory='organizationalUnit'"  
 
Set objRecordSet = objCommand.Execute
 
objRecordSet.MoveFirst
 
Do Until objRecordSet.EOF
    Set objOU = GetObject(objRecordSet.Fields("ADsPath").Value)
    objFile.WriteLine objOU.distinguishedName
 
    objOU.Filter = Array("Computer")
    
    For Each objItem in objOU
        objFile.WriteLine "  " & objItem.CN
    Next
 
    objFile.WriteLine
    objRecordSet.MoveNext
Loop
objFile.Close

Open in new window

0
 
Mark PavlakConnect With a Mentor Commented:
I did not look at the other respsone until after I coded it.  It is basically excatly what I did.  I would like to ask sirbounty though about his ObjFSO and ObjFile declorations.  I notice your dim / set statment is on one line with a : as a seprator, is this technique applicable to any type of dim?  ie could I do this?
dim a : a=1 ?  I know that that was a little off toipic but I was curious.  Also I noticed that when he writes is line out that all of them will statrt with white space is that what you wanted?

Dim ofso,oOutFile
Const ADS_SCOPE_SUBTREE = 2
Set ofso = CreateObject("Scripting.FileSystemObject")
Set oOutFile = ofso.CreateTextFile("c:\results.txt",True)
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 ADsPath FROM 'LDAP://dc=mydomain,dc=com' WHERE " & _
        "objectCategory='organizationalUnit'"  
 
Set objRecordSet = objCommand.Execute
 
objRecordSet.MoveFirst
 
Do Until objRecordSet.EOF
    Set objOU = GetObject(objRecordSet.Fields("ADsPath").Value)
    Wscript.Echo objOU.distinguishedName
 
    objOU.Filter = Array("Computer")
    
    For Each objItem in objOU
        Wscript.Echo "  " & objItem.CN
        oOutFile.WriteLine objItem.CN
    Next
 
    Wscript.Echo
    Wscript.Echo
    objRecordSet.MoveNext
Loop
oOutFile.Close

Open in new window

0
 
sirbountyCommented:
Yes - you can include multiple commands on a single line...this single-line of code is fully functional, though not advised because of it's lack of readability:

I just typically keep my declarations and assignments on the same line (just personal preference really)...
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject") : Dim objFile : Set objFile = objFSO.CreateTextFile("F:\mytest.txt") : objFile.WriteLine "Test" : objFile.Close

Open in new window

0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
Mark PavlakCommented:
Kool That you,  Sometimes I am in a hurry and write on the fly leaving me carless of redability.  I apperciate it
0
 
jskfanAuthor Commented:
I have an OU before the Domain Controllers OU and has compuers in it, I don't know why the script skips it and doesn't show the computers in it.
By the way it would be better if the script shows computers that are in containers too, instead of OUs only.
Sorry I should have mentioned that first time. I relaized that we have some containers that are not OUs and have computers inside.
Thanks
0
 
sirbountyCommented:
Since this question is about redirecting output, I think that's best left to a new question...
Is the redirecting of the output resolved for you?
0
 
jskfanAuthor Commented:
it works fine now. the only thing missing is to tweak the script to show the computers in containers that are not OUs
0
 
sirbountyCommented:
And that should really be placed in a new question...
EE prefers that multiple questions not be asked in the same thread as it devalues the PAQ (question/answer database).  
For instance, someone looking on how to search multiple OUs & containers for computers would not locate that information in this thread, were it answered here....
0
 
jskfanAuthor Commented:
No problem Sirbounty.....
Welcome back
0
 
Mark PavlakCommented:
To go through an OU with subs you will need recursion to Get a container set the class you are looking for is "container"
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.