locked
Printing eventlog using powershell and vbs? RRS feed

  • Question

  • Can anyone tell me why this script does not work?

    function check_event_logs()
    
     Set objShell = CreateObject("Wscript.Shell")
        objShell.Run("powershell.exe -noexit .\eventquery.ps1")
     
     Set objFSO = CreateObject("Scripting.FileSystemObject")
     Set objFile = objFSO.GetFile("C:\scripts\eventlog.txt")
     If objFile.Size > 0 Then
       Set objReadFile = objFSO.OpenTextFile("C:\scripts\eventlog.txt", 1)
       strContents = objReadFile.ReadAll
       Wscript.Echo strContents
       objReadFile.Close
     Else
       Wscript.Echo "No recent Critical errors found."
     End If
    
    

    This is my powershell script:

    get-EventLog application -newest 50 | where {$_.entryType -match "Error"} | format-table timewritten, entrytype, source, instanceid, message -wrap -autosize | Out-File eventlog.txt

    What am I doing wrong?

    I am trying to print out errors in the application event query. (eventquery.vbs does not work on server 2008)

    The powershell script works by itself, but it does not show any output when I run it using the above vbs script.

    Also, how can I call and run the powershell script silently from vbs?

    Thursday, August 12, 2010 4:08 AM

Answers

  • Your problem appears to be right here:

    object.Run(strCommand, [intWindowStyle], [bWaitOnReturn])

    (taken from the script56.chm help file)

    You omitted the switch that causes the .Run method to wait until the PS command has finished its job. Now while I do not know anything about Power Shell, I wonder why you don't do the whole job in that language. I understand that PS can easily do anything that VB Script can, and more. Why use a hybrid solution when PS can do the lot?

    • Marked as answer by LikeToCode Tuesday, August 17, 2010 3:45 AM
    Thursday, August 12, 2010 5:37 AM

All replies

  • Your problem appears to be right here:

    object.Run(strCommand, [intWindowStyle], [bWaitOnReturn])

    (taken from the script56.chm help file)

    You omitted the switch that causes the .Run method to wait until the PS command has finished its job. Now while I do not know anything about Power Shell, I wonder why you don't do the whole job in that language. I understand that PS can easily do anything that VB Script can, and more. Why use a hybrid solution when PS can do the lot?

    • Marked as answer by LikeToCode Tuesday, August 17, 2010 3:45 AM
    Thursday, August 12, 2010 5:37 AM
  • I tried your suggestion, but it stalls the script when running.

    Here is what I am currently trying:

    function check_event_logs()
    
     	Set objShell = CreateObject("Wscript.Shell")
        
    	Set objScriptExec = objShell.Exec("cmd /c powershell c:\scripts\eventquery2.ps1")
    	eventlog = objScriptExec.StdOut.ReadAll
    
    	fileoutput = fileoutput & """" & "EVENTLOG" & """" & "," & """" & eventlog & """" & vbcrlf
    
    	message = message & "Eventlog quiet output:" & vbcrlf & vbcrlf
    	message = message & eventlog & vbcrlf
    
    
    end function

    This is part of a larger vbs script.

    When it gets to this part, I see the command prompt come up, but it just freezes on this part, and the script never continues.

    (Just a command prompt window with a blinking cursor.)

    If I run "cmd /c powershell c:\scripts\eventquery2.ps1" from Start->Run, or from the command promt manually, it runs fine with no issues.

    Monday, August 16, 2010 7:29 AM
  • Ok.

    I can get it to run using this:

    Dim oShell, appCmd
    	Set oShell = CreateObject("WScript.Shell")
    	appCmd = "powershell &'" & Replace(WScript.ScriptFullName, ".vbs", ".ps1") & "'"
    	
    	oShell.Run(appCmd, 4, false)

    But I can't get it to run with this:

    Dim oShell, appCmd
    	Set oShell = CreateObject("WScript.Shell")
    	appCmd = "powershell &'" & Replace(WScript.ScriptFullName, ".vbs", ".ps1") & "'"
    	
    eventlog = objShell.Exec(eventlog_cmd)
    Set objScriptExec = objShell.Exec(eventlog_cmd)
    	eventlog = objScriptExec.StdOut.ReadAll
    
    	fileoutput = fileoutput & """" & "EVENTLOG" & """" & "," & """" & eventlog & """" & vbcrlf
    
    	message = message & "Eventlog quiet output:" & vbcrlf & vbcrlf
    	message = message & eventlog & vbcrlf
    • Edited by Joe Ho1 Monday, August 16, 2010 8:47 AM forgot some code
    • Marked as answer by LikeToCode Tuesday, August 17, 2010 3:45 AM
    • Unmarked as answer by Joe Ho1 Tuesday, August 17, 2010 3:10 PM
    Monday, August 16, 2010 8:10 AM