简体   繁体   English

使用Powershell或SSIS将EventViewer错误日志导入到SQL Server表中

[英]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. 首先,您可以轻松地在网络上找到答案,但是由于我也想尝试这一点,因此这里是经过测试的结果。

Create table 建立表格

You can make something like this: 您可以进行如下操作:

CREATE TABLE WinLogs (
    EntryType VARCHAR(255),
    [Source] VARCHAR(255),
    [Message] VARCHAR(4000),
    TimeGenerated datetime
)

Create package 创建包

  • Add 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(数据库时间戳记)

在此处输入图片说明

  • Add below code to this component: 将以下代码添加到此组件:

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));
    }
}
  • And the last step is to add SQL Server Destination component (also can be OLE DB Destination ) 最后一步是添加SQL Server Destination组件(也可以是OLE DB Destination
  • Create Connection manager and select table where to insert data (look if mapping is correct). 创建连接管理器,然后选择要在其中插入数据的表(查看映射是否正确)。

在此处输入图片说明

  • Run this package and you will see that data are inserted. 运行该程序包,您将看到已插入数据。

The source is from this example Eventlog as a source 来源来自此示例Eventlog作为来源

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM