PowerRhell을 사용하여 PowerShell 스크립트의 출력을 로그 창에 기록하는 콘솔을 만들 수있는 MVC4 웹 응용 프로그램을 만들고 있습니다. 현재 내가 일한 것은 Capturing Powershell output in C# after Pipeline.Invoke throws입니다. 문제는이 방법을 사용하여 클라이언트에게 실시간 출력 스트림을 전달할 수 없다는 것입니다. 스크립트 처리가 완료되면 출력물을 공급할 수 있습니다. 현재 나는 이것을 시도하고 있지만 출력을받지 못하고있다.Powershell 스크립트의 출력 얻기
var loggingHub = new LoggingHub();
string powerShellExeLocation = null;
var localKey = Registry.LocalMachine;
var subKey = localKey.OpenSubKey(@"SOFTWARE\Microsoft\PowerShell\1\PowerShellEngine");
powerShellExeLocation = subKey.GetValue("ApplicationBase").ToString();
if (!Directory.Exists(powerShellExeLocation))
throw new Exception("Cannot locate the PowerShell dir.");
powerShellExeLocation = Path.Combine(powerShellExeLocation, "powershell.exe");
if (!File.Exists(powerShellExeLocation))
throw new Exception("Cannot locate the PowerShell executable.");
if (!File.Exists(scriptFile))
throw new Exception("Cannot locate the PowerShell script.");
var processInfo = new ProcessStartInfo
{
Verb = "runas",
UseShellExecute = false,
RedirectStandardOutput = true
};
processInfo.RedirectStandardOutput = true;
processInfo.WorkingDirectory = Environment.CurrentDirectory;
processInfo.FileName = powerShellExeLocation;
//processInfo.Arguments = "-NoLogo -OutputFormat Text -NonInteractive -WindowStyle Hidden -ExecutionPolicy Unrestricted -File \"" + scriptFile + "\" ";
processInfo.Arguments = build;
var powerShellProcess = new Process {StartInfo = processInfo};
powerShellProcess.Start();
while (!powerShellProcess.HasExited)
{
loggingHub.Send(DateTime.Now.ToString("h:mm:ss tt"), "info", powerShellProcess.StandardOutput.ReadLine());
}