Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在C#中运行Psexec.exe时捕获%ERRORLEVEL%,保存到日志文件_C#_Streamwriter_Psexec_Errorlevel_Console.setout - Fatal编程技术网

如何在C#中运行Psexec.exe时捕获%ERRORLEVEL%,保存到日志文件

如何在C#中运行Psexec.exe时捕获%ERRORLEVEL%,保存到日志文件,c#,streamwriter,psexec,errorlevel,console.setout,C#,Streamwriter,Psexec,Errorlevel,Console.setout,我正在使用psexec.exe在大量计算机上安装一些软件。我希望能够捕获%ERRORLEVEL%(或其c#等价物)并将其写入TXT文件。(cmd.exe在6440-nbpb00f51w上退出,错误代码为0。) 经过广泛的研究,我发现了几种可能性。我的最终目标是拥有一个.CSV文件。每一行都有计算机名和psexec命令的输出。使用这些数据,我将能够确定更新在哪些计算机上成功 string[] pcnamearray = new string[] {"COMPUTERNAME1", "COMPUTE

我正在使用psexec.exe在大量计算机上安装一些软件。我希望能够捕获%ERRORLEVEL%(或其c#等价物)并将其写入TXT文件。(cmd.exe在6440-nbpb00f51w上退出,错误代码为0。)

经过广泛的研究,我发现了几种可能性。我的最终目标是拥有一个.CSV文件。每一行都有计算机名和psexec命令的输出。使用这些数据,我将能够确定更新在哪些计算机上成功

string[] pcnamearray = new string[] {"COMPUTERNAME1", "COMPUTERNAME2"};
foreach (string i in pcnamearray)
{
    Process p = new Process();
    string psexeclaunch = "psexec.exe";
    p.StartInfo.FileName = psexeclaunch;
    p.StartInfo.RedirectStandardOutput = false;
    p.StartInfo.UseShellExecute = true;
    p.StartInfo.CreateNoWindow = false;
    string arg = "/silent";
    p.StartInfo.Arguments = @" -i \\" + i + @" -u mydomain\admin -p mypassword -h                -e cmd.exe /c start \\networkserver\sw\myfile.exe" + arg;
    p.Start();
}
我不是在这里找人为我写代码的人。我在这里征求人们的意见,或者过去的经验。我知道有几种不同的方法可用。理想情况下,我希望每个结果都有一个简短的甜线,但如果我必须获得整个输出并使用宏将其精简为更可读的形式,那也没关系

到目前为止,我已经尝试:

Console.WriteLine(p.StandardOutput.ReadToEnd());
而且

System.IO.File.WriteAllLines(@"C:\Users\areu2447\Desktop\UpdateDAT\Final\out.txt", lines);
FileStream filestream = new FileStream(@"C:\Users\areu2447\Desktop\UpdateDAT\Final\out.txt", FileMode.Append);
var streamwriter = new StreamWriter(filestream);
streamwriter.AutoFlush = true;
Console.SetOut(streamwriter);
Console.SetError(streamwriter);
而且

System.IO.File.WriteAllLines(@"C:\Users\areu2447\Desktop\UpdateDAT\Final\out.txt", lines);
FileStream filestream = new FileStream(@"C:\Users\areu2447\Desktop\UpdateDAT\Final\out.txt", FileMode.Append);
var streamwriter = new StreamWriter(filestream);
streamwriter.AutoFlush = true;
Console.SetOut(streamwriter);
Console.SetError(streamwriter);
最后一个在我看来似乎是最好的一个,但似乎无法让它发挥作用。我已经尝试将它包含在我的foreach循环中,但另一个进程正在使用该文件,所以在查看它之后,我发现它需要独立运行。即使只有它,我仍然无法让它工作

请帮助/建议

我能够通过使用以下命令,让它实际修改我的TXT文件,即使没有添加任何内容:

                    System.IO.StreamReader reader = p.StandardOutput;
                    String sRes = reader.ReadToEnd();
                    StreamWriter SW;
                    SW = File.CreateText(@"C:\Users\areu2447\Desktop\UpdateDAT\Final\out.txt");
                    SW.WriteLine(sRes);
                    SW.Close();
                    Console.WriteLine("File Created Successfully");
                    reader.Close();

我建议使用System.Management.Automation创建PowerShell管道并在过程中运行脚本


您是否正在寻找
流程。退出代码