System.Management.ManagementException 'generic failure' during remote WMI query

  • Thread starter Thread starter Malcolm McCaffery
  • Start date Start date
M

Malcolm McCaffery

Hi There I'm using a C# Visual Studio 2008 / .NET 3.5 / Windows Vista SP1 to
write a utility that queries windows event log remotely.

When querying a certain remote PC it works fine returning a couple of
hundred events then suddenly an exception on this line.

foreach (ManagementObject oReturn in oReturnCollection) (see below for
context)

It appears to be the same event log entry being read that causes this,
because I am always getting the same event log entry before the exception.

Is there anything I can do to detect the cause of this exception, or trap it
(and continue the foreach loop?)

The exception detail is

System.Management.ManagementException was unhandled
Message="Generic failure "
Source="System.Management"
StackTrace:
at
System.Management.ManagementException.ThrowWithExtendedInfo(ManagementStatus
errorCode)
at
System.Management.ManagementObjectCollection.ManagementObjectEnumerator.MoveNext()
at EventAnalyzer.Program.Main(String[] args) in
C:\Users\mccafma\Documents\Visual Studio
2008\Projects\EventAnalyzer\EventAnalyzer\Program.cs:line 387
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[]
args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence
assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:


using System.Management;

RemoteComputer = "remotepc01"

bool display = true;
ManagementScope oMs;
ObjectQuery oQuery;
ManagementObjectSearcher oSearcher;
ManagementObjectCollection oReturnCollection;

oMs=new ManagementScope(@"\\" + RemoteComputer +
@"\root\cimv2", oConn);

Query = "Select * from Win32_NTLogEvent Where (Logfile =
'System' OR Logfile='Application')";

//get system events
oQuery = new ObjectQuery(Query);

Console.WriteLine("Executing \"" + Query + "\" on " +
oMs.Path.ToString());
Console.WriteLine();
//Execute the query
oSearcher = new ManagementObjectSearcher(oMs, oQuery);

//Get the results
oReturnCollection = oSearcher.Get();

//loop through found drives and write out info

foreach (ManagementObject oReturn in oReturnCollection)
{
// process items here
}

regards,

Malcolm.
 
Back
Top