VBScript写入Excel而不是写入

VBScript写入Excel而不是写入,excel,vbscript,Excel,Vbscript,我试图完成的是在windows系统日志中搜索多台计算机的事件代码41(意外关机),然后将其写入每台计算机的每个实例的excel文件中 我没有收到任何错误,但excel文件中从未写入任何内容。我设置了一个echo来确保它到达了循环的正确部分(确实如此!),并设置了一个文本条目来查看变量是否有错误(它没有写入)。在这一点上,我不知所措 ' https://technet.microsoft.com/library/ee176684.aspx ' http://blogs.technet.com/b

我试图完成的是在windows系统日志中搜索多台计算机的事件代码41(意外关机),然后将其写入每台计算机的每个实例的excel文件中

我没有收到任何错误,但excel文件中从未写入任何内容。我设置了一个echo来确保它到达了循环的正确部分(确实如此!),并设置了一个文本条目来查看变量是否有错误(它没有写入)。在这一点上,我不知所措

' https://technet.microsoft.com/library/ee176684.aspx

' http://blogs.technet.com/b/heyscriptingguy/archive/2009/04/06/how-can-i-check-my-event-logs.aspx

' http://stackoverflow.com/questions/21738159/extracting-error-logs-from-windows-event-viewer

Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("H:\Chris Created Stuffs\Windows Stuffs\check_error_41.xlsx")
objExcel.Visible = False

i = 1
x = 0
'On error resume next
'This is the code that will read the computer names off of the 
'appropriate spreadhseet

Do Until objExcel.Cells(i, 1).Value = ""
    ReDim Preserve strPC(x)
    strPC(x) = objExcel.Cells(i, 1).Value
    i = i + 1
    x = x + 1
Loop

'And this is the code that will write the success or failure
'data in the Excel spreadsheet

Set objSheet1 = objWorkbook.sheets("Missed")
Set objSheet2 = objWorkbook.sheets("Sheet1")

'Set objSheet1 = objExcel.ActiveWorkbook.Worksheets(1)
'Set objSheet2 = objExcel.ActiveWorkbook.Worksheets(2)

f = 1
m = 1


'Set obj = CreateObject("Scripting.FileSystemObject")
For Each strPC In strPC

