1740 PST | 01AUG'08 | Joe
This is a script that I wrote to handle event logs. This script will
back them up, copy them to a network share, and optionally clear the
system event logs for any machine it is run on.
logArchive.vbs
'#==============================================================================
'#==============================================================================
'# SCRIPT.........: logArchive.vbs
'# AUTHOR.........: Joe Glessner
'# EMAIL..........: jglessner@gmail.com
'# VERSION........: 1.0
'# DATE...........: 30JUL07
'# COPYRIGHT......: 2007, Joe-IT.com
'# LICENSE........: Freeware
'# REQUIREMENTS...:
'#
'# DESCRIPTION....: This script backs up all of the event logs on the
'# designated computer, to the specified file server.
'# Optionally this script can also clear the event logs once
'# they are archived.
'#
'# NOTES..........:
'#
'# CUSTOMIZE......: Make changes to the configuration section to customize for
'# your environment.
'#==============================================================================
'# REVISED BY.....:
'# EMAIL..........:
'# REVISION DATE..:
'# REVISION NOTES.:
'#
'#==============================================================================
'#==============================================================================
'**Start Encode**
'#==============================================================================
'# START OF SCRIPT
'#==============================================================================
'Option Explicit
'On Error Resume Next
'#--------------------------------------------------------------------------
'# SCRIPT CONFIGURATION SECTION
'#--------------------------------------------------------------------------
'# OPTIONS:
'# strComputer = The name of the computer that generated the
'# event logs (e.g. fs01 - use "." for the local
'# machine.
'# objDir2 = The destination directory on the file server.
'# clearEVTLogs = "No" does not clear the event logs. "Yes"
'# will clear the event logs once the current
'# logs are archived.
'#--------------------------------------------------------------------------
Dim strComputer, objDir2
strComputer = "."
objDir2 = "\\Server\DOCS$\EventLogs\" & strComputer
clearEVTLogs = "No"
'#--------------------------------------------------------------------------
'# Define Remaining Variables
'#--------------------------------------------------------------------------
Dim current: current = Now
Dim strDateStamp: strDateStamp = dateStamp(current)
Dim objDir1: objDir1 = "\\" & strComputer & "\c$\EVT"
'#--------------------------------------------------------------------------
'# Ensure that the scratch directory exists on the source computer.
'#--------------------------------------------------------------------------
Set filesys=CreateObject("Scripting.FileSystemObject")
If Not filesys.FolderExists(objDir1) Then
createDir(objDir1)
End If
'#--------------------------------------------------------------------------
'# Ensure that the destination directory exists on the file server.
'#--------------------------------------------------------------------------
If Not filesys.FolderExists(objDir2) Then
createDir(objDir2)
End If
'#--------------------------------------------------------------------------
'# Make backups of the event logs to the scratch directory.
'#--------------------------------------------------------------------------
strPath = objDir2 & "\"
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate, (Backup, Security)}!\\" _
& strComputer & "\root\cimv2")
Set colLogFiles = objWMIService.ExecQuery _
("Select * from Win32_NTEventLogFile")
For Each objLogfile In colLogFiles
strCopyFile = strDateStamp & "_" & strComputer & "_" _
& objLogFile.LogFileName & ".evt"
strBackupFile = "c:\EVT\" & strDateStamp & "_" _
& strComputer & "_" & objLogFile.LogFileName & ".evt"
strBackupLog = objLogFile.BackupEventLog _
(strBackupFile)
'WScript.Echo objLogFile.LogFileName & " backed up to " _
' & strBackupFile
'#----------------------------------------------------------------------
'# Copy the event logs to the file server.
'#----------------------------------------------------------------------
Call copyAFile(objDir1, strPath, strCopyFile)
'#----------------------------------------------------------------------
'# Clear the event logs, or not.
'#----------------------------------------------------------------------
If clearEVTLogs = "Yes" Then
objLogFile.ClearEventLog()
End If
Next
'#==============================================================================
'# SUBROUTINES/FUNCTIONS/CLASSES
'#==============================================================================
'#--------------------------------------------------------------------------
'# FUNCTION.........: dateStamp(ByVal dt)
'# PURPOSE..........: Generate an 8-character date stamp from the current
'# VBScript date.
'# ARGUMENTS........: dt = The date stamp to convert.
'# EXAMPLE..........: Dim current: current = Now
'# WScript.Echo dateStamp(current)
'# REQUIREMENTS.....:
'# NOTES............: The above example will produce output of 20080730 if
'# run on 07/30/08.
'#--------------------------------------------------------------------------
Function dateStamp(ByVal dt)
Dim y, m, d
y = Year(dt)
m = Month(dt)
If Len(m) = 1 Then m = "0" & m
d = Day(dt)
If Len(d) = 1 Then d = "0" & d
dateStamp = y & m & d
End Function
'#--------------------------------------------------------------------------
'# FUNCTION........: copyAFile()
'# ARGUMENTS.......: strScourceFolder = The folder containing the files to
'# be copied.
'# strTargetFolder = The Destination Folder
'# strFileName = The name and file extension of the file
'# to be copied.
'# PURPOSE.........: General purpose file copying function.
'# EXAMPLE.........: Wscript.Echo copyAFile("C:\", "\\Server\Share", _
'# & "fileName.txt")
'# NOTES...........: strSourceFolder folder must exist
'# strTargetFolder folder must exist
'# strFileName file must exist in strSourceFolder folder
'#--------------------------------------------------------------------------
Function copyAFile( Byval strSourceFolder, Byval strTargetFolder, _
Byval strFileName)
Dim objFSO, booOverWrite, strResult
Set objFSO = CreateObject( "Scripting.FileSystemObject")
If objFSO.FileExists( strSourceFolder & "\" & strFileName) _
And UCase( strSourceFolder) <> UCase( strTargetFolder) Then
If objFSO.FolderExists( strTargetFolder) Then
Else
strResult = "The destination folder does not exist!"
'copyAFile = strResult
Exit Function
End If
If objFSO.FileExists( strTargetFolder & "\" & strFileName) Then
strResult = "The file exists, overwritten"
booOverWrite = vbTrue
Else
strResult = "The file does not exist, created"
booOverWrite = vbFalse
End If
objFSO.CopyFile strSourceFolder & "\" _
& strFileName, strTargetFolder & "\", booOverWrite
Else
strResult = "The source file does not exist, or " _
& "identical Source and Target folders!"
End If
'copyAFile = strResult
End Function
'#--------------------------------------------------------------------------
'# FUNCTION.......: createDir(strDir)
'# ARGUMENTS......: strDir = UNC path of the directory to create.
'# PURPOSE........: Creates directories.
'# EXAMPLE........: createDir("c:\WSH_TEST\")
'# createDir("c:\WSH_TEST\" & "Files\")
'# NOTES..........: If creating a subdirectory of a directory that does
'# not exist, the parent directory must be created
'# first, as shown in the example.
'#--------------------------------------------------------------------------
Function createDir(strDir)
Set filesys=CreateObject("Scripting.FileSystemObject")
Set objFSO = CreateObject("Scripting.FileSystemObject")
If Not filesys.FolderExists(strDir) Then
Set objFolder = objFSO.CreateFolder(strDir)
End If
End Function
'#==============================================================================
'# END OF FILE
'#==============================================================================
This script provides code formatting, as as well as a couple of
extra features:
- Line 29 can be uncommented to allow the Microsoft Script Encoder
to encode the script (this is NOT encryption!).
- Line 34 can be uncommented to set "Option Explicit" globally.
- Line 35 can be uncommented to set "On Error Resume Next" globally.
- The formatting characters used in this script can be used in
conjunction with this script
to retrieve all of the comments from the script, and write them into a text file.
I use the last feature to create "psuedo-documentation" for my
scripts after they are complete. It helps me keep track of which
scripts do what, and why.
Lines 34 and 35 deal with error handling in VBScript. If you are not
familiar with the concept, it is probably best to leave them
commented out for now.