![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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 Specs![]() |
![]() |
| 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 | |||