[英]Import EventViewer error logs to sql server table using Powershell or SSIS
Is there is any way to import the error logs from event viewer to SQL table and schedule a job on daily basis. 有什么方法可以将错误日志从事件查看器导入到SQL表中,并每天安排作业。 I would like to use a powershell script or SSIS package.
我想使用Powershell脚本或SSIS包。
First you could easily find answer on the web, but as I wanted to try this too, here is tested result. 首先,您可以轻松地在网络上找到答案,但是由于我也想尝试这一点,因此这里是经过测试的结果。
You can make something like this: 您可以进行如下操作:
CREATE TABLE WinLogs (
EntryType VARCHAR(255),
[Source] VARCHAR(255),
[Message] VARCHAR(4000),
TimeGenerated datetime
)
Data Flow task
to package; Data Flow task
添加到包中; Inside package add Script Component
, where you should add 4 output columns (arrows show what to change): 在包内部添加
Script Component
,您应在其中添加4个输出列(箭头显示要更改的内容):
EntryType (string 255)
EntryType(字符串255)
Source (string 255)来源(字符串255)
Message (string 4000)消息(字符串4000)
TimeGenerated (database timestamp)TimeGenerated(数据库时间戳记)
public override void CreateNewOutputRows()
{
// Get all events from the Application(/System/Security) log from the local server (.)
EventLog myEvenLog = new EventLog("Application", ".");
// Create variable to store the entry
EventLogEntry myEntry;
// Loop trough all entries (oldest first)
for (int i = 0; i < myEvenLog.Entries.Count; i++)
{
// Get single entry
myEntry = myEvenLog.Entries[i];
// Add new records
this.Output0Buffer.AddRow();
// Fill columns
this.Output0Buffer.EntryType = myEntry.EntryType.ToString();
this.Output0Buffer.Source = myEntry.Source;
this.Output0Buffer.TimeGenerated = myEntry.TimeGenerated;
// Take a max of 4000 chars
this.Output0Buffer.Message = myEntry.Message.Substring(0, (myEntry.Message.Length > 4000 ? 3999 : myEntry.Message.Length - 1));
}
}
SQL Server Destination
component (also can be OLE DB Destination
) SQL Server Destination
组件(也可以是OLE DB Destination
) The source is from this example Eventlog as a source 来源来自此示例Eventlog作为来源
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.