系统管理.管理异常
我正在运行以下代码:
System.Management.ManagementClass wmiNetAdapterConfiguration = new System.Management.ManagementClass("Win32_NetworkAdapterConfiguration");
System.Management.ManagementObjectCollection wmiNetAdapters = wmiNetAdapterConfiguration.GetInstances();
Log.logInfo("Net adapters:" + wmiNetAdapters.get_Count());
在某些机器上它没问题,在某些机器上我收到以下错误:
System.Management.ManagementException:未找到
调用堆栈:
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()
知道为什么吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我遇到了类似的问题,因为 WMI 文件以某种方式损坏了。
修复:
这应该可以解决 WMI 文件损坏问题。
I had the similar issue caused because WMI files got corrupted somehow.
Fix:
This should fix WMI file corruption issue.
System.Management
命名空间提供的功能 依赖于 WMI (Windows Management Instrumentation)服务。我怀疑抛出该异常的系统上尚未启动 WMI 服务。
为了进行故障排除,您可以使用管理工具 → 服务实用程序进行验证。
如果情况确实如此,您可以将代码包装在
try
-catch
块中,并使用ServiceController
类 启动和停止相应的服务。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 theServiceController
class to start and stop the appropriate service.我们刚刚遇到了同样的问题,并按照下面链接的帖子一步步操作,并成功解决了问题。
https:// answers.microsoft.com/en-us/windows/forum/all/systemmanagementmanagementexception-invalid/74ac22c7-509d-42a5-9f7f-2686dc87b7b2
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
WMI 服务正在为我运行,并且我使用的是管理员帐户。
当我使用 rsconfig.exe 运行命令时,我得到了完全相同的错误和堆栈跟踪,例如
我做错的是实例名称有自己的参数 -i 和这修复了它:
要检查预期的参数,请运行:
更新:
这实际上是不正确的,如果您查看预期的参数,它会指出服务器名称也必须包含实例名称,您不能包含引号,如果实例名称包含 $ 符号,您可能需要对其进行转义,请参阅此处了解更多信息:https://stackoverflow。 com/a/56370766/495455
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
What I was doing wrong was the instance name has its own argument -i and this fixed it:
To check the expected arguments run:
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