Storage News
Security News
Networking News 
FREE NEWSLETTERS
search
 

follow us on Twitter

internet.commerce
Be a Commerce Partner















internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers


Products
 morespace (Hammer Storage)
 Futronic Biometrics (Futronic Technology Company Ltd)
 Secunia Network Software Inspector (NSI) (Secunia)
 UDgateway (UDcast SA)
 FlashDisk HyperSAN (Winchester Systems Inc)
 NCP Secure Enterprise (Network Communications Products Engineering GmbH)
» Enterprise IT Planet » Resources » Win Scripts

Backup and Clear Event Logs

October 6, 2008

Email Print Digg This Add to del.icio.us

Want to share a script? Click here to contribute!

Author:
Shane Boudreaux

Platform:
Windows

Type:
Win

Description:
This script will back up and save your app and sys event logs, based on a list of servers.

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.

Email Print Digg This Add to del.icio.us

Win Scripts Archives





internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Whitepapers and eBooks

Microsoft Technology Overview: Virtualization for Windows
IBM IT Innovation Article: Using Software to Turn Your Business Green
Symantec Whitepaper: Converging System and Data Protection for Complete Disaster Recovery
Avaya Article: Developing Speech Grammars That Rock, Part 1: Best Practices
Intel Whitepaper: Comparing Two- and Four-Socket Platforms for Server Virtualization
IBM Solutions Brief: Go Green With IBM System xTM And Intel
HP eBook: Simplifying SQL Server Management
Go Parallel Article: PDC 2008 in Review
IBM IT Innovation Article: IT Needs More Involvement in Green Initiatives
Avaya Article: Communication-Enabled Mashups: Empowering Both Business Owners and IT
Intel Whitepaper: Building a Real-World Model to Assess Virtualization Platforms
PDF: Intel Centrino Duo Processor Technology with Intel Core2 Duo Processor
  Microsoft Article: Build and Run Virtual Machines with Hyper-V Server 2008
Go Parallel Article: Q&A; with a TBB Junkie
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
IBM eBook: The Pros and Cons of Outsourcing
Internet.com eBook: Best Practices for Developing a Web Site
IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
Avaya eBook: IP Application Development - Explore Device, Media and Call Control
IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
Symantec Whitepaper: Comprehensive Backup and Recovery of VMware Virtual Infrastructure
MORE WHITEPAPERS, EBOOKS, AND ARTICLES