8

I am running following code:

System.Management.ManagementClass wmiNetAdapterConfiguration = new System.Management.ManagementClass("Win32_NetworkAdapterConfiguration");
System.Management.ManagementObjectCollection wmiNetAdapters = wmiNetAdapterConfiguration.GetInstances();
Log.logInfo("Net adapters:" + wmiNetAdapters.get_Count());

And on some machines it is ok, and on some I am getting following error:

System.Management.ManagementException: Not found

Call stack:

System.Management.ManagementException: Not found 
   at System.Management.ManagementException.ThrowWithExtendedInfo(ManagementStatus errorCode)
   at System.Management.ManagementScope.InitializeGuts(Object o)
   at System.Management.ManagementScope.Initialize()
   at System.Management.ManagementObject.Initialize(Boolean getObject)
   at System.Management.ManagementClass.GetInstances(EnumerationOptions options)
   at System.Management.ManagementClass.GetInstances()

Any idea why?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Night Walker
  • 20,638
  • 52
  • 151
  • 228

4 Answers4

11

The functionality provided by the System.Management namespace is dependent upon the WMI (Windows Management Instrumentation) service.

I suspect that the WMI service has not been started on the systems that are throwing that exception.

For troubleshooting purposes, you can verify that using the Administrative Tools → Services utility.

If this turns out to be the case, you can wrap the code in a try-catch block and use the ServiceController class to start and stop the appropriate service.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • on my local machine this WMI is not running and those functions are running ok . – Night Walker Mar 09 '11 at 15:13
  • 6
    @Night: I doubt very seriously that the WMI service is *not* running on your machine. The name of the WMI service is `winmgmt`. Check Services or Task Manager again, just to be sure. The [`ManagementStatus` enum](http://msdn.microsoft.com/en-us/library/system.management.managementstatus.aspx) says that a "Not Found" error means that "the object could not be found", which confirms my theory that it can't find the WMI object it's trying to use internally. – Cody Gray - on strike Mar 09 '11 at 15:15
  • 3
    I'm running into this same issue and Windows Management Instrumentation is Running in services. I do not get the exception when I run as myself, I only get the error when I try to run as administrator. – Hagelt18 Dec 11 '15 at 14:40
7

I had the similar issue caused because WMI files got corrupted somehow.

Fix:

  1. Run cmd as Administrator
  2. Type "cd wbem" and hit Enter
  3. Type “dir /b *.mof *.mfl | findstr /v /i uninstall > moflist.txt & for /F %s in (moflist.txt) do mofcomp %s” and hit Enter.

This should fix WMI file corruption issue.

Mustafa
  • 71
  • 1
  • 2
  • It works for. Failed to install service [nginxservice]. Check ~/.config/valet/Log for errors. FATAL - Unhandled exception System.Management.ManagementException: Invalid class at System.Management.ManagementException.ThrowWithExtendedInfo(ManagementStatus errorCode) at System.Management.ManagementObjectCollection.ManagementObjectEnumerator.MoveNext() – Shanaka Madusanka Jun 25 '23 at 18:11
  • To find the corruption of WMI https://support.buildsoft.eu/knowledge-base/error-invalid-class-at-system-management-managementexception-throwwithextendedinfo/ Steps: Start > Run > enter wmimgmt.msc and it OK Console Root > WMI Control(Local) Right click > Properties > General. You can find the corruption there. – Shanaka Madusanka Jun 25 '23 at 18:13
3

We just experienced the same problem and followed the below linked post step by step and were able to fix the problem successfully.

https://answers.microsoft.com/en-us/windows/forum/all/systemmanagementmanagementexception-invalid/74ac22c7-509d-42a5-9f7f-2686dc87b7b2

Basically, Open up Computer management and in the sidebar on the left side go to the services and application drop menu and go to WMI control. Then look at the bar on the right and click on more actions and then properties. If you are having the same error as me you will see it here. I tried doing the 'restore from last backup' and it came up with some errors. Then I clicked on 'Restore now'... I had no backups but I saw that the backup files are called '*.rec files'. I went to windows search and searched *.rec and it found one in C:\Windows\System32\wbem . It was called corrupted.rec so I didn't hold much hope, and it seemed as though it came up with the same message as before. So I continued looking around and stuff until I eventually went back to WMI control and saw that it no longer displayed the namespace error but my system spec. After I saw this I opened the software that controls the lights for my laptop and IT OPENED. I have finally been able to turn my god damn obnoxious laptop lights off. After this I made a backup for WMI and I will now make a system restore point.

zıəs uɐɟəʇs
  • 1,728
  • 3
  • 14
  • 27
0

The WMI Service was running for me and I was using a Administrator account.

I got the exact same error and stacktrace when I was running a command using rsconfig.exe, eg

cd C:\Program Files (x86)\Microsoft SQL Server\130\Tools\Binn\

rsconfig.exe -c -s sqlserver\instance -d dbName -a SQL -u user -p pass

What I was doing wrong was the instance name has its own argument -i and this fixed it:

rsconfig.exe -c -s sqlserver -i instance -d dbName -a SQL -u user -p pass

To check the expected arguments run:

rsconfig.exe /?

Update:

This is actually incorrect, if you look at the expected arguments it states the server name has to include the instance name as well, you can't have quotes and if the instance name contains a $ sign you may need to escape it, see here for more info: https://stackoverflow.com/a/56370766/495455

Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321