troubleshooting Question

Event log backup using C# and WMI

Avatar of dgerler
dgerlerFlag for United States of America asked on
.NET ProgrammingC#Visual Basic.NET
5 Comments2 Solutions1833 ViewsLast Modified:
I need to maintain a years worth of eventlogs on over 200 systems. Because of several environmental factors I've decied to add some functionallity to the monitor service we already have running on all the systems.

During my development, I am getting an "Access Denied" error with the attached code. I'm sure it's because I'm not assing credentials, but I can't find any examples of it in C#. Can anyone assist? I think I can convert from if anyone has an example of that?

Sorry, the error is coming from the InvokeMethod.
ConnectionOptions oConn = new ConnectionOptions();
            ManagementScope oMs = new ManagementScope(@"\\localhost", oConn);

            ObjectQuery oQry = new ObjectQuery("select * from Win32_NTEventLogFile");

            ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(oMs, oQry);

            ManagementObjectCollection oRtnColl = oSearcher.Get();

            foreach (ManagementObject oRtn in oRtnColl)
                Console.Write("Name: " + oRtn["Name"].ToString() + "\t");
                Console.WriteLine("FileName: " + oRtn["LogfileName"].ToString());
                Object[] file = {@"C:\Test" + oRtn["Name"].ToString()};
                oRtn.InvokeMethod("BackupEventlog", file);
                //Console.WriteLine("FreeSpace: " + oRtn["FreeSpace"].ToString());
                //Console.WriteLine("Size: " + oRtn["Size"].ToString());


Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 2 Answers and 5 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros