Access Denied Clear Event Log

Posted on 2011-10-19
Last Modified: 2012-08-13
I an getting an error when trying to clear the application and system event log with my vbscript. I am able to back it up but get the error when trying to clear it. Below is the code I am using to connect.

Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate,(Backup, Security)}!\\" & _
        strComputer & "\root\cimv2")

Set colLogFiles = objWMIService.ExecQuery _
    ("Select * from Win32_NTEventLogFile where LogFileName='Application'")

For Each objLogfile in colLogFiles
    objLogFile.BackupEventLog(pth &"\IOS\Application_" & tDate & ".evt")
    WScript.Sleep 400000

I am logged on to the 2008 R2 server as a domain admin. I added full control to the HKLM\System\CurrentControlSet\Services\EventLog to my user account. I am not in the Domain Guest Group. What am I missing? I also changed the restrictedguestaccess from 1 to 0 for the system and application log.
Question by:dthillsr
    LVL 65

    Accepted Solution

    It should certainly work....maybe try running cmd.exe by right clicking it, and selecting "Run as administrator", then type
    cscript C:\Scripts\YourScript.vbs

    and see if that helps.

    I'm not sure you'll need the Sleep in there either...try using:

    For Each objLogfile in colLogFiles
        intReturn = objLogFile.BackupEventLog(pth &"\IOS\Application_" & tDate & ".evt")
        If intReturn = 0 Then
           WScript.Echo "Backup of event log failed with return code " & intReturn
        End If

    Open in new window



    Author Closing Comment

    Running it as administrator with cscript worked, thanks
    LVL 65

    Expert Comment

    No worries.  That's down to UAC being enabled.  Same happens on Vista and Windows 7.


    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Recently I finished a vbscript that I thought I'd share.  It uses a text file with a list of server names to loop through and get various status reports, then writes them all into an Excel file.  Originally it was put together for our Altiris server…
    Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now