The easy way to run Powershell 2.0 using .NET Framework 4.0
2013-02-28 14:44
597 查看
http://viziblr.com/news/2012/5/16/the-easy-way-to-run-powershell-20-using-net-framework-40.html
If you've been exploring creating your own modules and cmdlets with Visual Studio, you've likely stumbled onto the following problem: you are unable to load your module using the import-module command because of an error stating that “This
assembly is built by a runtime newer than the currently loaded runtime and cannot be loaded.”
The reason is simple enough. When PowerShell 2.0 starts it is using the old .NET Framework's CLR (which begins which is version 2.) so it will be able to load it modules compiled against .Net Framework 3.5 (which continues to use the version 2 CLR) but not
.Net Framework 4.0 (which uses the version 3.0 CLR). This is perhaps a little surprising because Windows 7 comes with .NET Framework 4.0 installed.
You can see exactly what versions Powershell is using by examining the value of $PSVersionTable
Notice the value or CLRVersion begins with a "2".
In any case, this is a common occurrence with a straightforward solution. You can force PowerShell and PowerShell ISE to start using the .NET Framework 4.0 as
documented in this StackOverflow question. The procedure involves creating two small XML files and placing them in the appropriate place.
Being lazy, of course, we hate manual steps, so here is a small PowerShell script that will automatically create and place the necessary files. Be aware that the script as shown below will overwrite existing .config files.
$config_text = @"
<?xml version="1.0"?>
<configuration>
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0.30319"/>
<supportedRuntime version="v2.0.50727"/>
</startup>
</configuration>
"@
$config_text| Out-File $pshome\powershell.exe.config
$config_text| Out-File $pshome\powershell_ise.exe.config
Start PowerShell as an Administrator an then run the script.
Now restart PowerShell and examine the value of $PSVersionTable.
Notice the value or CLRVersion now begins with a "4".
And now your .NET 4.0 module will load correctly.
If you've been exploring creating your own modules and cmdlets with Visual Studio, you've likely stumbled onto the following problem: you are unable to load your module using the import-module command because of an error stating that “This
assembly is built by a runtime newer than the currently loaded runtime and cannot be loaded.”
The reason is simple enough. When PowerShell 2.0 starts it is using the old .NET Framework's CLR (which begins which is version 2.) so it will be able to load it modules compiled against .Net Framework 3.5 (which continues to use the version 2 CLR) but not
.Net Framework 4.0 (which uses the version 3.0 CLR). This is perhaps a little surprising because Windows 7 comes with .NET Framework 4.0 installed.
You can see exactly what versions Powershell is using by examining the value of $PSVersionTable
Notice the value or CLRVersion begins with a "2".
In any case, this is a common occurrence with a straightforward solution. You can force PowerShell and PowerShell ISE to start using the .NET Framework 4.0 as
documented in this StackOverflow question. The procedure involves creating two small XML files and placing them in the appropriate place.
Being lazy, of course, we hate manual steps, so here is a small PowerShell script that will automatically create and place the necessary files. Be aware that the script as shown below will overwrite existing .config files.
$config_text = @"
<?xml version="1.0"?>
<configuration>
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0.30319"/>
<supportedRuntime version="v2.0.50727"/>
</startup>
</configuration>
"@
$config_text| Out-File $pshome\powershell.exe.config
$config_text| Out-File $pshome\powershell_ise.exe.config
Start PowerShell as an Administrator an then run the script.
Now restart PowerShell and examine the value of $PSVersionTable.
Notice the value or CLRVersion now begins with a "4".
And now your .NET 4.0 module will load correctly.
相关文章推荐
- 【摘要】Advanced Serialization: Format Your Way to Success with the .NET Framework Versions 1.1 and 2.0
- .Net Framework Initialization Error – Unable to find a version of the runtime to run this applicati
- .Net Framework Initialization Error – Unable to find a version of the runtime to run this applicatio
- The way of using SqlCacheDependency in .net 2.0
- .NET错误The 'targetFramework' attribute in the <compilation> element of the Web.config file is used only to target version 4.0 and later of the .NET Framework
- .Net Framework Initialization Error – Unable to find a version of the runtime to run this applicatio
- Unhandled exceptions cause ASP.NET-based applications to unexpectedly quit in the .NET Framework 2.0
- The easy way to execute sudo command in Python using subprocess.Popen
- Net Framework Initialization Error – Unable to find a version of the runtime to run this applicatio
- 让 PowerShell 2.0 支持 DotNet FrameWork 4.0
- 黄聪:.Net Framework Initialization Error – Unable to find a version of the runtime to run this application解决方案
- .NET版本问题 转[.Net Framework Initialization Error – Unable to find a version of the runtime to run this applicatio]
- .Net Framework Initialization Error – Unable to find a version of the runtime to run
- You must use the Role Management Tool to install or configure Microsoft .NET Framework 3.5
- using powershell to check the hard disk information
- The coding gentleman's guide to detecting the .NET Framework
- [转载]How to hide the OK button in the dialog in .Net Compact Framework application?
- [转]Using the Interop Activity in a .NET Framework 4 Workflow
- HOW-TO: Handling cookies using the java.net.* API
- Unable to find the requested .Net Framework Data Provider