i’m writing code to terminate specific processes after a specified amount of time. i’m using the below code (simplified for post):
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT Name, CreationDate FROM Win32_Process WHERE Name = 'foo'");
foreach (ManagementObject process in searcher.Get())
{
process.InvokeMethod("Terminate", null);
}
the problem — using a WQL statement of SELECT Name, CreationDate throws an exception when trying to do the terminate:
"Operation is not valid due to the current state of the object."
…but, using SELECT * works and terminates the process. why is this — is there a specific WMI column that’s needed in the resultset?
thanks!
When you executes a WMI method, the WMI internally searh for the WMI Object path to identify the instance over the method will be executed.
In this case for the
Win32_ProcessWMI class the WMI Object Path looks likeWin32_Process.Handle="8112", So as you see theHandleproperty is part of the WMi Object path and must be included in your WQL sentece,Check this sample.