|
|
Backup and Clear Event LogsOctober 6, 2008
Want to share a script? Click here to contribute! Author:
Platform: Type:
Description: Scroll down to view the script. Save and Clear Event Logs''================================== '' Back Up & Clear Event Logs on servers '' Author: Shane Boudreaux '' Start Date: 5/27/08 '' Last Modified: 5/27/08 ''================================== ''================================== '' GLOBAL DECLARES & CONSTANTS ''================================== Const SERVER_LIST = "c:servers.txt" Const LOG_PATH = "c:" Const ForReading = 1 Const ForAppending = 8 Dim objServers, objNoPingServers, tFormat tFormat = vbcrlf & "===========================" & vbcrlf Set objServers = CreateObject("Scripting.Dictionary") Set objNoPingServers = CreateObject("Scripting.Dictionary") ''=========================================================== ''================================== '' MAIN ENTRY POINT ''================================== ''On Error Resume Next fileName = InputBox("Enter name for saved EVT file") '' make sure has the *.EVT extension If UCase(Right(fileName, 4)) <> ".EVT" Then fileName = fileName & "*.EVT" End If '' get start time startTime = Now '' get servers from list getServers(SERVER_LIST) '' roll thru servers, backing up and clearing event logs servers = objServers.Keys For Each server in servers '' Application Event Log backupClearEventLog server, "Application", "\" & server & "d$EventLogsApplication", fileName '' System Event Log backupClearEventLog server, "System", "\" & server & "d$EventLogsSystem", fileName Next '' enum NO PING servers If objNoPingServers.Count > 0 Then noPings = objNoPingServers.Keys For Each noPing in noPings If noPing <> "" Then logText = logText & noPing & vbcrlf End If Next '' log NO PING servers logToFile LOG_PATH, "_NO-PING.txt", logText End If '' get stop time stopTime = Now totalSeconds = DateDiff("s", startTime, stopTime) totalMinutes = totalSeconds / 60 wscript.echo "Done!" & vbcrlf & "Servers Processed: " & objServers.Count _ & vbcrlf & "Time Elapsed (seconds): " & totalSeconds & vbcrlf & "Time Elapsed (minutes): " & totalMinutes ''================================== '' SUPPORTING SUBS & FUNCTIONS ''================================== ''================================== Private Sub getServers(list) '' create reference for server list file Set objFSO = CreateObject("Scripting.FileSystemObject") Set objServerList = objFSO.OpenTextFile(list, ForReading) Do While objServerList.AtEndOfStream <> True '' read server name server = Trim(objServerList.Readline) '' ensure server is pingable p = pingHost(server) If p = True Then '' add server to dictionary for processing objServers.Add server, server Else '' log NO PING servers objNoPingServers.Add server, server End If Loop '' close the server list file objServerList.Close End Sub ''================================== ''================================== Private Function backupClearEventLog(server, evtLog, savePath, saveFile) On Error Resume Next Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate,(Backup)}!\" & _ server & "rootcimv2") Set colLogFiles = objWMIService.ExecQuery _ ("Select * from Win32_NTEventLogFile where LogFileName=''" & evtLog & "''") For Each objLogfile in colLogFiles errBackupLog = objLogFile.BackupEventLog(savePath & saveFile) If errBackupLog <> 0 Then logToFile LOG_PATH, "EvtLogErrors.txt", tFormat & "Server: " & server & vbcrlf & "Event Log: " & evtLog & vbcrlf & "EVENT LOG NOT Backed Up or Cleared" & tFormat Else objLogFile.ClearEventLog() logText = server & " SUCCESSFULLY Cleared / Backed Up Event Logs as " & saveFile '' log successes to file logToFile LOG_PATH, "EvtSuccess.txt", logText End If Next End Function ''================================== ''================================== Private Sub logToFile(logPath, filename, text) On Error Resume Next '' Determine if file exists Set objFSO = CreateObject("Scripting.FileSystemObject") If objFSO.FileExists(logPath & "" & filename) = False Then Set objFile = objFSO.CreateTextFile(logPath & "" & filename) objFile.Write(text & vbcrlf) objFile.Close Else '' APPEND to Log file Set objFile = objFSO.OpenTextFile(logPath & "" & filename, ForAppending) objFile.Write(text & vbcrlf) objFile.Close End If End Sub ''================================== ''================================== Private Function pingHost(server) Set objShell = CreateObject("WScript.Shell") Set objExec = objShell.Exec("ping -n 1 -w 500 " & server) strPingResults = LCase(objExec.StdOut.ReadAll) If InStr(strPingResults, "reply from") Then pingHost = True Else pingHost = False End If End Function ''================================== Disclaimer: We hope that the information in these pages is valuable to you. Your use of the information contained in these pages, however, is at your sole risk. All information on these pages is provided "as -is", without any warranty, whether express or implied, of its accuracy, completeness, fitness for a particular purpose, title or non-infringement, and none of the third-party products or information mentioned in the work are authored, recommended, supported or guaranteed by me. I shall not be liable for any damages you may sustain by using this information, whether direct, indirect, special, incidental or consequential, even if it has been advised of the possibility of such damages.
|
|
|
|
|