使用 ManagementObjectSearcher 获取准确的 BitLocker WMI 值

Using ManagementObjectSearcher to get an exact BitLocker WMI value

大家好, 我在使用 ManagementObjectSearcher 时遇到问题。我正在尝试查询我想要的确切值,但找不到任何对精确语法要求的引用,并且在尝试将代码完成为我需要的代码时,我不断收到错误消息。

出现问题的代码的特定部分是当我检查驱动器加密状态时(我知道我的磁盘在这台机器上没有加密,这就是为什么这是我唯一的值如果现在)。非常感谢任何帮助使此代码提取正确值的帮助。

我已经尝试了“=”方法和 "LIKE" 方法,输出没有变化。

using Microsoft.Win32;
using System;
using System.Drawing;
using System.IO;
using System.Management;
using System.Windows.Forms;

    public Form1()
    {
        InitializeComponent();

        // Check for OS Version
        string OSVer = Convert.ToString(Registry.GetValue("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion", "ProductName", null));
        OSDialog.Text = OSVer;

        // Check Architecture
        if (Directory.Exists("C:\Program Files (x86)"))
        {
            ArchitectureDialog.Text = "64 Bit";
        }
        else
        {
            ArchitectureDialog.Text = "32 Bit";
        }

        // Check Encryption
        ManagementObjectSearcher Collect = new ManagementObjectSearcher("SELECT ProtectionStatus FROM Win32_EncryptableVolume WHERE DriveLetter = 'C:'");

        string Encryption = Collect.ToString();

        if (Encryption == "0")
        {
            EncryptionDialog.Text = "Disk is not Encrypted";
            EncryptionDialog.ForeColor = Color.Green;
        }
    }

    private void Cancel_Click(object sender, EventArgs e)
    {
        Close();
    }

从 WMI 获取 BitLocker 信息需要提升权限。您的代码必须是 运行 作为管理员并且您必须请求提升权限。因此,我不使用 ManagementObjectSearcher 来获取 BitLocker 信息。相反,我做了类似于以下的事情(修改为您的场景 - 但未如图所示进行测试):

ManagementObject GetBitLockerManager( string driveLetter )
{
    var path = new ManagementPath( );
    path.Server = string.Empty;
    path.NamespacePath = @"\ROOT\CIMV2\Security\MicrosoftVolumeEncryption";
    path.ClassName = "Win32_EncryptableVolume";

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

    var scope = new ManagementScope( path, options );
    var mgmt = new ManagementClass( scope, path, new ObjectGetOptions( ) );

    mgmt.Get( );
    return mgmt
      .GetInstances( )
      .Cast<ManagementObject>( )
      .FirstOrDefault
      ( vol => 
        string.Compare
        (
          vol[ "DriveLetter" ] as string, 
          driveLetter, 
          true
        ) == 0 
      );
}

好的,我明白了,感谢您提供的所有帮助。代码如下。

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";
            }
        }