System.Management库中ManagementClass的GetInstances()方法中的异常 [英] Exception in GetInstances() method from ManagementClass in System.Management library

查看:73
本文介绍了System.Management库中ManagementClass的GetInstances()方法中的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨支持,



我们正在使用来自" System.Management.dll ManagementClass >"用于查找驱动器的程序集由位锁定器锁定。以下是我们使用的代码段。



此代码在Windows 10,8及更低版本中正常工作



但是,我们在" management.GetInstances(); "中遇到例外情况。在
Windows Server 2012,2016和某些Windows 10操作系统计算机中。



有没有办法解决此异常,似乎这很奇怪,只发生在服务器操作系统和一些Windows 10上


代码片段:

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(s​​cope,drive,new ObjectGetOptions());



var instances = management.GetInstances() ;



例外详情:



ErrorCode: InvalidNamespace



错误消息:无效的命名空间 



堆栈跟踪:



    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)

   在C:\ Users \ Administrator {\\ docsject \ visual studio 2015 \Projects \ConsoleApplication1 \ConsoleApplication1 \Program.cs:第45行
中的ConsoleApplication1.Program.Main(String [] args) />
    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()



问候,b
Balaji R

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

推荐答案

错误表明命名空间不存在。使用WMI Code Creator或类似工具查看该计算机上的WMI命名空间。听起来像机器上没有启用BitLocker。如果不是那么可能命名空间也不可用?您可能会先
尝试查询命名空间。

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.

如果它存在,则可能是权限问题。此类的
文档
表明您必须使用加密连接。您可能需要修改连接逻辑。我没有使用BitLocker所以我不能肯定地说。

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.


这篇关于System.Management库中ManagementClass的GetInstances()方法中的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