1

The first of these 2 locks a BitLocked drive. The 2nd's InvokeMethod throws: 'Invalid object path'. Why? They seem equivalent.

//Using a search:
ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2\\Security\\MicrosoftVolumeEncryption", "SELECT * FROM Win32_EncryptableVolume WHERE DriveLetter = 'E:'");
foreach (ManagementObject classInstance1 in searcher.Get())
    classInstance1.InvokeMethod("Lock", new object[] { true }); 

//Direct:
ManagementObject classInstance2 = new ManagementObject("root\\CIMV2\\Security\\MicrosoftVolumeEncryption", "Win32_EncryptableVolume.DriveLetter='E:'", null);
classInstance2.InvokeMethod("Lock", new object[] { 0 });//throws: 'Invalid object path'.
ispiro
  • 26,556
  • 38
  • 136
  • 291

3 Answers3

3

You unfortunately can't instantiate an object using a property that isn't a key property. A key property in WMI is a property that has the CIM_Key qualifier, the WMI documentation goes into further detail about the Key Qualifier. For more information about the WMI requirement of using a full path with key to reference an object you can read the WMI documentation about Instance Object Paths.

In C#, for the particular class you specified (Win32_EncryptableVolume), You can only accomplish what you are trying to do by using the ManagementObjectSearcher as shown in your example. This is because you are trying to get an instance based on a standard property rather than a key property.

A great utility to explore WMI is WMI Explorer 2.0. This gives a great visual representation of WMI classes. In this utility, Key Properties are identified with an asterisk.

https://github.com/vinaypamnani/wmie2/releases

Paul G
  • 1,219
  • 7
  • 14
0

It seems you are not calling Get() method. Try this:

ManagementObject classInstance2 = new ManagementObject("root\\CIMV2\\Security\\MicrosoftVolumeEncryption", "Win32_EncryptableVolume.DriveLetter='E:'", null);
classInstance2.Get();
classInstance2.InvokeMethod("Lock", new object[] { 0 });

Check out this documentation: https://learn.microsoft.com/en-us/windows/desktop/wmisdk/retrieving-an-instance

NicoRiff
  • 4,803
  • 3
  • 25
  • 54
  • I tried and got the 'Invalid object path' at the `Get` line. – ispiro Feb 26 '19 at 20:12
  • Is DriveLetter the only key of the class?? – NicoRiff Feb 26 '19 at 20:17
  • No. And it actually works with DeviceID but my code doesn't know that. It knows the letter. – ispiro Feb 26 '19 at 20:51
  • That is the thing. You only can retrieve the instance in a 'complete way' by searching by the key attributes. I think your best shot is to use ManagementObjectSearcher – NicoRiff Feb 26 '19 at 21:03
  • Thanks. Do you have any link regarding that "key attribute" thing? – ispiro Feb 26 '19 at 21:14
  • Yes. Look at page 348 of this book: https://books.google.com.ar/books?id=e7afsJzJVCQC&pg=PA361&lpg=PA361&dq=managementobject+get+instance+constructor&source=bl&ots=r4USvpfwtT&sig=ACfU3U2xEX9J1-ryGfQrSD7PWJhBH4s0Tg&hl=es-419&sa=X&ved=2ahUKEwjK1tz6k9rgAhVBIrkGHQkPBWEQ6AEwCHoECAIQAQ#v=onepage&q=managementobject%20get%20instance%20constructor&f=false – NicoRiff Feb 26 '19 at 21:20
-2

I'll just assume that the correct answer is similar to what the others have mentioned but not exactly.

The class's page mentions that DeviceID has the following property:

Qualifiers: Key

I assume, for lack of actual documentation, that searching for something by their Key returns the thing itself. While searching by something else returns a list of objects that satisfy that condition. Even if the list contains only 1 entry - it's not the object itself but rather a list.

But if someone could supply some documentation, that would be nice.

ispiro
  • 26,556
  • 38
  • 136
  • 291