Set objWMIService = GetObject("winmgmts:\\" & strPC & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_NTLogEvent WHERE LogFile='System'")

    If Err.Number <> 0 Then
        'objSheet1.Add
        objSheet1.Cells(f, 1).Value = strPC
        objSheet1.Cells(f, 2).Value = err.number
        f = f + 1
        Err.clear
    Else
        For Each objEvent in colItems
            If objEvent.EventCode = 41 Then
                'writeLog "Event Code: " & objEvent.EventCode
                'writeLog "Event Identifier: " & objEvent.EventIdentifier
                'writeLog "Logfile: " & objEvent.Logfile
                'writeLog "Message: " & objEvent.Message
                'writeLog "Record Number: " & objEvent.RecordNumber
                'writeLog "Source Name: " & objEvent.SourceName
                'writeLog "Time Generated: " & objEvent.TimeGenerated
                'writeLog "Time Written: " & objEvent.TimeWritten
                'objSheet2.Add
                objSheet2.Cells(m,1).Value = strPC
                objSheet2.Cells(m,2).Value = objEvent.EventCode
                objSheet2.Cells(m,3).Value = objEvent.EventIdentifier
                objSheet2.Cells(m,4).Value = objEvent.Logfile
                objSheet2.Cells(m,5).Value = objEvent.Message
                objSheet2.Cells(m,6).Value = objEvent.RecordNumber
                objSheet2.Cells(m,7).Value = objEvent.SourceName
                objSheet2.Cells(m,8).Value = objEvent.TimeGenerated
                objSheet2.Cells(m,9).Value = objEvent.TimeWritten
                objSheet2.Cells(m,10).Value = "Listen!"
                m = m + 1
                wscript.echo "We Got One!!!!" 
            Else
                m = m + 1
            End If
        Next
    Err.clear
    End If
Next


objExcel.ActiveWorkbook.Save 
objExcel.Quit
wscript.echo "Done"
'https://technet.microsoft.com/library/ee176684.aspx
' http://blogs.technet.com/b/heyscriptingguy/archive/2009/04/06/how-can-i-check-my-event-logs.aspx
' http://stackoverflow.com/questions/21738159/extracting-error-logs-from-windows-event-viewer
设置objExcel=CreateObject(“Excel.Application”)
设置objWorkbook=objExcel.Workbooks.Open(“H:\Chris Created Stuffs\Windows Stuffs\check\u error\u 41.xlsx”)
objExcel.Visible=False
i=1
x=0
'出现错误时,请继续下一步
'这是将从中读取计算机名称的代码
“适当的展销会”
直到objExcel.Cells(i,1).Value=“”
ReDim保存strPC(x)
strPC(x)=objExcel.Cells(i,1).Value
i=i+1
x=x+1
环
'这是编写成功或失败的代码
'Excel电子表格中的数据
设置objSheet1=objWorkbook.sheets(“缺失”)
设置objSheet2=objWorkbook.sheets(“Sheet1”)
'设置objSheet1=objExcel.ActiveWorkbook.Worksheets(1)
'设置objSheet2=objExcel.ActiveWorkbook.Worksheets(2)
f=1
m=1
'Set obj=CreateObject(“Scripting.FileSystemObject”)
对于strPC中的每个strPC
设置objWMIService=GetObject(“winmgmts:\\”&strPC&“\root\cimv2”)
Set colItems=objWMIService.ExecQuery(“从Win32\u NTLogEvent中选择*,其中LogFile='System'))
如果错误号为0,则
'objSheet1.Add
objSheet1.Cells(f,1).Value=strPC
objSheet1.Cells(f,2).Value=err.number
f=f+1
清楚
其他的
对于每个对象,以colItems为单位
如果objEvent.EventCode=41,则
“writeLog”事件代码:“&objEvent.EventCode”
“writeLog”事件标识符:“&objEvent.EventIdentifier”
“writeLog”日志文件:“&objEvent.Logfile”
“writeLog”消息:“&objEvent.Message”
“writeLog”记录编号:“&objEvent.RecordNumber”
“writeLog”源名称:“&objEvent.SourceName”
“writeLog”生成的时间:“&objEvent.TimeGenerated
“writeLog”写入的时间:“&objEvent.TimeWrited”
'objSheet2.Add
objSheet2.Cells(m,1).Value=strPC
objSheet2.Cells(m,2).Value=objEvent.EventCode
单元格(m,3).Value=objEvent.EventIdentifier
单元格(m,4).Value=objEvent.Logfile
objSheet2.Cells(m,5).Value=objEvent.Message
objSheet2.Cells(m,6).Value=objEvent.RecordNumber
objSheet2.Cells(m,7).Value=objEvent.SourceName
objSheet2.Cells(m,8).Value=objEvent.TimeGenerated
objSheet2.Cells(m,9).Value=objEvent.timewrited
objSheet2.Cells(m,10).Value=“Listen!”
m=m+1
echo“我们有一个!!!!”
其他的
m=m+1
如果结束
下一个
清楚
如果结束
下一个
objExcel.ActiveWorkbook.Save
退出
wscript.echo“完成”

我认为您的主要问题是忽略和。在此代码中:

Do Until objExcel.Cells(i, 1).Value = ""
    ReDim Preserve strPC(x)
    strPC(x) = objExcel.Cells(i, 1).Value
    i = i + 1
    x = x + 1
Loop
实际上没有从工作表中提取任何内容。我不得不猜测一下实际的来源,但语法是正确的;您可能需要对自己的工作表布局进行特定调整

Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True 'False
Set objWorkbook = objExcel.Workbooks.Open("H:\Chris Created Stuffs\Windows Stuffs\check_error_41.xlsx")

i = 1
x = 0
'On error resume next
'This is the code that will read the computer names off of the appropriate spreadhseet

Do Until objWorkbook.Worksheets(1).Cells(i, 1).Value = ""
    ReDim Preserve strPCs(x)
    strPCs(x) = objWorkbook.Worksheets(1).Cells(i, 1).Value
    'msgbox objWorkbook.Worksheets(1).Cells(i, 1).Value
    i = i + 1
    x = x + 1
Loop

'And this is the code that will write the success or failure data in the Excel spreadsheet

Set objSheet1 = objWorkbook.Worksheets("Missed")
Set objSheet2 = objWorkbook.Worksheets("Sheet1")
f = 1
m = 1

