Richard
Tue Nov 30 22:50:12 CST 2004
Michelle wrote:
> Id like to be able to generate a log file that pumps out all the steps
that
> a vbscript does.
> Similar to using a batch file >> log.txt
> And all errors.
>
> Can someone steer me in the right direction?
Hi,
The simplest approach would be run the script at a command prompt and echo
info to the screen. Then, you can redirect the output to a text file. For
example, the VBScript could be similar to:
=============
' Do something.
Wscript.Echo "Step 1 complete"
' Do something else.
Wscript.Echo "Step 2 complete"
=============
If the VBScript file is called Project.vbs, run this at a command prompt
with the following. The log is in the file Project.log
cscript //nologo Project.vbs > Project.log
A more advanced method would be to use the FileSystemObject to write lines
to a text file. I have a sample logon VBScript that logs information this
way linked here:
http://www.rlmueller.net/Logon5.htm
The procedure in brief is:
=============
' Bind to FileSystemObject.
Set objFSO = CreateObject("Scripting.FileSystemObject")
' Open a log file for write access.
' This creates the file if it does not exist.
Set objLogFile = objFSO.OpenTextFile("c:\MyFolder\MyFile.Log", 8, True, 0)
' Write to the log file.
objLogFile.WriteLine "Step 1 complete at " & Now
' Write another line to the file.
objLogFile.WriteLine "Step 2 complete at " & Now
' Be sure to close the file.
objLogFile.Close
=============
Errors will not get redirected to a file with either method. However, you
could trap anticipated errors, using "On Error Resume Next", "On Error GoTo
0", and "If Err.Number <> 0 Then". For example, extending the second snippet
above:
=============
objLogFile.WriteLine "Starting Step 3"
' Trap possible error.
On Error Resume Next
Set objUser = GetObject("LDAP://cn=JUser,ou=Sales,dc=MyDomain,dc=com")
If Err.Number <> 0 Then
' Attempt to bind objUser failed. Restore normal error handling.
On Error GoTo 0
objLogFile.WriteLine "Error - failed to bind objUser"
Else
' Bind succeeded. Restore normal error handling.
On Error GoTo 0
objLogFile.WriteLine "Step 3 complete"
End If
=============
You could also use Wscript.Echo above. I try to turn off normal error
handling only for the statements I anticipate could raise an error. Look up
documentation on the Err object. Err.Number will be 0 if there is no error
condition.
--
Richard
Microsoft MVP Scripting and ADSI
Hilltop Lab web site -
http://www.rlmueller.net
--