【发布时间】:2017-01-09 08:59:02
【问题描述】:
我尝试从以 c# 中的新进程开始的 powershell 脚本块获取返回值。
Process powershellProcess = null;
try
{
powershellProcess = new Process();
powershellProcess.StartInfo.FileName = "powershell.exe";
powershellProcess.StartInfo.Arguments = string.Format("-EncodedCommand {0}", isEnabledBase64String);
powershellProcess.StartInfo.RedirectStandardInput = false;
powershellProcess.StartInfo.RedirectStandardError = false;
powershellProcess.StartInfo.RedirectStandardOutput = false;
powershellProcess.StartInfo.UseShellExecute = false;
powershellProcess.StartInfo.CreateNoWindow = true;
bool value = powershellProcess.Start();
powershellProcess.BeginOutputReadLine();
while (!powershellProcess.HasExited)
{
Thread.Sleep(1000);
}
powershellProcess.CancelOutputRead();
}
catch (Exception ex)
{
throw ex;
}
finally
{
powershellProcess.Close();
}
例如脚本块“isEnabledBase64String”返回一个数字。 我怎样才能拿到号码?有什么建议吗? 谢谢
【问题讨论】:
-
解决方法:如果使用了 powershellProcess.OutputDataReceived += OnOutputDataReceived,则可以只使用 Write-Host xxx 并在 OnOutputDataReceived 中获取它。缺点:它是一个黑客。
标签: c# powershell