WMI + Bitlocker + C# 获取加密卷产生超时

发布于 2024-09-11 10:14:37 字数 1126 浏览 4 评论 0原文

我想为影响 Bitlocker 功能的特定 WMI 函数创建一个包装类。第一步是获取计算机的所有 Bitlocker 卷,因此我创建了一个控制台应用程序并执行了以下操作:

private static ManagementClass management;
    private static ManagementObjectCollection Volumes = null;

    static void Main(string[] args)
    {

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


        ConnectionOptions options = new ConnectionOptions();
        options.Authentication = AuthenticationLevel.PacketPrivacy;
        options.Impersonation = ImpersonationLevel.Impersonate;

        ManagementScope scope = new ManagementScope(path, options);
        ObjectGetOptions getOptions = new ObjectGetOptions();

        management = new ManagementClass(scope, path, getOptions);
        management.Get();
        Volumes = management.GetInstances();

    }

当我在非 Bitlocker 计算机上运行此应用程序时,卷集合会初始化正常,只是它的计数当然为 0 。现在,我将代码复制到 WinForms 应用程序,当我单击按钮运行此代码时,它会逐步执行“正常”,但是当我在调试期间尝试扩展集合时,应用程序会挂起,并且收到“函数评估超时”消息。这是另一个应用程序中的相同代码。这可能是什么原因?

i want to create a wrapper class for specific WMI functions that affect Bitlocker functionality. The first step is to get all the Bitlocker volumes of a machine so I created a Console Application and did this:

private static ManagementClass management;
    private static ManagementObjectCollection Volumes = null;

    static void Main(string[] args)
    {

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


        ConnectionOptions options = new ConnectionOptions();
        options.Authentication = AuthenticationLevel.PacketPrivacy;
        options.Impersonation = ImpersonationLevel.Impersonate;

        ManagementScope scope = new ManagementScope(path, options);
        ObjectGetOptions getOptions = new ObjectGetOptions();

        management = new ManagementClass(scope, path, getOptions);
        management.Get();
        Volumes = management.GetInstances();

    }

When I run this on a non-Bitlocker machine the Volumes Collection gets initialized OK, only that it has a Count of 0 of course. Now I copied the code over to a WinForms App and when I click a button to run this code it steps through OK but when I try to expand the collection during debugging the App hangs and I get a "Function evaluation timed out". It's the same code just in another Application. What could be the reason for this?

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

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

发布评论

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

评论(2

听,心雨的声音 2024-09-18 10:14:37

嗯。如果我没有以管理员身份运行它,我会得到一个空引用异常,但是当我以管理员身份运行它时(Win 7 x64,顺便说一句),我得到了四个卷。

Hm. I got a null reference exception if I didn't run it as administrator, but when I ran it as administrator (Win 7 x64, btw), I got four Volumes back.

谁的新欢旧爱 2024-09-18 10:14:37

我刚刚遇到了类似的问题,我将为您发布我的代码,希望对您有所帮助。

ManagementObjectSearcher Encryption = new ManagementObjectSearcher(@"root\cimv2\Security\MicrosoftVolumeEncryption", "SELECT * FROM Win32_EncryptableVolume");

        foreach (ManagementObject QueryObj in Encryption.Get())
        {
            string EncryptionStatus = QueryObj.GetPropertyValue("ProtectionStatus").ToString();

            if (EncryptionStatus == "0")
            {
                EncryptionDialog.Text = "Unencrypted";
            }
            else if (EncryptionStatus == "1")
            {
                EncryptionDialog.Text = "Encrypted - SysPrep will not complete";
            }
            else if (EncryptionStatus == "2")
            {
                EncryptionDialog.Text = "Cannot Determine Encryption";
            }
        }

我使用它来显示我正在创建的 sysprep 工具的状态,以便“EncryptionDialog.Text = ...”可以替换为您可能需要的任何其他调用。如果您使用 Visual Studio,您还需要记住“这至少给我带来了问题”,您需要在“添加新文件”对话框中将一个标有“应用程序清单文件”的文件添加到项目中。原因是应用程序需要在管理员模式下打开(仅供参考,以防您还没有做到这一点)

I just had a similar issue, I will post my code for you hopefully it helps.

ManagementObjectSearcher Encryption = new ManagementObjectSearcher(@"root\cimv2\Security\MicrosoftVolumeEncryption", "SELECT * FROM Win32_EncryptableVolume");

        foreach (ManagementObject QueryObj in Encryption.Get())
        {
            string EncryptionStatus = QueryObj.GetPropertyValue("ProtectionStatus").ToString();

            if (EncryptionStatus == "0")
            {
                EncryptionDialog.Text = "Unencrypted";
            }
            else if (EncryptionStatus == "1")
            {
                EncryptionDialog.Text = "Encrypted - SysPrep will not complete";
            }
            else if (EncryptionStatus == "2")
            {
                EncryptionDialog.Text = "Cannot Determine Encryption";
            }
        }

I'm using this to display the status for a sysprep tool i'm creating so the "EncryptionDialog.Text = ..." can be replaced with any other calls you may need. you also need to remember "which caused me issues at least" if you are using visual studio you will need to add a file to your project labeled "Application Manifest File" in the "Add New File" Dialog. The reason for this is that the application will need to be opened in Administrator mode(Just an FYI in case you haven't made it that far)

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