[英]How to get the return value from a powershell scriptblock started in c#
I try to get a return value from a powershell scriptblock started with new Process in c#. 我尝试从以C#中的新Process开始的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();
}
Eg the scriptblock "isEnabledBase64String" returns a number. 例如,脚本块“ isEnabledBase64String”返回一个数字。 How do I get to the number?
我如何获得电话号码? Any advice?
有什么建议吗? Thanks
谢谢
Do you have to start a process to execute the powershell script? 您是否必须启动一个过程来执行Powershell脚本? You can use powershell instance object from the System.Management.Automation.
您可以使用System.Management.Automation中的Powershell实例对象。
See example at windows powershell from c# https://blogs.msdn.microsoft.com/kebab/2014/04/28/executing-powershell-scripts-from-c/ 请参阅Windows Powershell上的示例(来自C# https: //blogs.msdn.microsoft.com/kebab/2014/04/28/executing-powershell-scripts-from-c/
this will give you much more control over execution of script, passing the param as well as reading the output. 这将使您对脚本的执行,传递参数以及读取输出有更多控制。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.