查询 WMI 类 Win32_ShadowCopy 时初始化失败

发布于 2024-11-26 21:19:16 字数 1742 浏览 1 评论 0原文

在多年来的编程过程中,我从未在此网站上发布过问题,但我遇到过许多以前在此解决过的问题。然而现在,我遇到了一个我似乎找不到答案的问题。

我正在创建一个应用程序,其中需要系统上 ShadowCopies 的信息。我试图通过使用 WMI(在 C# 中)来实现这一点。然而,这给了我一个“初始化失败”异常。 这是代码: ManagementScope 范围 = new ManagementScope("\\.\ROOT\cimv2");

//create object query
ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_ShadowCopy");

//create object searcher
ManagementObjectSearcher searcher =
                        new ManagementObjectSearcher(scope, query);

//get collection of WMI objects
ManagementObjectCollection queryCollection = searcher.Get();

//enumerate the collection.
foreach (ManagementObject m in queryCollection) 
{
// access properties of the WMI object
  Console.WriteLine("ClientAccessible : {0}", m["ClientAccessible"]);

}

每当到达 foreach 行时,就会引发 ManagementException 并显示消息“初始化失败”。我完全不知道为什么会发生这种情况。如果我使用完全相同的代码并更改 WMI 类(更改为 Win32_Processor/Win32_LogicalDisk/...),我不会收到此异常,并且 foreach 循环正常工作。我还注意到异常来自语句“searcher.Get();”。 我在 Windows Server 2008 计算机和 Windows 7 Enterprise 计算机上尝试了此代码,两者都生成相同的异常。

我也尝试过在 vb 脚本中使用此类,并且成功了。 VBS 代码:

Set objWMIService = GetObject("winmgmts:\\" + ComputerName + "\root\cimv2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_ShadowCopy")

For Each objItem in colItems
    Wscript.Echo objItem.ClientAccessible
Next

我真的不知道是什么产生了这个异常,特别是因为当我使用 VB 脚本时 WMI 类正在工作。有人看到我在这里做错了什么吗?感谢任何帮助,如果您需要更多信息来解决此问题,请告诉我!

问候, Simon

//注意:我从 http://include.wutils.com/wmi/ROOT%5Ccimv2/CIM_ManagedSystemElement/CIM_LogicalElement/Win32_ShadowCopy/cs-samples.html

while programming throughout the years I have never posted a question on this website, but I have encounted numerous of problems that had been addressed here before. Now, however, I encountered a problem that I can't seem to find an answer to.

I am creating an application in which I need information from the ShadowCopies on the system. I am trying to achieve this by using WMI (in C#). This is however giving me an "Initialization Failure"-exception.
Here's the code:
ManagementScope scope = new ManagementScope("\\.\ROOT\cimv2");

//create object query
ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_ShadowCopy");

//create object searcher
ManagementObjectSearcher searcher =
                        new ManagementObjectSearcher(scope, query);

//get collection of WMI objects
ManagementObjectCollection queryCollection = searcher.Get();

//enumerate the collection.
foreach (ManagementObject m in queryCollection) 
{
// access properties of the WMI object
  Console.WriteLine("ClientAccessible : {0}", m["ClientAccessible"]);

}

Whenever the foreach line is reached, a ManagementException is thrown with the message "Initialization Failure". I have absolutely no clue why this is happening. If I use the exact same code and change the WMI-class (to Win32_Processor/Win32_LogicalDisk/...) I am not getting this exception and the foreach-loop just works. I also noticed that the exception comes forth from the statement "searcher.Get();".
I have tried this code on a Windows Server 2008-machine as well as on a Windows 7 Enterprise-machine, both generating the same exception.

I have also tried using this class in a vb-script and that worked.
Code of VBS:

Set objWMIService = GetObject("winmgmts:\\" + ComputerName + "\root\cimv2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_ShadowCopy")

For Each objItem in colItems
    Wscript.Echo objItem.ClientAccessible
Next

I am really clueless on what is generating this exception, especially since the WMI-class is working when I use a VB-script. Does anybody see what I am doing wrong here? Any help is appreciated and if you need more information to resolve this issue, just let me know!

Greetz,
Simon

//NOTE: I got this code from http://include.wutils.com/wmi/ROOT%5Ccimv2/CIM_ManagedSystemElement/CIM_LogicalElement/Win32_ShadowCopy/cs-samples.html

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

瀞厅☆埖开 2024-12-03 21:19:16

假设您运行的是 64 位版本的 Windows,在“项目属性”中,取消选中“常规”下的“首选 32 位”。我发现除了以管理员身份运行 Visual Studio 之外,这对于防止引发“初始化失败”异常也是必要的。我相信您无法从 64 位平台上的 32 位应用程序调用 WMI 方法。

Assuming you are running a 64-bit version of Windows, in Project Properties, un-check "Prefer 32-bit" under General. I found that this, in addition to running Visual Studio as Administrator, was necessary to prevent the "Initialization Failure" exception being thrown. I believe you cannot call the WMI method from a 32-bit application on 64-bit platforms.

晚风撩人 2024-12-03 21:19:16

我编写的执行实时 WIM 备份的脚本也遇到了同样的问题。您运行此程序的系统有 UAC 吗?如果是这样,请尝试将其关闭或以管理员身份运行该应用程序 - 一旦我这样做,它就会启动。

希望这有帮助

I had the same issue with a script I wrote that performs live WIM backups. Is the system you're running this on have UAC? If so, try either turning it off or running the app as Administrator - as soon as I did that it sprung to life.

Hope this helps

独自←快乐 2024-12-03 21:19:16

在 Windows Server 2008 R2 上工作时,我遇到了同样的问题。

这是我发现的:

mshta.exe 在我的系统中存在 2 个版本:
- C:\Windows\System32\mshta.exe(64 位)
- C:\Windows\SysWOW64\mshta.exe (32 位)

在注册表中,与 hta 文件 (HKCR\htafile\Shell\Open\Command) 关联的应用程序为 C:\Windows\SysWOW64\mshta.exe,其中Win32_ShadowCopy 查询不起作用。
另一方面,当使用 C:\Windows\System32\mshta.exe 运行时,包含查询的 hta 文件按预期工作。

简而言之:使用 64 位版本的 mshta.exe 运行包含 Win32_ShadowCopy wmi 查询的 hta 应用程序。

希望这有帮助

Working on Windows Server 2008 R2, I had the same problem.

Here is what I discovered:

mshta.exe exists in 2 versions in my system:
- C:\Windows\System32\mshta.exe (64 bits)
- C:\Windows\SysWOW64\mshta.exe (32 bits)

In registry, the application associated with hta files (HKCR\htafile\Shell\Open\Command) is C:\Windows\SysWOW64\mshta.exe, with which the Win32_ShadowCopy query does not work.
On the other hand, the hta file containing the query works as expected when run with C:\Windows\System32\mshta.exe.

In a word : run hta applications containing a Win32_ShadowCopy wmi query with the 64 bit version of mshta.exe.

Hope this helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文