none
Exception in GetInstances() method from ManagementClass in System.Management library RRS feed

  • Question

  • Hi Support,

    We are using ManagementClass from "System.Management.dll" assembly to find the drive is locked by bit-locker. Below is the code snippet we were using.

    This is code works fine in Windows 10, 8 and lower versions

    However, we are getting exception in "management.GetInstances();" in Windows Server 2012, 2016 and in some Windows 10 OS machines.

    Is there any ways to resolve this exception, seems this is weird and occurs only on server os and some Windows 10

    Code snippet:

    var drive = new ManagementPath();
    drive.NamespacePath = "\\ROOT\\CIMV2\\Security\\MicrosoftVolumeEncryption";
    drive.ClassName = "Win32_EncryptableVolume";

    var scope = new ManagementScope(drive, new ConnectionOptions() { Impersonation = ImpersonationLevel.Impersonate });
    var management = new ManagementClass(scope, drive, new ObjectGetOptions());

    var instances= management.GetInstances();

    Exception Details:

    ErrorCode: InvalidNamespace

    Error Message: Invalid namespace 

    Stack Trace:

       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 ConsoleApplication1.Program.Main(String[] args) in C:\Users\Administrator\documents\visual studio 2015\Projects\ConsoleApplication1\ConsoleApplication1\Program.cs:line 45
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()

    Regards,
    Balaji R

       

    -ji

    Wednesday, October 31, 2018 4:41 PM

All replies

  • The error indicates the namespace doesn't exist. Use WMI Code Creator or similar tool to see the WMI namespaces on that machine. Sounds like maybe BitLocker isn't enabled on the machine. If it isn't then maybe the namespace isn't available either? You might try querying for the namespace first.

    If it is there it could be a permissions issue perhaps. The documentation for this class indicates you have to use an encrypted connection. You might need to modify your connection logic. I haven't worked with BitLocker so I cannot say for certain.


    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, October 31, 2018 5:39 PM
    Moderator