在安装了PowerShell 2.0和PowerShell 3.0的计算机上,如何在创建C#时选择从RunSpace应用程序启动?
似乎有各种各样的配置参数,但没有一个可以控制启动哪个版本的PowerShell。我可以想象它可能基于调用进程所使用的.NET版本,但是当您调用RunspaceFactory.CreateOutOfProcessRunspace时又会怎样呢?在这种情况下,这应该无关紧要,对吧?
发布于 2014-10-29 04:05:52
您需要使用带有PowerShellProcessInstance的CreateOutOfProcessRunspace重载,并且需要在创建PowerShellProcessInstance时指定版本。
PowerShellProcessInstance instance = new PowerShellProcessInstance(new Version(2, 0), null, null, false);
using (Runspace rs = RunspaceFactory.CreateOutOfProcessRunspace(new TypeTable(new string[0]), instance))
{
rs.Open();
using (PowerShell ps = PowerShell.Create())
{
ps.Runspace = rs;
ps.AddScript("$PSVersionTable");
dynamic vt = ps.Invoke().Single();
Console.WriteLine(vt.PSVersion); // This will output "2.0"
}
}发布于 2014-03-14 01:50:38
PowerShell v3附带了一个新的3.0版本的System.Management.Automation.dll。您可以尝试在启动代码早期使用Assembly.Load()抢先加载3.0 SMA dll,以加载PowerShell v3。
https://stackoverflow.com/questions/22383915
复制相似问题