How can I run PowerShell with the .NET 4 runtime?
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.
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.
How can I run PowerShell with the .NET 4 runtime?
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
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>
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.
Remove the registry keys when you don't need them any more. These are machine-wide keys and forcibly migrate ALL applications to .NET 4.0, even applications using .net 2 and .net 3.5
The best solution I have found is in the blog post Using Newer Version(s) of .NET with PowerShell. This allows powershell.exe to run with .NET 4 assemblies.
Simply modify (or create) $pshomepowershell.exe.config
so that it contains the following:
<?xml version="1.0"?>
<configuration>
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0.30319"/>
<supportedRuntime version="v2.0.50727"/>
</startup>
</configuration>
Additional, quick setup notes:
Locations and files are somewhat platform dependent; however will give you an inline gist of how to make the solution work for you.
cd $pshome
in the Powershell window (doesn't work from DOS prompt). C:WindowsSystem32WindowsPowerShellv1.0
powershell.exe.config
if your PowerShell.exe
is being executed (create the config file if need be). PowerShellISE.Exe
is running then you need to create its companion config file as PowerShellISE.Exe.config
Please be VERY careful with using the registry key approach. These are machine-wide keys and forcibily migrate ALL applications to .NET 4.0.
Many products do not work if forcibily migrated and this is a testing aid and not a production quality mechanism. Visual Studio 2008 and 2010, MSBuild, turbotax, and a host of websites, SharePoint and so on should not be automigrated.
If you need to use PowerShell with 4.0, this should be done on a per-application basis with a configuration file, you should check with the PowerShell team on the precise recommendation. This is likely to break some existing PowerShell commands.
链接地址: http://www.djcxy.com/p/4942.html