使用在 C# 应用程序中生成的批处理文件解锁 BitLocker 驱动器

Unlock BitLocker drive with batch file generated in C# app

我正在开发一个用我们自己的 shell 替换 explorer.exe 的 C# 应用程序。我们想让用户从我们的 UI.

中解锁 BitLocker USB 驱动器

C# 应用程序会定期刷新连接到计算机的驱动器列表。对于找到的每个驱动器,它通过启动执行 manage-bde -status 并解析输出的 Process 来检查 BDE 状态。它工作正常。

问题 自从

解锁驱动器就给我带来了问题

manage-bde -unlock <drive>: -password

是一个活动提示,我们根本不希望用户看到命令提示符打开以输入文本。他们将 select 驱动器名称并预先在 C# 应用程序中输入密码。

我的一个想法是使用驱动器名称和密码在 C# 应用程序中生成一个 .bat 文件。但是我不知道实现提交密码的正确语法(这里是.bat noob)。

我的(非常)WIP 批处理文件

@echo off
set driveName=F:
set pass=thePassword
manage-bde -unlock %driveName% -password 

我应该如何提交 pass 变量?我知道使用纯文本密码绝非安全,但我需要的最重要的收获是知道如何在没有用户输入 cmd 的情况下在批处理文件中构建它。

谢谢。

在尝试了一些建议后得到了一个可以工作的 PowerShell 脚本解决方案(感谢 Compo)。它解决了在用户不与命令提示符交互的情况下发出 BitLocker 操作的问题。我知道还有更优雅的方法。

public void ToggleBDELock(string driveLetter, string password )
        {
            string directory = @"C:\thePath\";
            string scriptName = $"bdeunlock_{driveLetter}.ps1";
            string scriptPath = Path.Combine( directory, scriptName );
            string output;
            FileStream fileStream;

            // Set up location for the script.
            // yada yada yada

            // Write the script to the file. 
            fileStream = File.Create( scriptPath );
            using( var writer = new StreamWriter( fileStream ) )
            {
                writer.WriteLine( $"$SecureString = ConvertTo-SecureString \"{password}\" -AsPlainText -Force" );
                writer.WriteLine( $"Unlock-BitLocker -MountPoint \"{driveLetter}:\" -Password $SecureString" );
            }

            // Configure a process to run the script.
            var startInfo = new ProcessStartInfo
            {
                FileName = "powershell.exe",
                Arguments = $"-NoProfile -ExecutionPolicy unrestricted -file \"{scriptPath}\"",
                UseShellExecute = false,
                RedirectStandardOutput = true,
                CreateNoWindow = true
            };

            // Try to execute the script.
            using( var process = new Process { StartInfo = startInfo } )
            {
                try
                {
                    process.Start();
                    output = process.StandardOutput.ReadToEnd();
                }
                catch( Exception e )
                {
                    // yada yada yada
                }
            }
        }