Special thanks to
ebgreen for helping me clean this up a bit, if anyone knows how to do this without the "On Error Resume Next" line, by all means go for it. I am still reading up on it at the moment
This script is run from a "syslog" server, where all the file backups will be deposited. A .txt file will be needed that contains the IPs of all the remote machines to collect logs from.
The script will create a folder based on the current date to deposit event logs. Then, a list of IPs is read from a txt file and the script will connect to each, in turn, to perform several tasks.
If the connection is made:
1. Create a folder c:\logs
2. Back up four event logs, using current date and machine IP as part of the name
3. Clear the logs
4. Move the logs to the syslog server
Else:
1. Create/append an error log with data concerning any connection issues encountered
The script should repeat the process for every IP in the list. Once the tasks are completed, the script will Wscript.Echo one of two messages, depending on whether connection errors were encountered or not.
I imagine someone could make this fancy by using code to pull machines from AD if they are so inclined or could use an array if there are just a few machines to collect logs from.
*Currently, I am working on a script to pull IIS logs from remote machines as well, but it's giving me some "twist and turns."
Hope this is useful to someone!
NOTE: Make sure the script works before removing the ' from all four "objLogFile.ClearEventLog()" lines! On Error Resume Next
'=== variables that assist in naming files and folders.
dtmThisDay = Day(Now)
dtmThisMonth = Month(Now)
dtmThisYear = Year(Now)
strDate = dtmThisYear & "_" & dtmThisMonth & "_" & dtmThisDay
strBackupName = strDate & "_"
'=== creates folder based on current date to deposit event logs.
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CreateFolder("E:\logs\evtlogs\" & strDate)
'=== Opens list of computers for remote connection.
Const ForReading = 1
Set objFile = objFSO.OpenTextFile("C:\scripts\computers.txt")
'=== A Loop that will either connect to remote machines from the list, create a backup folder, back up and clear event logs
'=== and then move the backups to the syslog server OR generate an error log with data concerning the failed connection.
Do Until objFile.AtEndOfStream
strComputer = objFile.ReadLine
Set objWMIService = GetObject _
("winmgmts:\\" & strComputer & "\root\cimv2:Win32_Process")
'25
'=== creates c:\logs on remote machine
If Err.Number = 0 Then
errReturn = objWMIService.Create ("cmd.exe /c md c:\logs", Null, Null, intProcessID)
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate,(Backup, Security)}!\\" & _
strComputer & "\root\cimv2")
Set colLogFiles = objWMIService.ExecQuery _
("SELECT * FROM Win32_NTEventLogFile where LogFileName='Application'")
For Each objLogfile in colLogFiles
objLogFile.BackupEventLog("c:\logs\" & strBackupName & strComputer & "_application.evt")
'objLogFile.ClearEventLog()
Next
Set colLogFiles = objWMIService.ExecQuery _
("SELECT * FROM Win32_NTEventLogFile where LogFileName='Security'")
For Each objLogfile in colLogFiles
objLogFile.BackupEventLog("c:\logs\" & strBackupName & strComputer & "_security.evt")
'objLogFile.ClearEventLog()
Next
'47
Set colLogFiles = objWMIService.ExecQuery _
("SELECT * FROM Win32_NTEventLogFile where LogFileName='System'")
For Each objLogfile in colLogFiles
objLogFile.BackupEventLog("c:\logs\" & strBackupName & strComputer & "_system.evt")
'objLogFile.ClearEventLog()
Next
Set colLogFiles = objWMIService.ExecQuery _
("SELECT * FROM Win32_NTEventLogFile where LogFileName='Tumbleweed'")
For Each objLogfile in colLogFiles
objLogFile.BackupEventLog("c:\logs\" & strBackupName & strComputer & "_tumbleweed.evt")
'objLogFile.ClearEventLog()
Next
'=== moves event logs to syslog server
strRemoteFile = "\\syslog server IP\e$\logs\evtlogs\" & strDate
objFSO.MoveFile "\\" & strComputer & "\C$\logs\*.evt", strRemoteFile
Else
bError = true
Const ForAppending = 8
'67
'=== Creates connection error log
Set objLogFile = objFSO.OpenTextFile("E:\logs\evtlogs\" & strDate & "_Error Log.txt", ForAppending, True)
objLogFile.Write "No connection to " & strComputer & ": " & Err.Description & "."
objLogFile.Writeline
Err.Clear
End If
Loop
objLogFile.Close
objFile.Close
If bError <> 0 Then
Wscript.Echo "Logfile backup is complete. Please check the error logs."
Else
Wscript.Echo "Logfile backup is complete."
End If
bError = Nothing
<message edited by mtw999 on Tuesday, August 23, 2011 2:35 AM>