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 AlbertA 9 years, 4 months ago.
-
AuthorPosts
-
August 12, 2010 at 11:20 am #150618
hi guys, i need to output to a txt file this vbs script
Code:On Error Resume Next
Const ADS_SCOPE_SUBTREE = 2Set objConnection = CreateObject(“ADODB.Connection”)
Set objCommand = CreateObject(“ADODB.Command”)
objConnection.Provider = “ADsDSOObject”
objConnection.Open “Active Directory Provider”
Set objCommand.ActiveConnection = objConnectionobjCommand.Properties(“Page Size”) = 1000
objCommand.Properties(“Searchscope”) = ADS_SCOPE_SUBTREEobjCommand.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.distinguishedNameobjOU.Filter = Array(“Computer”)
For Each objItem in objOU
Wscript.Echo ” ” & objItem.CN
NextWscript.Echo
Wscript.Echo
objRecordSet.MoveNext
Loopthanks for your help
August 12, 2010 at 11:57 am #349649Re: 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 nothingSet 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
August 12, 2010 at 10:12 pm #376950Re: 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 -
AuthorPosts
You must be logged in to reply to this topic.