VBS VBScript EventLog EventID
Security Event Log를 이벤트 ID로 필터링해 원격 서버에 저장하고 로컬 서버에는 전체 이벤트 로그를 Export해 저장한다. 저장이 끝난 후 이벤트 로그를 비운다.
' ------------------------------------------------------------------
' ------------------------------------------------------------------
' --------- Filtering by Event ID (Comma Separated)
EventID = "528,529,540,4624,4625"
' --------- Local Store
LocalDir = "C:\SecurityEvent"
' --------- Destination
ServerShare = \\123.123.123.123\FileShare
UserName = "Domain\User"
Password = "Password"
' ------------------------------------------------------------------
' ------------------------------------------------------------------
' --------- Start Script
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate,(Security)}!\\.\root\cimv2")
Set FileSystem = WScript.CreateObject("Scripting.FileSystemObject")
Set NetworkObject = CreateObject("WScript.Network")
If Not FileSystem.FolderExists(LocalDir) Then
Createfolder = FileSystem.CreateFolder (LocalDir)
End If
EventIDFilter = ""
arrEventID = Split(EventID,",")
For Each EventID in arrEventID
EventIDFilter = EventIDFilter & "EventCode=" & Trim(EventID) & " OR "
Next
EventIDFilter = Left(EventIDFilter,Len(EventIDFilter)-4)
If Right(LocalDir,1) <> "\" Then
LocalDir = LocalDir & "\"
End If
If Right(ServerShare,1) <> "\" Then
ServerShare = ServerShare & "\"
End If
Set colOS = objWMIService.ExecQuery ("Select * From Win32_OperatingSystem")
For Each OSInfo in colOS
HostName = OSInfo.CSName
Next
Set colOS = Nothing
LogFileName = HostName & "-" & Year(Date) & Month(Date) & Day(Date) & "-" & "SecurityLog.csv"
LogFileFullPath = LocalDir & LogFileName
NetworkObject.MapNetworkDrive "", Left(ServerShare,Len(ServerShare)-1), False, UserName, Password
Set colLoggedEvents = objWMIService.ExecQuery ("Select * From Win32_NTLogEvent Where Logfile = 'Security' AND (" & EventIDFilter & ")")
Set oFile = FileSystem.CreateTextFile(LogFileFullPath, True)
oFile.WriteLine ("EventType,EventID,Date,Time,Description")
For Each objEvent in colLoggedEvents
strDateTime = DateAdd("h",9,WMIDateStringToDate(objEvent.TimeGenerated))
strDate = FormatDateTime(strDateTime,2)
strTime = FormatDateTime(strDateTime,3)
strDescription = ""
arrDescription = Split(objEvent.Message,vbCRLF)
For Each objDescription in arrDescription
If InStr(objDescription,"계정 이름:") OR InStr(objDescription,"로그온 유형:") OR InStr(objDescription,"워크스테이션 이름:") OR InStr(objDescription,"원본 네트워크 주소:") _
OR InStr(objDescription,"User Name:") OR InStr(objDescription,"Account Name:") OR InStr(objDescription,"Logon Type:") OR InStr(objDescription,"Workstation Name:") OR InStr(objDescription,"Source Network Address:") Then
strDescription = strDescription & Replace(objDescription,vbTab,"") & " "
End If
Next
oFile.WriteLine (objEvent.Type&","&objEvent.EventCode&","&strDate&","&strTime&","&strDescription)
Next
oFile.Close
Set colLoggedEvents = Nothing
If (FileSystem.FileExists(ServerShare & LogFileName)) Then
Set aFile = FileSystem.GetFile(ServerShare & LogFileName)
aFile.Delete
End If
FileSystem.MoveFile LogFileFullPath, ServerShare & LogFileName
NetworkObject.RemoveNetworkDrive Left(ServerShare,Len(ServerShare)-1), True, False
If (FileSystem.FileExists(LocalDir & HostName & "-" & Year(Date) & Month(Date) & Day(Date) & "-" & "SecurityLog.evt")) Then
Set bFile = FileSystem.GetFile(LocalDir & HostName & "-" & Year(Date) & Month(Date) & Day(Date) & "-" & "SecurityLog.evt")
bFile.Delete
End If
Set objWMIService2 = GetObject("winmgmts:{impersonationLevel=impersonate,(Backup)}!\\.\root\cimv2")
Set colLogFiles = objWMIService2.ExecQuery("Select * from Win32_NTEventLogFile Where LogFileName='Security'")
For Each objLogfile in colLogFiles
objLogFile.BackupEventLog(LocalDir & HostName & "-" & Year(Date) & Month(Date) & Day(Date) & "-" & "SecurityLog.evt")
objLogFile.ClearEventLog()
Next
Set colLogFiles = Nothing
Set objWMIService = Nothing
Set objWMIService2 = Nothing
Set NetworkObject = Nothing
Set FileSystem = Nothing
Function WMIDateStringToDate(dtmInstallDate)
WMIDateStringToDate = CDate(Mid(dtmInstallDate, 5, 2) & "/" & Mid(dtmInstallDate, 7, 2) & "/" & Left(dtmInstallDate, 4) & " " & Mid (dtmInstallDate, 9, 2) & ":" &Mid(dtmInstallDate, 11, 2) & ":" & Mid(dtmInstallDate, 13, 2))
End Function