VB / C#通过查看EventData

时间:2016-04-27 15:06:05

标签: c# vb.net event-log domaincontroller

我试图让特定的事件日志包含来自DC的安全审核失败的用户名,在powershell中我可以通过以下方式轻松完成此操作:

变量的位置如下:$ DC =“MyDomainController”和$ user =“jdoe”

Get-WinEvent -ComputerName $DC -FilterHashtable @{Logname='Security';Keywords='4503599627370496';Data=$user} -MaxEvents 4 | Format-List -Property ID, TimeCreated, MachineName, Message

这将从我正在查看的DC中提取安全审核失败的4个事件日志和人的用户名,但是我无法在vb.net中找到或重现此类行为,我一直在搜索页面在过去的几天里,想出很多关于DC的所有日志,但没有过滤掉,任何帮助或指导都会很棒,谢谢!

1 个答案:

答案 0 :(得分:0)

我能够通过使用xpath查看自定义日志查询来找到答案,我在C#中做了以下操作,但同样可以在VB中应用,
Domaincontroller.text =您正在查找的域控制器:
Username.text =要查找的AD用户名←Statustextbox =我将所有日志转到文本框进行阅读,但您可以执行类似console.writeline的操作

private void LookupLogs_Click(object sender, EventArgs e)
    {
        Statustextbox.Clear();
        string query = "<QueryList>" +
                       "  <Query Id=\"0\" Path=\"Security\">" +
                       "    <Select Path=\"Security\">" +
                       "        *[System[band(Keywords,4503599627370496)]] and *[EventData[Data[@Name='TargetUserName'] and (Data='" + Username.Text + "')]]" +
                       "    </Select>" +
                       "  </Query>" +
                       "</QueryList>";
        EventLogSession session = new EventLogSession(DomainController.Text);
        EventLogQuery evntquery = new EventLogQuery("Security", PathType.LogName, query);
        evntquery.Session = session;
        try
        {
            EventLogReader logreader = new EventLogReader(evntquery);
            DisplayEventAndLogInformation(logreader);
        }
        catch (Exception ex)
        {
            MessageBox.Show("An exception occured: " + ex.Message);
        }
    }

private void DisplayEventAndLogInformation(EventLogReader logReader)
    {
        for (EventRecord eventInstance = logReader.ReadEvent();
            null != eventInstance; eventInstance = logReader.ReadEvent())
        {
            Statustextbox.AppendText(Environment.NewLine + Environment.NewLine);
            Statustextbox.AppendText("---------------------------------------------------------------------------------------------------------------------------------------------------------------" + Environment.NewLine);
            Statustextbox.AppendText("Event ID: " + eventInstance.Id + Environment.NewLine);
            Statustextbox.AppendText("Publisher: " + eventInstance.ProviderName + Environment.NewLine);

            try
            {
                Statustextbox.AppendText("Description: " + eventInstance.FormatDescription() + Environment.NewLine);
            }
            catch (EventLogException ex)
            {
                Statustextbox.AppendText("An exception was thrown: " + ex.Message + Environment.NewLine);
            }
            EventLogRecord logRecord = (EventLogRecord)eventInstance;
            Statustextbox.AppendText(Environment.NewLine);
            Statustextbox.AppendText("Container Event Log: " + logRecord.ContainerLog + Environment.NewLine);
        }
    }