用脚本管理服务器日志

       您是否经常碰到在登录系统时“系统日志已满,请清空日志”的警告消息呢?如果您的是终端服务器还可能造成终端用户无法登录等问题,即使将日志存储空间加大也会有满的一天,但是作为一名专业的管理员,服务器的日志又是那么的重要,就算你没时间全部阅读一遍,最起码也要保留一份作为日后排错用。那么以下的小脚本就可以帮你解除这些烦恼:
 
1:备份并清空系统日志,复制代码另存为SysEvtLog_Clear_bak_C.vbs
 
 
  1. On Error Resume Next  
  2. strYear = Year(Date)  
  3. strMonth = Month(Date)  
  4. If strMonth < 10 Then strMonth = 0 & strMonth  
  5. strDay = Day(Date)  
  6. If strDay < 10 Then strDay = 0 & strDay  
  7. strDate = strYear & strMonth & strDay '得到当前日期  
  8. strLogfileName = strYear & strMonth & strDay  
  9. Set objFSO = CreateObject("Scripting.FileSystemObject")  
  10. strPath = "c:\"  '日志保存位置  
  11. strComputer = "." 
  12. Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate,(Backup)}!\\" & strComputer & "\root\cimv2")  
  13. Set colLogFiles = objWMIService.ExecQuery("Select * from Win32_NTEventLogFile where LogFileName='System'")  
  14. For Each objLogfile in colLogFiles  
  15.  objLogFile.BackupEventLog(strPath & "System(" & strLogfileName & ").evt")  
  16.  objLogFile.ClearEventLog()  
  17. Next 
 
 
2:备份并清空应用程序日志,复制代码另存为AppEvtLog_Clear_bak_C.vbs
 
 
  1. On Error Resume Next  
  2. strYear = Year(Date)  
  3. strMonth = Month(Date)  
  4. If strMonth < 10 Then strMonth = 0 & strMonth  
  5. strDay = Day(Date)  
  6. If strDay < 10 Then strDay = 0 & strDay  
  7. strDate = strYear & strMonth & strDay '得到当前日期  
  8. strLogfileName = strYear & strMonth & strDay  
  9. Set objFSO = CreateObject("Scripting.FileSystemObject")  
  10. strPath = "c:\" '日志保存位置  
  11. strComputer = "." 
  12. Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate,(Backup)}!\\" & strComputer & "\root\cimv2")  
  13. Set colLogFiles = objWMIService.ExecQuery("Select * from Win32_NTEventLogFile where LogFileName='Application'")  
  14. For Each objLogfile in colLogFiles  
  15.  objLogFile.BackupEventLog(strPath & "Application(" & strLogfileName & ").evt")  
  16.  objLogFile.ClearEventLog()  
  17. Next 
 
 
3:备份并清空安全日志,复制代码另存为SecEvtLog_Clear_bak_C.vbs
 
 
  1. On Error Resume Next  
  2. strYear = Year(Date)  
  3. strMonth = Month(Date)  
  4. If strMonth < 10 Then strMonth = 0 & strMonth  
  5. strDay = Day(Date)  
  6. If strDay < 10 Then strDay = 0 & strDay  
  7. strDate = strYear & strMonth & strDay '得到当前日期  
  8. strLogfileName = strYear & strMonth & strDay  
  9. Set objFSO = CreateObject("Scripting.FileSystemObject")  
  10. strPath = "c:\" '日志保存位置  
  11. strComputer = "." 
  12. Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate,(Backup, Security)}!\\" & strComputer & "\root\cimv2")  
  13. Set colLogFiles = objWMIService.ExecQuery("Select * from Win32_NTEventLogFile where LogFileName='Security'")  
  14. For Each objLogfile in colLogFiles  
  15.  objLogFile.BackupEventLog(strPath & "Security(" & strLogfileName & ").evt")  
  16.  objLogFile.ClearEventLog()  
  17. Next 
 
将上面的脚本设置计划任务,每月最后一天运行即可(自行定义备份频率)。
其中代码 objLogFile.ClearEventLog() 为清空日志,如果只需要备份而不清空日志的话删除此行即可。


本文转自yangye1985 51CTO博客,原文链接:http://blog.51cto.com/yangye/265076,如需转载请自行联系原作者

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值