简体   繁体   English

从 C# 运行的 powershell 命令捕获输出

[英]Capturing output from powershell commands run from C#

I'm creating a simple C# program used to check various settings on windows 10 builds we're testing.我正在创建一个简单的 C# 程序,用于检查我们正在测试的 Windows 10 版本上的各种设置。 I need to capture the output from a CMD or Powershell invoke and display the result as a string (human-readable).我需要捕获来自 CMD 或 Powershell 调用的输出并将结果显示为字符串(人类可读)。 Specifically, I'm trying to capture the output from get-bitlockervolume to check if the drives are encrypted.具体来说,我试图从 get-bitlockervolume 捕获输出以检查驱动器是否已加密。 The program should be able to run without using admin creds.该程序应该能够在不使用管理员凭据的情况下运行。

Get Powershell command's output when invoked through code unfortunately doesn't quite seem to work, so I thought I'd attempt to capture the output to a txt file and read it from there, but for some reason, the txt ends up being empty.不幸的是, 通过代码调用时获取 Powershell 命令的输出似乎不太有效,所以我想我会尝试将输出捕获到 txt 文件并从那里读取它,但由于某种原因,txt 最终为空。 For my latest attempts, I've ditched PowerShell and attempted to get it done using simple CMD.对于我最近的尝试,我已经放弃了 PowerShell 并尝试使用简单的 CMD 来完成它。

Process bl = new Process();
bl.StartInfo.WindowStyle = ProcessWindowStyle.Hidden ;
bl.StartInfo.FileName = "cmd.exe";
bl.StartInfo.Arguments = @"/c manage-bde -status > C:\windows\temp\bitlockerstatus.txt";
bl.StartInfo.RedirectStandardOutput = true;
bl.StartInfo.UseShellExecute = false;
bl.Start();

This seems to create the output file I was looking for in the right location, but it always turns up empty.这似乎在正确的位置创建了我正在寻找的输出文件,但它总是空的。 When running the command directly from cmd, there doesn't seem to be an issue recording the output.直接从 cmd 运行命令时,记录输出似乎没有问题。

I'm still quite new to C#, self-learning as I go along, so any suggestions for an alternate method/way of doing this would be appreciated.我对 C# 还是很陌生,在我进行过程中自学,因此对于替代方法/方式的任何建议将不胜感激。

Very late edit: I've figured out a way by using a Collection.很晚的编辑:我已经找到了使用集合的方法。 code:代码:

StringBuilder str = new Stringbuilder();

using (Powershell ps = Powershell.create())
     {
       ps.addscript ("manage-bde -status");
       Collection<PSObject> psoutp = ps.Invoke();

       foreach(PSObject outp in psoutp)
             {
                if (outp != null)
                   {
                      str.Append(outp);
                   }
                else str.Append ("Error");
             }

       return str.ToString();
      }

This for a Method that returns the output of manage-bde - status from powershell back to main.这是一个将 manage-bde - status 从 powershell 的输出返回到 main 的方法。 If there's no output at all (not even an errormessage) , it'll simply return "error" back to main.如果根本没有输出(甚至没有错误消息),它只会将“错误”返回给 main。 Here's hoping this helps someone else one day.希望有一天这可以帮助其他人。

I think the answer is here :我认为答案在这里

while (!proc.StandardOutput.EndOfStream)
{
    string line = proc.StandardOutput.ReadLine();
    // do something with line
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM