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

问题描述

我正在开发一个用我们自己的 shell 替换 explorer.exe@H_404_2@ 的 C# 应用程序。我们希望让用户从我们的 UI 中解锁 BitLocker USB 驱动器。

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

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

manage-bde -unlock <drive>: -password@H_404_2@

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

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

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

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

我应该如何继续提交 pass@H_404_2@ 变量?我知道以纯文本形式使用密码绝不是安全的,但我需要的最重要的一点是知道如何在没有用户输入 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
                }
            }
        }