在 Windows 8 上使用 PowerShell 2 作为默认版本 [英] Using PowerShell 2 as the default version on Windows 8

查看:49
本文介绍了在 Windows 8 上使用 PowerShell 2 作为默认版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 Windows 8 上使用 PowerShell 2 作为默认 PowerShell 版本,而不指定 -Version 开关.

我开始使用 PowerShell 3 附带的 Windows 8 RTM,但我的脚本与 PowerShell 3 不兼容.

解决方案

Powershell 使用 发布商政策(请参阅此处 还)自动将针对 Powershell 2 构建的主机重定向到 Powershell 3 运行时(如果可用).

大多数情况下,这正是您想要的,但是如果您的应用需要,您可以明确禁用发布者政策.

将此放在您的 app.config 文件中以禁用 System.Management.Automation(powershell 运行时)的发布者策略:

<预><代码><配置><运行时><assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"><依赖程序集><assemblyIdentity name="System.Management.Automation" publicKeyToken="31bf3856ad364e35"/><publisherPolicy apply="no"/></dependentAssembly></assemblyBinding></运行时></配置>

进行测试(针对 .NET 4.0 的控制台应用,明确引用 PS v2 运行时):

PowerShell ps = PowerShell.Create();ps.AddScript("$psversiontable");var result = ps.Invoke()[0].BaseObject 作为哈希表;Console.WriteLine("Powershell 版本:{0}", result["PSVersion"]);Console.WriteLine(".NET 版本:{0}", typeof(string).Assembly.GetName().Version);

在我的 Win8 机器上运行这个(PSv3 肯定在那里),我得到了

的结果

Powershell 版本:2.0.NET 版本:4.0.0.0

如果我注释掉 app.config,PS 版本会变成 3.0.

I would like to use PowerShell 2 as the default PowerShell version on Windows 8 without specifying the -Version switch.

I started using Windows 8 RTM which comes with PowerShell 3, and I have scripts that are not compatible with PowerShell 3.

解决方案

Powershell uses a publisher policy (see here also) to automatically redirect hosts built against Powershell 2 onto the Powershell 3 runtime if it's available.

Most of the time this is exactly what you want, however you can explicitly disable the publisher policy if needed for your app.

Put this in your app.config file to disable the publisher policy for System.Management.Automation (powershell runtime):

<configuration>
  <runtime>
     <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
       <dependentAssembly>
         <assemblyIdentity name="System.Management.Automation" publicKeyToken="31bf3856ad364e35" />
         <publisherPolicy apply="no" />
      </dependentAssembly>
     </assemblyBinding>
  </runtime>
</configuration>

Testing it out (console app targeted to .NET 4.0, with explicit reference to PS v2 runtime):

PowerShell ps = PowerShell.Create();
ps.AddScript("$psversiontable");
var result = ps.Invoke()[0].BaseObject as Hashtable;

Console.WriteLine("Powershell version: {0}", result["PSVersion"]);
Console.WriteLine(".NET version: {0}", typeof(string).Assembly.GetName().Version);

Running this on my Win8 box (PSv3 definitely there), I get result of

Powershell version: 2.0
.NET version: 4.0.0.0

And PS version goes to 3.0 if I comment out app.config.

这篇关于在 Windows 8 上使用 PowerShell 2 作为默认版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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