首页 > 解决方案 > 使用 pywin32 获取过去 3 个月内事件日志中所有错误条目的来源、日期/时间和消息的列表

问题描述

编辑; 我原来的帖子对我的问题太模糊了,所以在完成我的任务一段时间后,我调整/修复了我的大部分代码。但是,我仍然无法让它工作。现在,当我从 cmd 提示符运行脚本时,我收到此消息...

Traceback (most recent call last):
  File "/PythonScriptsR2/autoadmin_a1_errors.py", line 45, in <module>
    log_handle_three = win32evtlog.OpenEventLog(computer,log_type_three)
pywintypes.error: (1722, 'OpenEventLogW', 'The RPC server is 
unavailable.')

这是我当前代码的样子(我在第 45 行及其附近写了一些注释,以便更容易找到);

import win32evtlog
import win32evtlogutil
import win32security
import win32con
import time
import winerror
import re
import string
import sys
import traceback

####################################################################
# All error entries for the last 3 months
########
#set date format
def date2sec(self,evt_date):
    '''
    convert '12/23/99 15:54:09' to seconds
    print '333333',evt_date
    '''
    regexp=re.compile('(.*)\\s(.*)')
    reg_result=regexp.search(evt_date)
    date = reg.result.group(1)
    time = reg_result.group(2)
    (mon,day,yr) = map(lambda x: sring.atoi(x),string.split(date,'/'))
    (hr,min,sec) = map(lambda x: sring.atoi(x),string.split(time,':'))
    tup = [yr,mon,day,hr,min,sec,0,0,0]
    sec = time.mktime(tup)
    return sec
################
#Initialize variables
flags_three=win32evtlog.EVENTLOG_BACKWARDS_READ|\
            win32evtlog.EVENTLOG_SEQUENTIAL_READ

    #Dictionary to convert event type into human readable form
evt_dict={win32con.EVENTLOG_AUDIT_FAILURE:'EVENTLOG_AUDIT_FAILURE',\
          win32con.EVENTLOG_AUDIT_SUCCESS:'EVENTLOG_AUDIT_SUCCESS',\
          win32con.EVENTLOG_INFORMATION_TYPE:'EVENTLOG_INFORMATION_TYPE',\
          win32con.EVENTLOG_WARNING_TYPE:'EVENTLOG_WARNING_TYPE',\
          win32con.EVENTLOG_ERROR_TYPE:'EVENTLOG_ERROR_TYPE'}

computer='bedrock'
log_type_three='System'
begin_sec=time.time()
begin_time=time.strftime('%H:%M:%S ',time.localtime(begin_sec))
############                 \/ Line 45 \/                   #############
log_handle_three=win32evtlog.OpenEventLog(computer,log_type_three) #line45
############                 /\ Line 45 /\                   #############
################
#Open the Event Log
print(log_type_three,' error events found from the last three months 
since:',begin_time)
try:
    ev_ents=1
    while ev_ents:
        ev_ents=win32evtlog.ReadEventLog(log_handle_three,flags_three, 0)
        for ev_obj in ev_ents:
#################
            #check if  the event is recent enough
            #checking data from the last three months
            date = ev_ent.TimeGenerated.Format()
            time = ev_ent.TimeGenerated.Format()
            seconds = date2sec(date)
            if seconds < begin_sec-7862400: break
#################
            #if the data is recent enough/from the correct time frame, 
            #print it  
            source=str(ev_obj.SourceName)
            message=str(win32evtlogutil.SafeFormatMessage(ev_obj,\ 
                                                          logtype))
            record = str(ev_obj.RecordNumber)
            print(string.join((record,source,date,\
                               time,message[0:15]),':'))
        if seconds < begin_sec-7862400: break #You need to get out of 
                                              #the while loop as well
        win32evtlog.CloseEventLog(log_handle_three)
except:
    print(traceback.print_exc(sys.exc_info()))

所以现在我的问题是,这个错误消息是什么意思,为什么我会收到这个错误消息,以及我可以做些什么来尝试修复它。

我认为他可能希望我们使用 windows powershell 来调用脚本,但是,当我这样做时,我得到了与在 cmd 提示符下所做的相同的错误消息,所以我不确定它是否有区别。

标签: pythonerror-handlingpywin32event-log

解决方案


出于某种原因,我认为它可能试图使用网络堆栈访问“基岩”,而不是仅仅从本地机器上获取它。我会确保“基岩”是您从 CLI 执行“主机名”时获得的实际计算机名称。

我使用“localhost”的计算机名称遇到了类似的问题,它在某些环境中无法正常工作并出现相同的错误。将该值更改为实际的本地计算机名称为我解决了这个问题。


推荐阅读