C# 调用 Powershell 执行命令

2022年10月9日 1176点热度 0人点赞 0条评论
内容纲要
private static void RunPowershellScript(string scriptFile)
{
    ProcessStartInfo ps = new ProcessStartInfo(@"powershell.exe",scriptFile) { RedirectStandardOutput = true };
    //ps.CreateNoWindow = true;
    var proc = Process.Start(ps);
    proc.OutputDataReceived += (s, e) =>
    {
        Console.WriteLine(e.Data);
    };
}

会弹出 Powershell 窗口,但是需要注意,PowerShell 不会输出完整内容。

如果需要 Powershell 输出执行的命令的所有日志,需要设置 RedirectStandardOutput = false

痴者工良

高级程序员劝退师

文章评论