Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista - Write To File

Reply
 
Old 09-24-2009   #1 (permalink)
gimolu


 
 

Write To File

HI,

I'm having some trouble writing a script that will gather info. from a bunch
of servers in my network.
I made a new one with the problem that I have; here's the source:



'BOF------------------------------------------------------------------------------------
Option Explicit

dim EventViewerAppDESDE
EventViewerAppDESDE = CDate("09-24-2009")

EventViewerApp(".")


function EventViewerApp(hostname)

dim objWMIService, objEvent, colEvents, VarStrEvtApp

VarStrEvtApp = VarStrEvtApp & "Category;" & vbtab & _
"Computer Name;" & vbtab & "Event #;" & vbtab & _
"Message;" & vbtab & "Record #;" & vbtab & _
"Source;" & vbtab & "Time Written;" & vbtab & _
"Event Type;" & vbtab & "User;" & _
vbCrLf

Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & hostname & "\root\cimv2")
Set colEvents = objWMIService.ExecQuery _
("Select * from Win32_NTLogEvent Where TimeWritten >= '" _
& EventViewerAppDESDE & "' and Logfile = 'Application'")


For Each objEvent in colEvents
VarStrEvtApp = VarStrEvtApp & objEvent.Category & ";" & vbtab & _
"Computer Name: " & objEvent.ComputerName & ";" & vbtab & _
"Event Code: " & objEvent.EventCode & ";" & vbtab & _
"Message: " & objEvent.Message & ";" & vbtab & _
"Record Number: " & objEvent.RecordNumber & ";" & vbtab & _
"Source Name: " & objEvent.SourceName & ";" & vbtab & _
"Time Written: " & objEvent.TimeWritten & ";" & vbtab & _
"Event Type: " & objEvent.Type & ";" & vbtab & _
"User: " & objEvent.User & ";" & vbtab & _
objEvent.LogFile
Next

'VarStrEvtApp = "small words..." ' IF I UNCOMMENT THIS AND COMMENT THE LINE
BELLOW IT WILL WORK
WriteToFile hostname & "_EventApp.csv", VarStrEvtAppend function



function WriteToFile(nome, OQue)
dim VarStrFSO, VarStrEscreve

Set VarStrFSO = CreateObject("Scripting.FileSystemObject")
Set VarStrEscreve = VarStrFSO.OpenTextFile("d:\" & nome, 2, True) ' 2 -
overwrite
VarStrEscreve.WriteLine(OQue)
SET VarStrEscreve = NOTHING
SET VarStrFSO = NOTHINGend function

wscript.quit()
'EOF------------------------------------------------------------------------------------


It appears that when I get to much output I kind of get a buffer overflow
because I cant't write the output to a file using the WriteToFile()
function.
If less information is passed, no errors occur.

Can anyone help me find the source of the problem?!

Thanks.


LR


My System SpecsSystem Spec
Old 09-25-2009   #2 (permalink)
Richard Mueller [MVP]


 
 

Re: Write To File

The script you posted got a bit messed up. The carriage returns before both
"End Function" statements got removed.

I would suggest writting each line to the file separately, rather than
concatenating all the lines and writting in one operation. I notice you end
the first line with vbCrLf, but none of the others. The WriteLine method add
a vbCrLf after each line it writes.

To write the file one line at a time, you will need to open the file outside
your function WriteToFile. Actually, I see no reason to use this function. I
would suggest the following (not tested):
=============
Option Explicit

dim EventViewerAppDESDE
dim VarStrFSO, VarStrEscreve, VarStrHost

VarStrHost = "."

Set VarStrFSO = CreateObject("Scripting.FileSystemObject")
Set VarStrEscreve = VarStrFSO.OpenTextFile("d:\" & VarStrHost &
"_EventApp.csv, 2, True)

EventViewerAppDESDE = CDate("09-24-2009")

Call EventViewerApp(VarStrHost)

sub EventViewerApp(hostname)
dim objWMIService, objEvent, colEvents, VarStrEvtApp

VarStrEvtApp = VarStrEvtApp & "Category;" & vbtab & _
"Computer Name;" & vbtab & "Event #;" & vbtab & _
"Message;" & vbtab & "Record #;" & vbtab & _
"Source;" & vbtab & "Time Written;" & vbtab & _
"Event Type;" & vbtab & "User;"
VarStrEscreve.WriteLine(VarStrEvtApp)

Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & hostname & "\root\cimv2")
Set colEvents = objWMIService.ExecQuery _
("Select * from Win32_NTLogEvent Where TimeWritten >= '" _
& EventViewerAppDESDE & "' and Logfile = 'Application'")

For Each objEvent in colEvents
VarStrEvtApp = VarStrEvtApp & objEvent.Category & ";" & vbtab & _
"Computer Name: " & objEvent.ComputerName & ";" & vbtab & _
"Event Code: " & objEvent.EventCode & ";" & vbtab & _
"Message: " & objEvent.Message & ";" & vbtab & _
"Record Number: " & objEvent.RecordNumber & ";" & vbtab & _
"Source Name: " & objEvent.SourceName & ";" & vbtab & _
"Time Written: " & objEvent.TimeWritten & ";" & vbtab & _
"Event Type: " & objEvent.Type & ";" & vbtab & _
"User: " & objEvent.User & ";" & vbtab & _
objEvent.LogFile
VarStrEscreve.WriteLine(VarStrEvtApp)
Next

end sub
===========
Note also that the csv file name will be invalid if you use "." for the host
name. I don't know why you separate your fields with both ";" and vbTab.
Seems like you could just use ";". Also, since you have a header line, why
concatenate a description to each field? Shouldn't the resulting csv file be
something that can be read into a spreadsheet program?

--
Richard Mueller
MVP Directory Services
Hilltop Lab - http://www.rlmueller.net
--

"gimolu" <gimolu@newsgroup> wrote in message
news:OZcwkzWPKHA.220@newsgroup
Quote:

> HI,
>
> I'm having some trouble writing a script that will gather info. from a
> bunch of servers in my network.
> I made a new one with the problem that I have; here's the source:
>
>
>
> 'BOF------------------------------------------------------------------------------------
> Option Explicit
>
> dim EventViewerAppDESDE
> EventViewerAppDESDE = CDate("09-24-2009")
>
> EventViewerApp(".")
>
>
> function EventViewerApp(hostname)
>
> dim objWMIService, objEvent, colEvents, VarStrEvtApp
>
> VarStrEvtApp = VarStrEvtApp & "Category;" & vbtab & _
> "Computer Name;" & vbtab & "Event #;" & vbtab & _
> "Message;" & vbtab & "Record #;" & vbtab & _
> "Source;" & vbtab & "Time Written;" & vbtab & _
> "Event Type;" & vbtab & "User;" & _
> vbCrLf
>
> Set objWMIService = GetObject("winmgmts:" _
> & "{impersonationLevel=impersonate}!\\" & hostname & "\root\cimv2")
> Set colEvents = objWMIService.ExecQuery _
> ("Select * from Win32_NTLogEvent Where TimeWritten >= '" _
> & EventViewerAppDESDE & "' and Logfile = 'Application'")
>
>
> For Each objEvent in colEvents
> VarStrEvtApp = VarStrEvtApp & objEvent.Category & ";" & vbtab & _
> "Computer Name: " & objEvent.ComputerName & ";" & vbtab & _
> "Event Code: " & objEvent.EventCode & ";" & vbtab & _
> "Message: " & objEvent.Message & ";" & vbtab & _
> "Record Number: " & objEvent.RecordNumber & ";" & vbtab & _
> "Source Name: " & objEvent.SourceName & ";" & vbtab & _
> "Time Written: " & objEvent.TimeWritten & ";" & vbtab & _
> "Event Type: " & objEvent.Type & ";" & vbtab & _
> "User: " & objEvent.User & ";" & vbtab & _
> objEvent.LogFile
> Next
>
> 'VarStrEvtApp = "small words..." ' IF I UNCOMMENT THIS AND COMMENT THE
> LINE BELLOW IT WILL WORK
> WriteToFile hostname & "_EventApp.csv", VarStrEvtAppend function
>
>
>
> function WriteToFile(nome, OQue)
> dim VarStrFSO, VarStrEscreve
>
> Set VarStrFSO = CreateObject("Scripting.FileSystemObject")
> Set VarStrEscreve = VarStrFSO.OpenTextFile("d:\" & nome, 2, True) ' 2 -
> overwrite
> VarStrEscreve.WriteLine(OQue)
> SET VarStrEscreve = NOTHING
> SET VarStrFSO = NOTHINGend function
>
> wscript.quit()
> 'EOF------------------------------------------------------------------------------------
>
>
> It appears that when I get to much output I kind of get a buffer overflow
> because I cant't write the output to a file using the WriteToFile()
> function.
> If less information is passed, no errors occur.
>
> Can anyone help me find the source of the problem?!
>
> Thanks.
>
>
> LR

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
how to get powershell to write to event log or write a log file? PowerShell
When using System.IO.FileStream, I write 8 bytes, then seek to the start of the file, does the 8 bytes get flushed on seek and the buffer become a readbuffer at that point instead of being a write buffer? .NET General
Can out-file write to the screen PowerShell
Write to a file VB Script
canno write file Vista security


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46