当我运行这个C#代码时,它会给出以下错误。
代码:
Runspace runspace = RunspaceFactory.CreateRunspace();
runspace.Open();
RunspaceInvoke runSpaceInvoker = new RunspaceInvoke(runspace);
runSpaceInvoker.Invoke("Set-ExecutionPolicy Unrestricted");
Pipeline pipeline = runspace.CreatePipeline();
Command command = new Command("ps\\script.ps1");
string StorageName = "appstorage";
command.Parameters.Add("Name", StorageName.ToLower());
pipeline.Commands.Add(command);
pipeline.Invoke();
runspace.Close();
错误:'System.Management.Automation.SetValueInvocationException‘发生在System.Management.Automation.dll中 异常设置"ForegroundColor":“由于枚举值无效,无法将null转换为"System.ConsoleColor”。请指定下列枚举值之一,然后重试。可能的枚举值为"Black、DarkBlue、DarkGreen、DarkCyan、DarkRed、DarkMagenta、DarkYellow、Gray、DarkGray、Blue、Green、Cyan、Red、Magenta、黄色、白色“。
发布于 2017-04-25 16:44:27
我也有同样的问题。脚本在控制台中运行良好,但我从C#获得了相同的消息。
至少在我的例子中,错误是由脚本中包含的CLS命令生成的。为执行PowerShell而创建的运行空间没有要清除的屏幕,因此CLS调用失败。使用CLS命令注释行后,脚本将从C#正确执行。
https://stackoverflow.com/questions/34204962
复制相似问题