从C#调用特定版本的PowerShell [英] Call a specific version of PowerShell from C#

查看:216
本文介绍了从C#调用特定版本的PowerShell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Hyper-V主机上使用从C#调用的Get-VM Cmdlet.

I'm trying to use the Get-VM Cmdlet called from C# on a Hyper-V host.

显然,必须首先导入相应的PowerShell模块Hyper-V.但是,导入失败-显然是因为该模块仅在PowerShell 3.0上受支持(至少我从

Obviously, the according PowerShell module Hyper-V has to be imported first. The import fails, however - apparently because the module is supported only on PowerShell 3.0 (at least that's what I figure from this article). The PowerShell used by System.Management.Automation seems to be version 2.0, though.

InitialSessionState iss = InitialSessionState.CreateDefault();
iss.ImportPSModule(new string[] { "Hyper-V" });
Runspace runSpace = RunspaceFactory.CreateRunspace(iss);
runSpace.Open();
foreach (var err in (ArrayList)runSpace
        .SessionStateProxy.PSVariable.GetValue("Error"))
    Console.WriteLine(err.ToString());
runSpace.Close();

返回

'C:\ Windows \ system32 \ WindowsPowerShell \ v1.0 \ Modules \ Hyper-V \ Hyper-V.psd1' 无法导入模块,因为其清单包含一个或多个成员 那是无效的.有效的清单成员是('ModuleToProcess',...). 删除无效的成员("HelpInfoUri"), 然后尝试再次导入该模块.

The 'C:\Windows\system32\WindowsPowerShell\v1.0\Modules\Hyper-V\Hyper-V.psd1' module cannot be imported because its manifest contains one or more members that are not valid. The valid manifest members are ('ModuleToProcess', ...). Remove the members that are not valid ('HelpInfoUri'), then try to import the module again.

是否可以在C#中使用特定版本的PowerShell?

Is there a way to use a specific version of PowerShell in C#?

推荐答案

一位同事想通了:

显然,.NET 4+附带了一个全新的公共语言运行时:CLR4
此运行时使用从位于C:\Windows\Microsoft.NET\assembly的新程序集缓存中加载的自己的程序集.

Apparently, .NET 4+ comes with an all new common language runtime: the CLR4
This runtime uses its own assemblies loaded from a new assembly cache located at C:\Windows\Microsoft.NET\assembly.

System.Management.Automation version 3.0.0.0将自动使用PowerShell 3.0,仅适用于CLR4.因为我将应用程序配置为在.NET 3.5下运行,所以它将使用旧的CLR2,甚至看不到较新的程序集.

The System.Management.Automation version 3.0.0.0, which will automatically use PowerShell 3.0, exists for the CLR4 only. Because I configured my application to run under .NET 3.5, it would use the old CLR2 and could not even see the newer assembly.

要确保该应用程序仍能在.NET 3.5上运行,请将其添加到项目文件夹中的App.config文件中:

To make sure the application would still run on .NET 3.5, add this to the App.config file in the project folder:

<supportedRuntime version="v4.0"/> 
<supportedRuntime version="v2.0.50727"/>


如果CLR4可用,它将加载相应的GAC,找到一个策略文件,该文件将所有对System.Management.Automation version 1.0.0.0的引用重定向到version 3.0.0.0,并且PowerShell模块按预期工作. 如果只有.NET 3.5,则将加载较早的版本;否则,将重新加载. PowerShell仍然可以运行,但仅在2.0版以下.


If CLR4 is available, it'll load the according GAC, find a policy file that redirects all references to System.Management.Automation version 1.0.0.0 to version 3.0.0.0 and the PowerShell-Modules work as expected.
If you only have .NET 3.5, the older version will be loaded; PowerShell still works, but only up to version 2.0.

这篇关于从C#调用特定版本的PowerShell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