-
-
Save mirsaeedi/d3ad13bc6d3e44700787718751b1cca6 to your computer and use it in GitHub Desktop.
How to unlock BitLocker drives in Windows
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Management; | |
namespace Windows.Security.FileSystem | |
{ | |
public class BitLocker | |
{ | |
private readonly string _computerIp; | |
public BitLocker(string computerIp) | |
{ | |
_computerIp = computerIp; | |
} | |
/// <summary> | |
/// Simple Usage for localhost: | |
/// var bitlocker = new BitLocker("localhost"); | |
/// var result = bitlocker.UnlockWithPassphrase("D:", "password"); | |
/// </summary> | |
/// <param name="driveLetter"></param> | |
/// <param name="passphrase"></param> | |
/// <returns></returns> | |
public object UnlockWithPassphrase(string driveLetter,string passphrase) | |
{ | |
object result=null; | |
var wmiNamespace="\\\\{0}\\root\\CIMV2\\Security\\MicrosoftVolumeEncryption"; | |
var scope = new ManagementScope(string.Format(wmiNamespace, _computerIp)); | |
var query = new ObjectQuery("SELECT * FROM Win32_EncryptableVolume"); | |
var searcher = new ManagementObjectSearcher(scope, query); | |
var allVolumes = searcher.Get(); | |
foreach (ManagementObject volume in allVolumes) | |
{ | |
if (volume.Properties["DriveLetter"].Value.ToString()==driveLetter) | |
{ | |
object[] methodArgs = {passphrase}; | |
result = volume.InvokeMethod("UnlockWithPassphrase", methodArgs); | |
} | |
} | |
return result; | |
} | |
} | |
} |
Note: requires admin rights
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
the class usage is as follows: