Looking for Automation of Maintenance tasks, I've had to write a script that'll run as a scheduled task to Purge Log Files, but only logs older than a specified number of days. Googling to find if that kind of script already exists I've found a nice blog with some example I've used to start. I've worked on this script to adapt it to my needs and came with two new scripts. These new scripts have been designed to run as Scheduled Tasks so they're logging everything within the Application EventLog. You can set the file extension so they're easily adaptable to purge other kind of files (like backups for example). The first one has been designed to clean the "C:\WINDOWS\system32\LogFiles" Root. It'll parse the 1-Level Subfolders and delete every log file that meets the specified criteria. That's very useful for a IIS Server hosting a Default Web Interface installation (or another Website) because It'll clean every IIS Site Logs (if stored in the default location) but also the "HTTPERR" Logs. As this kind of script could be dangerous, the Delete Action has been commented and you'll have to uncomment it when you have tested the script is working correctly in your environment (because basically It'll log but not delete). Purge Logs Files within 1-Level Subfolders (Case "C:\WINDOWS\system32\LogFiles") '--------------------------------------------------------------------------------- Option Explicit On Error Resume Next Dim intDaysOld, strObjTopFolderPath, strLogFIleSuffix, ObjFS, ObjTopFolder, objShell, tmpObjFileName Dim ObjLogFolder, ObjW3SvcFolder, ObjSubFolder, ObjLogFile, ObjFile intDaysOld = 5 'Number of days to retain on the server strObjTopFolderPath = "C:\WINDOWS\system32\LogFiles" 'The location of your log files Subfolders strLogFileSuffix = ".log" 'The suffix of your log files Set ObjFS = CreateObject("Scripting.FileSystemObject") Set ObjTopFolder = ObjFS.GetFolder(strObjTopFolderPath) Set objShell = Wscript.CreateObject("Wscript.Shell") PurgeLogs() Sub PurgeLogs() WriteLog 0,"Purge Logs Script Starting with Root Folder: " & ObjTopFolder.Path For Each ObjLogFolder in ObjTopFolder.SubFolders WriteLog 0,"Purge Logs Script Parsing SubFolder: " & ObjLogFolder.name Set ObjSubFolder = ObjFS.GetFolder(ObjLogFolder) For each ObjLogFile in ObjSubFolder.files On Error Resume Next Err.Clear Set ObjFile = ObjFS.GetFile(ObjLogFile) If datediff("d",ObjFile.DateLastModified,Date()) > intDaysOld and lcase(right(ObjLogFile,len(strLogFileSuffix)))=strLogFileSuffix then tmpObjFileName = ObjFile.Name '***************************************************** 'DON'T UNCOMMENT THIS UNTIL YOU KNOW IT WORKS PROPERLY!!! 'ObjFile.Delete '***************************************************** If Not Err then WriteLog 8,"Purge Logs Script has Deleted SucessFully " & ObjSubFolder.name & "\" & tmpObjFileName Else WriteLog 16,"Purge Logs Script Error While Deleting " & ObjSubFolder.name & "\" & tmpObjFileName End If End If Set ObjFile = nothing Next Set ObjSubFolder = nothing Next Set ObjTopFolder = nothing Set ObjFS = nothing End Sub Sub WriteLog(evType,msg) on error resume next Err.Clear objShell.LogEvent evType, msg End Sub '--------------------------------------------------------------------------------- Purge Logs Files within a specified Folder (Case "C:\WINDOWS\system32\LogFiles\W3SVC1") This other script has been designed to delete Log Files only within a specified folder, but you can also use it to purge, for example, daily backups after one week or one month. '--------------------------------------------------------------------------------- Option Explicit On Error Resume Next Dim intDaysOld, strObjTopFolderPath, strLogFIleSuffix, ObjFS, ObjTopFolder, objShell, tmpObjFileName Dim ObjLogFolder, ObjW3SvcFolder, ObjSubFolder, ObjLogFile, ObjFile intDaysOld = 5 'Number of days to retain on the server strObjTopFolderPath = "C:\WINDOWS\system32\LogFiles" 'The location of your log files strLogFileSuffix = ".log" 'The suffix of your log files Set ObjFS = CreateObject("Scripting.FileSystemObject") Set ObjTopFolder = ObjFS.GetFolder(strObjTopFolderPath) Set objShell = Wscript.CreateObject("Wscript.Shell") PurgeLogs() Sub PurgeLogs() WriteLog 0,"Purge Logs Script Starting with Root Folder: " & ObjTopFolder.Path For each ObjLogFile in ObjTopFolder.files On Error Resume Next Err.Clear Set ObjFile = ObjFS.GetFile(ObjLogFile) If datediff("d",ObjFile.DateLastModified,Date()) > intDaysOld and lcase(right(ObjLogFile,len(strLogFileSuffix)))=strLogFileSuffix then tmpObjFileName = ObjFile.Name '***************************************************** 'DON'T UNCOMMENT THIS UNTIL YOU KNOW IT WORKS PROPERLY!!! 'ObjFile.Delete '***************************************************** If Not Err then WriteLog 8,"Purge Logs Script has Deleted SucessFully " & ObjTopFolder.name & "\" & tmpObjFileName Else WriteLog 16,"Purge Logs Script Error While Deleting " & ObjTopFolder.name & "\" & tmpObjFileName End If End If Set ObjFile = nothing Next Set ObjFS = nothing End Sub Sub WriteLog(evType,msg) on error resume next Err.Clear objShell.LogEvent evType, msg End Sub '--------------------------------------------------------------------------------- Purge Exports and Logs Files within specified Folders (Case Daily Exports) This other script has been designed to delete Exports (backups) AND Log Files only within specified folders. Initially it has been written to clear XenApp App Export Manager / XenApp Policies Export Manager Export Files and associated logs, but it can be easily adaptated to handle for example SQL dumps or any kind or backups / exports. '--------------------------------------------------------------------------------- Option Explicit On Error Resume Next Dim intDaysOld, strObjTopLogsFolderPath, strObjTopExportsFolderPath, strLogFIleSuffix, strExpFileSuffix, ObjFS, objShell, tmpObjFileName Dim ObjLogFolder, ObjW3SvcFolder, ObjTopLogsFolder, ObjTopExportsFolder, ObjLogFile, ObjExpFile, ObjFile intDaysOld = 30 'Number of days to retain on the server strObjTopExportsFolderPath = "\\MyServer\Exports" 'The location of your Exports files strObjTopLogsFolderPath = "\\MyServer\Exports\Logs" 'The location of your log files strExpFileSuffix = ".ctxtools" 'The suffix of your export files strLogFileSuffix = ".log" 'The suffix of your log files Set ObjFS = CreateObject("Scripting.FileSystemObject") Set ObjTopLogsFolder = ObjFS.GetFolder(strObjTopLogsFolderPath) Set ObjTopExportsFolder = ObjFS.GetFolder(strObjTopExportsFolderPath) Set objShell = Wscript.CreateObject("Wscript.Shell") PurgeExports() PurgeLogs() Sub PurgeExports() WriteLog 0,"Purge Exports Files Task Starting with Root Folder: " & ObjTopExportsFolder.Path For each ObjExpFile in ObjTopExportsFolder.files On Error Resume Next Err.Clear Set ObjFile = ObjFS.GetFile(ObjExpFile) If datediff("d",ObjFile.DateLastModified,Date()) > intDaysOld and lcase(right(ObjExpFile,len(strExpFileSuffix)))=strExpFileSuffix then tmpObjFileName = ObjFile.Name '***************************************************** 'DON'T UNCOMMENT THIS UNTIL YOU KNOW IT WORKS PROPERLY!!! 'ObjFile.Delete '***************************************************** If Not Err then WriteLog 8,"Purge Exports Files Task has Deleted SucessFully " & ObjTopExportsFolder.name & "\" & tmpObjFileName Else WriteLog 16,"Purge Exports Files Task Error While Deleting " & ObjTopExportsFolder.name & "\" & tmpObjFileName End If End If Set ObjFile = nothing Next End Sub Sub PurgeLogs() WriteLog 0,"Purge Export Logs Files Task Starting with Root Folder: " & ObjTopLogsFolder.Path For each ObjLogFile in ObjTopLogsFolder.files On Error Resume Next Err.Clear Set ObjFile = ObjFS.GetFile(ObjLogFile) If datediff("d",ObjFile.DateLastModified,Date()) > intDaysOld and lcase(right(ObjLogFile,len(strLogFileSuffix)))=strLogFileSuffix then tmpObjFileName = ObjFile.Name '***************************************************** 'DON'T UNCOMMENT THIS UNTIL YOU KNOW IT WORKS PROPERLY!!! 'ObjFile.Delete '***************************************************** If Not Err then WriteLog 8,"Purge Export Logs Files Task has Deleted SucessFully " & ObjTopLogsFolder.name & "\" & tmpObjFileName Else WriteLog 16,"Purge Export Logs Files Task Error While Deleting " & ObjTopLogsFolder.name & "\" & tmpObjFileName End If End If Set ObjFile = nothing Next End Sub Sub WriteLog(evType,msg) on error resume next Err.Clear objShell.LogEvent evType, msg End Sub '--------------------------------------------------------------------------------- |
No comments:
Post a Comment