For Each strPC In strPCs

    Set objWMIService = GetObject("winmgmts:\\" & strPC & "\root\cimv2")
    Set colItems = objWMIService.ExecQuery("Select * from Win32_NTLogEvent WHERE LogFile='System'")

    If Err.Number <> 0 Then
        'objSheet1.Add
        objSheet1.Cells(f, 1).Value = strPC
        objSheet1.Cells(f, 2).Value = err.number
        f = f + 1
        Err.clear
    Else
        For Each objEvent in colItems
            If objEvent.EventCode = 41 Then
                'writeLog "Event Code: " & objEvent.EventCode
                'writeLog "Event Identifier: " & objEvent.EventIdentifier
                'writeLog "Logfile: " & objEvent.Logfile
                'writeLog "Message: " & objEvent.Message
                'writeLog "Record Number: " & objEvent.RecordNumber
                'writeLog "Source Name: " & objEvent.SourceName
                'writeLog "Time Generated: " & objEvent.TimeGenerated
                'writeLog "Time Written: " & objEvent.TimeWritten
                'objSheet2.Add
                objSheet2.Cells(m, 1).Value = strPC
                objSheet2.Cells(m, 2).Value = objEvent.EventCode
                objSheet2.Cells(m, 3).Value = objEvent.EventIdentifier
                objSheet2.Cells(m, 4).Value = objEvent.Logfile
                objSheet2.Cells(m, 5).Value = objEvent.Message
                objSheet2.Cells(m, 6).Value = objEvent.RecordNumber
                objSheet2.Cells(m, 7).Value = objEvent.SourceName
                objSheet2.Cells(m, 8).Value = objEvent.TimeGenerated
                objSheet2.Cells(m, 9).Value = objEvent.TimeWritten
                objSheet2.Cells(m, 10).Value = "Listen!"
                m = m + 1
                'wscript.echo "We Got One!!!!" 
            'do not add to m on no-write; it only creates blank rows
            End If
        Next
        Err.clear
    End If
Next


'objWorkbook.Close True
'objExcel.Quit
wscript.echo "Done"
Set-objExcel=CreateObject(“Excel.Application”)
objExcel.Visible=True'False
设置objWorkbook=objExcel.Workbooks.Open(“H:\Chris Created Stuffs\Windows Stuffs\check\u error\u 41.xlsx”)
i=1
x=0
'出现错误时,请继续下一步
'这是从相应spreadhseet读取计算机名称的代码
直到objWorkbook.Worksheets(1).Cells(i,1).Value=“”
ReDim保留STRPC(x)
strPCs(x)=obj工作簿。工作表(1)。单元格(i,1)。值
'msgbox objWorkbook.Worksheets(1).Cells(i,1).Value
i=i+1
x=x+1
环
'这是将成功或失败数据写入Excel电子表格的代码
设置objSheet1=objWorkbook.Worksheets(“缺失”)
设置objSheet2=objWorkbook.Worksheets(“Sheet1”)
f=1
m=1
对于strPC中的每个strPC
设置objWMIService=GetObject(“winmgmts:\\”&strPC&“\root\cimv2”)
Set colItems=objWMIService.ExecQuery(“从Win32\u NTLogEvent中选择*,其中LogFile='System'))
如果错误号为0,则
'objSheet1.Add
objSheet1.Cells(f,1).Value=strPC
objSheet1.Cells(f,2).Value=err.number
f=f+1
清楚
其他的
对于每个对象,以colItems为单位
如果objEvent.EventCode=41,则
“writeLog”事件代码:“&objEvent.EventCode”
“writeLog”事件标识符:“&objEvent.EventIdentifier”
“writeLog”日志文件:“&objEvent.Logfile”
“writeLog”消息:“&objEvent.Message”
“writeLog”记录编号:“&objEvent.RecordNumber”
“writeLog”源名称:“&objEvent.SourceName”
“writeLog”生成的时间:“&objEvent.TimeGenerated
“writeLog”写入的时间:“&objEvent.TimeWrited”
'objSheet2.Add
objSheet2.Cells(m,1).Value=strPC
objSheet2.Cells(m,2).Value=objEvent.EventCode
单元格(m,3).Value=objEvent.EventIdentifier
单元格(m,4).Value=objEvent.Logfile
objSheet2.Cells(m,5).Value=objEvent.Message
objSheet2.Cells(m,6).Value=objEvent.RecordNumber
objSheet2.Cells(m,7).Value=objEvent.SourceName
objSheet2.Cel