我正在尝试为可能使用 BitLocker 连接到 Win7 及更高版本机器的每个设备构建一个循环。自从我们一直使用 TPM 以来,这在过去一直不是问题,但现在我们正在尝试使用 USB 密钥来降低成本。我的旧代码只显示了最后一个设备加密状态,它最终是 USB 驱动器。所以我想创建一个循环,列出每个驱动器及其加密状态。
我的目标是创建一个包含每个驱动器状态的阵列。我遇到的问题是确定由于 searcher.Get() 实际找到了多少项目(我认为应该是下面的 encrArr 数组),所以我现在用 2 作为硬盘驱动器和 USB 驱动器。
下面的代码在编写时没有显示任何错误,但是当我尝试运行它时它崩溃了(“调用的目标已抛出异常。”)。
public string[] getBitLockerArr()
{
string[] encrArr = new string[2];
encrArr[0] = "Protection Off, Fully Decrypted, 0% encrypted";
encrArr[1] = "Protection Off, Fully Decrypted, 0% encrypted";
ManagementObjectSearcher searcher = new
ManagementObjectSearcher("root\\cimv2\\Security\\MicrosoftVolumeEncryption", "SELECT * FROM Win32_EncryptableVolume");
int i = 0;
foreach (ManagementObject wmi in searcher.Get())
{
try
{
string[] bitLockerArr = new string[3];
bitLockerArr[0] = "Unknown";
bitLockerArr[1] = "Unknown";
bitLockerArr[2] = "Unknown";
StringBuilder bitLocker = new StringBuilder();
ManagementObject classInstance =
new ManagementObject("root\\cimv2\\Security\\MicrosoftVolumeEncryption", "Win32_EncryptableVolume.DeviceID='" + wmi["DeviceID"] + "'", null);
ManagementBaseObject statProtection = classInstance.InvokeMethod("GetProtectionStatus", null, null);
ManagementBaseObject statConversion = classInstance.InvokeMethod("GetConversionStatus", null, null);
if (statProtection["ProtectionStatus"] != null) { bitLockerArr[0] = statProtection["ProtectionStatus"].ToString(); }
if (statConversion["ConversionStatus"] != null) { bitLockerArr[1] = statConversion["ConversionStatus"].ToString(); }
if (statConversion["EncryptionPercentage"] != null) { bitLockerArr[2] = statConversion["EncryptionPercentage"].ToString(); }
// BitLocker Protection Status
string blProtStat = bitLockerArr[0];
if (blProtStat == "0") { blProtStat = "Protection Off"; }
else if (blProtStat == "1") { blProtStat = "Protection On"; }
// BitLocker Conversion Statuses
string blConvStat = bitLockerArr[1];
if (blConvStat == "0") { blConvStat = ", Fully Decrypted"; }
else if (blConvStat == "1") { blConvStat = ", Fully Encrypted"; }
else if (blConvStat == "2") { blConvStat = ", Encryption In Progress"; }
else if (blConvStat == "3") { blConvStat = ", Decryption In Progress"; }
else if (blConvStat == "4") { blConvStat = ", Encryption Paused"; }
else if (blConvStat == "5") { blConvStat = ", Decryption Paused"; }
// BitLocker Encryption Status
string blEncStat = ", " + bitLockerArr[2] + "% encrypted";
encrArr[i] = wmi["DriveLetter"] + " " + blProtStat + blConvStat + blEncStat + ", " + wmi["DeviceID"];
i = i + 1;
}
catch { }
}
return encrArr;
}