Search code examples
c#windows-10wmisystem.managementbitdefender

Unable to foreach through an ManagementObjectCollection, "This method is not implemented in any class" exception thrown


I am trying to query the local machine for information about the status of the different services in windows security, such as windows defender and the firewall, using WMI from system.management. For testing purposes I am working in a console project and I am just trying to access MSFT_MpComputerStatus and properties like AMServiceEnabled, but no matter what i do an exception is thrown when a try to foreach through the collection.

I am a very new to WMI so it might just be something i have missed but I have been trying to get this to work for a few days now.Through my search i found a few different code examples showing how to access properties of classes, such as: (For finding everything in a class)

(For accessing properties on a specific ManagementObject instance)

(Using the WMI code creator tool was suggested here)

I tried all of these using the namespace: root\\Microsoft\\Windows\\Defender and class: MSFT_MpComputerStatus but nothing worked.

Below is the code the tool made and even this fails in both my console application and the tool itself.

        try
        {
            ManagementObjectSearcher searcher =
                new ManagementObjectSearcher("root\\Microsoft\\Windows\\Defender",
                    "SELECT * FROM MSFT_MpComputerStatus");

            foreach (ManagementObject queryObj in searcher.Get())
            {
                Console.WriteLine("-----------------------------------");
                Console.WriteLine("MSFT_MpComputerStatus instance");
                Console.WriteLine("-----------------------------------");
                Console.WriteLine("AMServiceEnabled: {0}", queryObj["AMServiceEnabled"]);
                Console.WriteLine("AntispywareEnabled: {0}", queryObj["AntispywareEnabled"]);
            }
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            throw;
        }

This should return some bool values indicating whether the services are on or off but once it reaches the foreach loop this exception is thrown "System.Management.ManagementException: This method is not implemented in any class". Am i missing something here? Is there some other way to get information i need using WMI?

Edit: After some more searching i also found that MSFT_MpComputerStatusalso exists in the root\\Microsoft\\protectionManagement, but using this namespace produces the same result.

Edit 2: It is a settings problem. Tested the above code on 3 company development pc's and one non development pc, and the code worked fine on the non development pc. If i find what is the culprit is will post it here.

Edit 3: It is our anti virus system (bitdefender) that is the root of the problem. Working on finding a workaround, if any.

Edit 4: See my own anwser.


Solution

  • When windows defender is completely disabled (which most AV software seem to do when it is installed) access to that class is lost but it is still visible. To get the status of windows security in general other methods must be used, such as the SecurityCenter2 namespace (not officially supported), the wscapi (c++) or through some powershell commands.