2016-02-13 99 views
2

我試圖完成的是在Windows系統日誌中搜索多臺計算機的事件代碼41(意外關機),然後將其寫入每個計算機的每個實例的Excel文件中。VBScript寫入Excel不寫

我沒有收到任何錯誤,但沒有寫入excel文件。我設置了一個回聲以確保它到達了循環的正確部分(它的確如此!),並且我設置了一個文字入口來查看變量是否有錯誤(它沒有寫入)。此時,我不知所措。

' 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" 
+0

你確定'objEvent.EventCode'是數字嗎? '41 <>「41」' – Jeeped

+0

您是否嘗試過將objExcel.Visible設置爲True並逐步執行代碼,以便您可以查看哪些工作簿是您正在保存的活動工作簿? 既然你知道你想保存objSheet2,也許你應該只是調用該工作表而不是活動工作表。 – nfloria

回答

1

我認爲您的主要問題是忽視了Workbook ObjectWorksheet Object。在此代碼中:

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" 

我註釋掉的代碼行,以使隱藏的保存ASN,以便你可以觀察的過程中關閉Excel應用程序對象。一旦您對流程感到滿意,請取消註釋。

+0

在沒有寫入的情況下注釋掉M + 1後,問題就解決了。 – Finch