?
Solved

Redirect output to a file with VBScript

Posted on 2009-05-01
10
Medium Priority
?
1,264 Views
Last Modified: 2012-05-06
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
0
Comment
Question by:jskfan
  • 4
  • 3
  • 3
10 Comments
 
LVL 67

Accepted Solution

by:
sirbounty earned 1000 total points
ID: 24279351
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
 
LVL 6

Assisted Solution

by:Mark Pavlak
Mark Pavlak earned 1000 total points
ID: 24280047
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
 
LVL 67

Expert Comment

by:sirbounty
ID: 24280160
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Expert Comment

by:Mark Pavlak
ID: 24280447
Kool That you,  Sometimes I am in a hurry and write on the fly leaving me carless of redability.  I apperciate it
0
 

Author Comment

by:jskfan
ID: 24304317
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
 
LVL 67

Expert Comment

by:sirbounty
ID: 24304370
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
 

Author Comment

by:jskfan
ID: 24304402
it works fine now. the only thing missing is to tweak the script to show the computers in containers that are not OUs
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 24304439
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
 

Author Comment

by:jskfan
ID: 24305029
No problem Sirbounty.....
Welcome back
0
 
LVL 6

Expert Comment

by:Mark Pavlak
ID: 24306114
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article we want to have a look at the directory attributes which are used by Microsoft to store the so called Security Identifiers (SID). These SIDs plays an important role in delegating and granting permissions and in authentication of trus…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses
Course of the Month13 days, 13 hours left to enroll

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question