Announcement

Collapse
No announcement yet.

Output to a txt

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Output to a txt

    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
    Last edited by Rems; 20th September 2010, 22:50. Reason: added code-tags

  • #2
    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.

    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
    If this is your entire code you should add some close statements:
    Code:
    objConnection.close
    and you should set all your objects to nothing
    Code:
     
    Set objConnection = nothing
    Set objCommand   = nothing
    The simple rule is if you do a Set command in the beginning it should have a Set = nothing command at the end.

    Hobie

    Comment


    • #3
      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

      Comment

      Working...
      X