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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3344855/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 14:33:37  来源:igfitidea点击:

Which .NET version is my PowerShell script using?

.netpowershellversion

提问by lance

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 版本?

采纳答案by Keith Hill

On PowerShell 2.0, just take a peek at the $PSVersionTablevariable:

在 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

回答by George Howarth

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 版本下运行脚本。

回答by Joshua Honig

...no, you cannot choose which .NET version you can run the script under -- George Howarth

...不,您不能选择可以在哪个 .NET 版本下运行脚本 -- George Howarth

Woah, that's not true! You canspecify which version of .NET that PowerShell uses. The key is the .NET standard application configuration file, which takes the form [appname].exe.config. You can drop that in the same directory as most .NET applications -- including the PowerShell and PowerShell ISEexecutables -- and the CLR will automatically load any recognizable options specified within the configuration file. One of those options is the CLR version you want the application to use.

哇,这不是真的!您可以指定 PowerShell 使用的 .NET 版本。关键是 .NET 标准应用程序配置文件,其格式为 [appname].exe.config。您可以将它放在与大多数 .NET 应用程序(包括 PowerShell 和PowerShell ISE可执行文件)相同的目录中,CLR 将自动加载配置文件中指定的任何可识别选项。这些选项之一是您希望应用程序使用的 CLR 版本。

This is documented in detail in the question: How can I run PowerShell with the .NET 4 runtime?. In particular, see Emperor XLII's post.

这在问题中有详细记录:How can I run PowerShell with the .NET 4 runtime? . 特别是见皇帝 XLII的帖子。

回答by driis

The .NET version can be inferred from the version of mscorlib. So you can do the following in PowerShell to output the current version of .NET:

.NET 版本可以从 mscorlib 的版本中推断出来。因此,您可以在 PowerShell 中执行以下操作来输出 .NET 的当前版本:

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

回答by spinon

Check out the article Hey, Scripting Guy! How Do I Check Which Version of Windows PowerShell I'm Using?. It shows where in the registry you can check to determine this.

查看文章嘿,脚本专家!如何检查我使用的是哪个版本的 Windows PowerShell?. 它显示了您可以在注册表中的哪个位置进行检查以确定这一点。

回答by Shay Levy

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

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

回答by Baltasarq

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