如何使用 .NET 4 运行时运行 PowerShell? [英] How can I run PowerShell with the .NET 4 runtime?

查看:38
本文介绍了如何使用 .NET 4 运行时运行 PowerShell?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在更新管理一些 .NET 程序集的 PowerShell 脚本.该脚本是为针对 .NET 2(与 PowerShell 运行的框架的相同版本)构建的程序集编写的,但现在需要使用 .NET 4 程序集以及 .NET 2 程序集.

I am updating a PowerShell script that manages some .NET assemblies. The script was written for assemblies built against .NET 2 (the same version of the framework that PowerShell runs with), but now needs to work with .NET 4 assemblies as well as .NET 2 assemblies.

由于 .NET 4 支持运行针对旧版本框架构建的应用程序,因此当我需要针对 .NET 4 程序集运行 PowerShell 时,似乎最简单的解决方案是使用 .NET 4 运行时启动 PowerShell.

Since .NET 4 supports running applications built against older versions of the framework, it seems like the simplest solution is to launch PowerShell with the .NET 4 runtime when I need to run it against .NET 4 assemblies.

如何使用 .NET 4 运行时运行 PowerShell?

How can I run PowerShell with the .NET 4 runtime?

推荐答案

PowerShell(引擎)在 .NET 4.0 下运行良好.PowerShell(控制台主机和 ISE)没有,仅仅因为它们是编译过的针对旧版本的 .NET.有一个注册表设置将更改在系统范围内加载的 .NET 框架,进而允许 PowerShell 使用 .NET 4.0 类:

PowerShell (the engine) runs fine under .NET 4.0. PowerShell (the console host and the ISE) do not, simply because they were compiled against older versions of .NET. There's a registry setting that will change the .NET framework loaded systemwide, which will in turn allow PowerShell to use .NET 4.0 classes:

reg add hklmsoftwaremicrosoft.netframework /v OnlyUseLatestCLR /t REG_DWORD /d 1
reg add hklmsoftwarewow6432nodemicrosoft.netframework /v OnlyUseLatestCLR /t REG_DWORD /d 1

要仅更新 ISE 以使用 .NET 4.0,您可以将配置 ($psHomepowershell_ise.exe.config) 文件更改为具有如下块:

To update just the ISE to use .NET 4.0, you can change the configuration ($psHomepowershell_ise.exe.config) file to have a chunk like this:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <startup>
      <supportedRuntime version="v4.0.30319" />
    </startup>
</configuration>

您可以使用 PowerShell API (System.Management.Automation.PowerShell) 构建调用 PowerShell 的 .NET 4.0 应用程序就好了,但这些步骤将有助于让内置的 PowerShell 主机在 .NET 4.0 下工作.

You can build .NET 4.0 applications that call PowerShell using the PowerShell API (System.Management.Automation.PowerShell) just fine, but these steps will help get the in-the-box PowerShell hosts to work under .NET 4.0.

当您不再需要注册表项时删除它们.这些是机器范围的密钥,强制将所有应用程序迁移到 .NET 4.0,甚至是使用 .net 2 和 .net 3.5 的应用程序

这篇关于如何使用 .NET 4 运行时运行 PowerShell?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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