How do I go about getting output to a log file?

Re: Write WMI query to file? by James

James
Wed Sep 13 15:18:48 CDT 2006


Hi rtm,
You can use the FileSystemObject to create or open a text file for
logging. You can read more about the FileSystemObject here:

http://www.microsoft.com/technet/scriptcenter/guide/sas_scr_xfxi.mspx?mfr=true

You can also try this LogIt function that I often use when I need to
create a text file log. The function takes two parameters. The first
is the path to the log file as a string, and the second is the line of
text you wish to have logged as a string. If the log file doesn't
already exist it will be created. The function will return a value of
True if the log file was successfully created/opened and written to or
it will return False if not.


--------------------------------

LogIt "C:\Temp\log.txt", "This is a line of text to be logged."

Function LogIt(strLogFile,strLine)
'Accepts a log file name and a line of text as strings
'Creates the log file if it doesn't exist and appends
'the line of text to the file
'Returns TRUE if writing line to file was successful,
'FALSE otherwise
Dim fso, file
Const ForAppending = 8
On Error Resume Next
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(strLogFile) = False Then
Set file = fso.CreateTextFile(strLogFile)
Else
Set file = fso.OpenTextFile(strLogFile,ForAppending)
End If
If Err.Number = 0 Then
file.WriteLine(strLine)
file.Close
LogIt = True
Else
LogIt = False
End If
End Function

--------------------------------


Hope this helps,


James Garringer





rtm wrote:
> How do I go about getting output to a log file?


Re: Write WMI query to file? by Richard

Richard
Thu Sep 14 21:51:12 CDT 2006

Hi,

Also, if the script outputs to the console, you can redirect the output to a
text file. For example, if MyScript.vbs echos output to the screen, you can
run this at a command prompt with a command similar to:

cscript //nologo MyScript.vbs > report.txt

The "//nologo" optional parameter suppresses logo information. The output is
redirected to the file report.txt. It is best if scripts use "Wscript.Echo"
to output to the screen.

--
Richard
Microsoft MVP Scripting and ADSI
Hilltop Lab - http://www.rlmueller.net

"James Garringer" <jgarringer@gmail.com> wrote in message
news:1158178728.004480.72330@e3g2000cwe.googlegroups.com...
>
> Hi rtm,
> You can use the FileSystemObject to create or open a text file for
> logging. You can read more about the FileSystemObject here:
>
> http://www.microsoft.com/technet/scriptcenter/guide/sas_scr_xfxi.mspx?mfr=true
>
> You can also try this LogIt function that I often use when I need to
> create a text file log. The function takes two parameters. The first
> is the path to the log file as a string, and the second is the line of
> text you wish to have logged as a string. If the log file doesn't
> already exist it will be created. The function will return a value of
> True if the log file was successfully created/opened and written to or
> it will return False if not.
>
>
> --------------------------------
>
> LogIt "C:\Temp\log.txt", "This is a line of text to be logged."
>
> Function LogIt(strLogFile,strLine)
> 'Accepts a log file name and a line of text as strings
> 'Creates the log file if it doesn't exist and appends
> 'the line of text to the file
> 'Returns TRUE if writing line to file was successful,
> 'FALSE otherwise
> Dim fso, file
> Const ForAppending = 8
> On Error Resume Next
> Set fso = CreateObject("Scripting.FileSystemObject")
> If fso.FileExists(strLogFile) = False Then
> Set file = fso.CreateTextFile(strLogFile)
> Else
> Set file = fso.OpenTextFile(strLogFile,ForAppending)
> End If
> If Err.Number = 0 Then
> file.WriteLine(strLine)
> file.Close
> LogIt = True
> Else
> LogIt = False
> End If
> End Function
>
> --------------------------------
>
>
> Hope this helps,
>
>
> James Garringer
>
>
>
>
>
> rtm wrote:
>> How do I go about getting output to a log file?
>