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

前端 未结 11 2168
走了就别回头了
走了就别回头了 2020-11-22 07:38

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 Power

相关标签:
11条回答
  • 2020-11-22 08:03

    If you only need to execute a single command, script block, or script file in .NET 4, try using Activation Configuration Files from .NET 4 to start only a single instance of PowerShell using version 4 of the CLR.

    Full details:

    http://blog.codeassassin.com/2011/03/23/executing-individual-powershell-commands-using-net-4/

    An example PowerShell module:

    https://gist.github.com/882528

    0 讨论(0)
  • 2020-11-22 08:04

    If you don't want to modify the registry or app.config files, an alternate way is to create a simple .NET 4 console app that mimicks what PowerShell.exe does and hosts the PowerShell ConsoleShell.

    See Option 2 – Hosting Windows PowerShell yourself

    First, add a reference to the System.Management.Automation and Microsoft.PowerShell.ConsoleHost assemblies which can be found under %programfiles%\Reference Assemblies\Microsoft\WindowsPowerShell\v1.0

    Then use the following code:

    using System;
    using System.Management.Automation.Runspaces;
    using Microsoft.PowerShell;
    
    namespace PSHostCLRv4
    {
        class Program
        {
            static int Main(string[] args)
            {
                var config = RunspaceConfiguration.Create();
                    return ConsoleShell.Start(
                    config,
                    "Windows PowerShell - Hosted on CLR v4\nCopyright (C) 2010 Microsoft Corporation. All rights reserved.",
                    "",
                    args
                );
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-22 08:05

    Just run powershell.exe with COMPLUS_version environment variable set to v4.0.30319. For example, from cmd.exe or .bat-file:

    set COMPLUS_version=v4.0.30319
    powershell -file c:\scripts\test.ps1
    
    0 讨论(0)
  • 2020-11-22 08:14

    Just as another option, the latest PoshConsole release includes binaries targeted to .NET 4 RC (which work fine against the RTM release) without any configuration.

    0 讨论(0)
  • 2020-11-22 08:15

    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.

    0 讨论(0)
提交回复
热议问题