简体   繁体   English

我的 PowerShell 脚本使用哪个 .NET 版本?

[英]Which .NET version is my PowerShell script using?

I'd like to use .NET in some PowerShell scripts I'm about to write -- how do I know/declare which version of .NET I'm dealing with when these scripts run?我想在我即将编写的一些 PowerShell 脚本中使用 .NET——当这些脚本运行时,我如何知道/声明我正在处理哪个版本的 .NET?

And is it possible to choose against which version of .NET my script will run?是否可以选择我的脚本运行哪个版本的 .NET?

On PowerShell 2.0, just take a peek at the $PSVersionTable variable: 在PowerShell 2.0上,只需看一下$PSVersionTable变量:

PS> $psversiontable

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

On PowerShell 1.0, use [System.Environment]::Version : 在PowerShell 1.0上,使用[System.Environment]::Version

PS> [Environment]::Version

Major  Minor  Build  Revision
-----  -----  -----  --------
2      0      50727  4927

...no, you cannot choose which .NET version you can run the script under -- George Howarth ...不,您无法选择可以运行脚本的.NET版本 - George Howarth

Woah, that's not true! 哇,那不是真的! You can specify which version of .NET that PowerShell uses. 可以指定PowerShell使用的.NET版本。 The key is the .NET standard application configuration file, which takes the form [appname].exe.config. 关键是.NET标准应用程序配置文件,其格式为[appname] .exe.config。 You can drop that in the same directory as most .NET applications -- including the PowerShell and PowerShell ISE executables -- and the CLR will automatically load any recognizable options specified within the configuration file. 您可以将其放在与大多数.NET应用程序相同的目录中 - 包括PowerShell和PowerShell ISE可执行文件 - 并且CLR将自动加载配置文件中指定的任何可识别选项。 One of those options is the CLR version you want the application to use. 其中一个选项是您希望应用程序使用的CLR版本。

This is documented in detail in the question: How can I run PowerShell with the .NET 4 runtime? 这在问题中有详细记录: 如何使用.NET 4运行时运行PowerShell? . In particular, see Emperor XLII 's post. 特别是,看看Emperor XLII的帖子。

To get the .NET version: 要获得.NET版本:

[System.Reflection.Assembly]::GetExecutingAssembly().ImageRuntimeVersion

...which is, by default, the version of the CLR the assembly ( System.Management.Automation.dll ) compiled under. ...默认情况下,编译的程序集( System.Management.Automation.dll )的CLR版本。

And no, you cannot choose which .NET version you can run the script under. 不,你不能选择你可以运行脚本的.NET版本。

The .NET version can be inferred from the version of mscorlib. 可以从mscorlib的版本推断出.NET版本。 So you can do the following in PowerShell to output the current version of .NET: 因此,您可以在PowerShell中执行以下操作以输出当前版本的.NET:

$a = [System.Reflection.Assembly]::Load("mscorlib")
$a.GetName().Version

This is an old thread, and the answer I am going to post now will not work for .NET versions from from before circa 2017.这是一个旧线程,我现在要发布的答案不适用于大约 2017 年之前的 .NET 版本。

There is a new FrameworkDescription property .有一个新的FrameworkDescription属性

Try:尝试:

[System.Runtime.InteropServices.RuntimeInformation]::FrameworkDescription

Note, however, on some versions of Windows PowerShell (not sure about newer PowerShell 6 and 7 etc.) there are two different types System.Runtime.InteropServices.RuntimeInformation in different assemblies.但是请注意,在某些版本的Windows PowerShell(不确定较新的 PowerShell 6 和 7 等)上,不同程序集中有两种不同类型的System.Runtime.InteropServices.RuntimeInformation And only one of them has the property: So you must qualify:而且其中只有一个具有属性: 所以你必须符合:

[System.Runtime.InteropServices.RuntimeInformation, Microsoft.Powershell.PSReadline]::FrameworkDescription   # does not exist
[System.Runtime.InteropServices.RuntimeInformation, mscorlib]::FrameworkDescription   # good

Check out the article Hey, Scripting Guy! 看看文章Hey,Scripting Guy! How Do I Check Which Version of Windows PowerShell I'm Using? 如何检查我正在使用的Windows PowerShell版本? . It shows where in the registry you can check to determine this. 它显示了注册表中的位置,您可以检查以确定这一点。

PS > [Runtime.InteropServices.RuntimeEnvironment]::GetRuntimeDirectory() PS> [Runtime.InteropServices.RuntimeEnvironment] :: GetRuntimeDirectory()
C:\\Windows\\Microsoft.NET\\Framework\\v2.0.50727\\ C:\\ WINDOWS \\ Microsoft.NET \\框架\\ V2.0.50727 \\

I've found out that you can look for that information in the directory C:\\Windows\\Microsoft.NET\\Framework: 我发现你可以在目录C:\\ Windows \\ Microsoft.NET \\ Framework中查找该信息:

cd C:\Windows\Microsoft.NET\Framework
dir

The directories inside that one will tell you the versions of the framework installed. 那个里面的目录会告诉你安装的框架的版本。

Mode                LastWriteTime     Length Name
----                -------------     ------ ----
d----        14/07/2009     10:48            3082
d----        14/07/2009      4:37            v1.0.3705
d----        14/07/2009      4:37            v1.1.4322
d----        25/06/2010     17:26            v2.0.50727
d----        14/07/2009     10:48            v3.0
d----        14/07/2009     10:48            v3.5

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM