【问题标题】:Possible to specify PowerShell CLR Version as an argument可以将 PowerShell CLR 版本指定为参数
【发布时间】:2015-07-24 13:39:34
【问题描述】:

是否可以通过我们需要的 .NET CLR 版本来运行 powershell?

使用 Powershell V2 的系统默认使用 CLR 版本 2,我们需要版本 4。

我们可以实现一个配置文件来:

  • C:\Windows\System32\WindowsPowerShell\v1.0\PowerShell.exe.config
  • C:\Windows\System32\WindowsPowerShell\v1.0\PowerShellISE.Exe.config
  • C:\Windows\SysWOW64\WindowsPowerShell\v1.0\PowerShell.exe.config
  • C:\Windows\SysWOW64\WindowsPowerShell\v1.0\PowerShellISE.exe.config

点赞this

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

但我们不希望对客户服务器进行更改,这可能会破坏其他依赖 PowerShell 的程序。

【问题讨论】:

  • This question 有一些有趣的答案,特别是 this one 关于使用 .NET 4 一次性调用的激活配置文件。

标签: c# windows powershell


【解决方案1】:

如果不满足最低要求,您可以检查脚本中的 CLR 版本并退出并返回错误:

if ($PSVersionTable.CLRVersion.Major -lt 4) {
  Write-Error 'CLR Version 4.0 or newer required.'
  exit 1
}

或者,如果检查最低 PowerShell 版本也可以,您可以将 #requires directive 作为脚本的第一行:

#requires -version 4.0

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-09
    • 2017-03-09
    • 2011-12-15
    • 1970-01-01
    • 2021-12-02
    • 2014-12-31
    • 2012-01-16
    • 1970-01-01
    相关资源
    最近更新 更多