Output to a txt

Home Forums Scripting Windows Script Host Output to a txt

This topic contains 2 replies, has 3 voices, and was last updated by Avatar AlbertA 9 years, 4 months ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • Avatar
    OKley
    Member
    #150618

    hi guys, i need to output to a txt file this vbs script

    Code:
    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=contoso,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

    thanks for your help

    Avatar
    Hobie
    Member
    #349649

    Re: Output to a txt

    I simply changed you wscript.echo lines to write to a file instead. I did not modify any of your code and I did not run your code, so I have not tested it.

    On Error Resume Next
    Const ADS_SCOPE_SUBTREE = 2
    Set objFSO = CreateObject(“Scripting.FileSystemObject”)
    set txtfile= objFSO.CreateTextFile(“C:somefile.txt”)
    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=contoso,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
    txtfile.writeline objItem.CN
    Next
    objRecordSet.MoveNext
    Loop
    txtfile.close
    set txtfile = nothing
    set objfso = nothing
    [/CODE]

    If this is your entire code you should add some close statements:
    [CODE]objConnection.close[/CODE]
    and you should set all your objects to nothing
    [CODE]
    Set objConnection = nothing
    Set objCommand = nothing
    [/CODE]

    The simple rule is if you do a Set command in the beginning it should have a Set = nothing command at the end.

    Hobie[CODE]
    On Error Resume Next
    Const ADS_SCOPE_SUBTREE = 2
    Set objFSO = CreateObject(“Scripting.FileSystemObject”)
    set txtfile= objFSO.CreateTextFile(“C:somefile.txt”)
    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=contoso,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
    txtfile.writeline objItem.CN
    Next
    objRecordSet.MoveNext
    Loop
    txtfile.close
    set txtfile = nothing
    set objfso = nothing
    [/CODE]

    If this is your entire code you should add some close statements:
    objConnection.close[/CODE]
    and you should set all your objects to nothing
    [CODE]
    Set objConnection = nothing
    Set objCommand = nothing
    [/CODE]

    The simple rule is if you do a Set command in the beginning it should have a Set = nothing command at the end.

    Hobie[CODE]objConnection.close[/CODE]
    and you should set all your objects to nothing

    Set objConnection = nothing
    Set objCommand = nothing
    [/CODE]

    The simple rule is if you do a Set command in the beginning it should have a Set = nothing command at the end.

    Hobie[CODE]
    Set objConnection = nothing
    Set objCommand = nothing
    [/CODE]

    The simple rule is if you do a Set command in the beginning it should have a Set = nothing command at the end.

    Hobie

    Avatar
    AlbertA
    Member
    #376950

    Re: Output to a txt

    Thanks for your help Hobie
    After change some lines this is the code in the way that i´d like it:

    On Error Resume Next
    Const ADS_SCOPE_SUBTREE = 2
    Set objFSO = CreateObject(“Scripting.FileSystemObject”)
    set txtfile= objFSO.CreateTextFile(“C:file.txt”)
    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=contoso,dc=com’ WHERE ” & _
    “objectCategory=’organizationalUnit'”
    Set objRecordSet = objCommand.Execute
    objRecordSet.MoveFirst
    Do Until objRecordSet.EOF
    Set objOU = GetObject(objRecordSet.Fields(“ADsPath”).Value)
    txtfile.writeline objOU.distinguishedName
    objOU.Filter = Array(“Computer”)
    For Each objItem in objOU
    txtfile.writeline ” ” & objItem.CN
    Next
    txtfile.writeline
    tstfile.writeline
    objRecordSet.MoveNext
    Loop
    txtfile.close
    set txtfile = nothing
    set objfso = nothing
    Set objCommand = nothing
    Set objConnection = nothing
    objConnection.close

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.