Link to home
Start Free TrialLog in
Avatar of jskfan
jskfanFlag for Cyprus

asked on

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
ASKER CERTIFIED SOLUTION
Avatar of sirbounty
sirbounty
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

Kool That you,  Sometimes I am in a hurry and write on the fly leaving me carless of redability.  I apperciate it
Avatar of jskfan

ASKER

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
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?
Avatar of jskfan

ASKER

it works fine now. the only thing missing is to tweak the script to show the computers in containers that are not OUs
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....
Avatar of jskfan

ASKER

No problem Sirbounty.....
Welcome back
To go through an OU with subs you will need recursion to Get a container set the class you are looking for is "container"