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

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

如何使用。net 4运行时运行PowerShell ?


当前回答

下面是我用来支持。net 2.0和。net 4程序集的配置文件的内容:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <!-- http://msdn.microsoft.com/en-us/library/w4atty68.aspx -->
  <startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0" />
    <supportedRuntime version="v2.0.50727" />
  </startup>
</configuration>

此外,这是一个简化版本的PowerShell 1.0兼容代码,我用来执行我们的脚本从传递的命令行参数:

class Program {
  static void Main( string[] args ) {
    Console.WriteLine( ".NET " + Environment.Version );

    string script = "& " + string.Join( " ", args );
    Console.WriteLine( script );
    Console.WriteLine( );

    // Simple host that sends output to System.Console
    PSHost host = new ConsoleHost( this );
    Runspace runspace = RunspaceFactory.CreateRunspace( host );

    Pipeline pipeline = runspace.CreatePipeline( );
    pipeline.Commands.AddScript( script );

    try {
      runspace.Open( );
      IEnumerable<PSObject> output = pipeline.Invoke( );
      runspace.Close( );

      // ...
    }
    catch( RuntimeException ex ) {
      string psLine = ex.ErrorRecord.InvocationInfo.PositionMessage;
      Console.WriteLine( "error : {0}: {1}{2}", ex.GetType( ), ex.Message, psLine );
      ExitCode = -1;
    }
  }
}

除了上面所示的基本错误处理之外,我们还在脚本中注入了一个trap语句,以显示额外的诊断信息(类似于Jeffrey Snover的Resolve-Error函数)。

其他回答

如果您只需要在。net 4中执行单个命令、脚本块或脚本文件,请尝试使用。net 4中的激活配置文件,使用CLR版本4仅启动单个PowerShell实例。

详情:

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

PowerShell模块示例:

https://gist.github.com/882528

我找到的最好的解决方案是在PowerShell中使用更新版本的。net的博文中。这允许powershell.exe与。net 4程序集一起运行。

只需修改(或创建)$pshome\powershell.exe。配置,使其包含以下内容:

<?xml version="1.0"?> 
<configuration> 
    <startup useLegacyV2RuntimeActivationPolicy="true"> 
        <supportedRuntime version="v4.0.30319"/> 
        <supportedRuntime version="v2.0.50727"/> 
    </startup> 
</configuration> 

另外,快速设置注意事项:

位置和文件多少依赖于平台;然而,将为您提供如何使解决方案为您工作的内联要点。

您可以通过在PowerShell窗口中执行cd $ phome来查找PowerShell在计算机上的位置(不能从DOS提示符中工作)。 路径将类似于(示例)C:\Windows\System32\WindowsPowerShell\v1.0\ 如果正在执行PowerShell.exe,则将配置放入的文件名为:PowerShell.exe .config(如果需要,则创建配置文件)。 如果PowerShellISE.Exe正在运行,那么您需要创建它的配套配置文件PowerShellISE.Exe.config

只需运行powershell.exe,并将COMPLUS_version环境变量设置为v4.0.30319。 例如,从cmd.exe或。bat-file:

set COMPLUS_version=v4.0.30319
powershell -file c:\scripts\test.ps1

下面是我用来支持。net 2.0和。net 4程序集的配置文件的内容:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <!-- http://msdn.microsoft.com/en-us/library/w4atty68.aspx -->
  <startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0" />
    <supportedRuntime version="v2.0.50727" />
  </startup>
</configuration>

此外,这是一个简化版本的PowerShell 1.0兼容代码,我用来执行我们的脚本从传递的命令行参数:

class Program {
  static void Main( string[] args ) {
    Console.WriteLine( ".NET " + Environment.Version );

    string script = "& " + string.Join( " ", args );
    Console.WriteLine( script );
    Console.WriteLine( );

    // Simple host that sends output to System.Console
    PSHost host = new ConsoleHost( this );
    Runspace runspace = RunspaceFactory.CreateRunspace( host );

    Pipeline pipeline = runspace.CreatePipeline( );
    pipeline.Commands.AddScript( script );

    try {
      runspace.Open( );
      IEnumerable<PSObject> output = pipeline.Invoke( );
      runspace.Close( );

      // ...
    }
    catch( RuntimeException ex ) {
      string psLine = ex.ErrorRecord.InvocationInfo.PositionMessage;
      Console.WriteLine( "error : {0}: {1}{2}", ex.GetType( ), ex.Message, psLine );
      ExitCode = -1;
    }
  }
}

除了上面所示的基本错误处理之外,我们还在脚本中注入了一个trap语句,以显示额外的诊断信息(类似于Jeffrey Snover的Resolve-Error函数)。

如果您还停留在PowerShell 1.0或2.0版本上,下面是我对Jason Stangroome的精彩回答的改进。

创建一个powershell4。CMD在你路径的某处,包含以下内容:

@echo off
:: http://stackoverflow.com/questions/7308586/using-batch-echo-with-special-characters
if exist %~dp0powershell.exe.activation_config goto :run
echo.^<?xml version="1.0" encoding="utf-8" ?^>                 > %~dp0powershell.exe.activation_config
echo.^<configuration^>                                        >> %~dp0powershell.exe.activation_config
echo.  ^<startup useLegacyV2RuntimeActivationPolicy="true"^>  >> %~dp0powershell.exe.activation_config
echo.    ^<supportedRuntime version="v4.0"/^>                 >> %~dp0powershell.exe.activation_config
echo.  ^</startup^>                                           >> %~dp0powershell.exe.activation_config
echo.^</configuration^>                                       >> %~dp0powershell.exe.activation_config
:run
:: point COMPLUS_ApplicationMigrationRuntimeActivationConfigPath to the directory that this cmd file lives in
:: and the directory contains a powershell.exe.activation_config file which matches the executable name powershell.exe
set COMPLUS_ApplicationMigrationRuntimeActivationConfigPath=%~dp0
%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe %*
set COMPLUS_ApplicationMigrationRuntimeActivationConfigPath=

这将允许您启动在。net 4.0下运行的powershell控制台实例。

通过检查从cmd运行的以下两个命令的输出,您可以看到在我的PowerShell 2.0系统上的差异。

C:\>powershell -ExecutionPolicy ByPass -Command $PSVersionTable

Name                           Value
----                           -----
CLRVersion                     2.0.50727.5485
BuildVersion                   6.1.7601.17514
PSVersion                      2.0
WSManStackVersion              2.0
PSCompatibleVersions           {1.0, 2.0}
SerializationVersion           1.1.0.1
PSRemotingProtocolVersion      2.1


C:\>powershell4.cmd -ExecutionPolicy ByPass -Command $PSVersionTable

Name                           Value
----                           -----
PSVersion                      2.0
PSCompatibleVersions           {1.0, 2.0}
BuildVersion                   6.1.7601.17514
CLRVersion                     4.0.30319.18408
WSManStackVersion              2.0
PSRemotingProtocolVersion      2.1
SerializationVersion           1.1.0.1