lan*_*nce 41 .net powershell version
我想在我即将编写的一些PowerShell脚本中使用.NET - 当这些脚本运行时,我如何知道/声明我正在处理哪个版本的.NET?
是否可以选择运行哪个版本的.NET脚本?
Kei*_*ill 39
在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
Run Code Online (Sandbox Code Playgroud)
在PowerShell 1.0上,使用[System.Environment]::Version:
PS> [Environment]::Version
Major Minor Build Revision
----- ----- ----- --------
2 0 50727 4927
Run Code Online (Sandbox Code Playgroud)
Geo*_*rth 16
要获得.NET版本:
[System.Reflection.Assembly]::GetExecutingAssembly().ImageRuntimeVersion
Run Code Online (Sandbox Code Playgroud)
...默认情况下,System.Management.Automation.dll编译下的assembly()的CLR版本.
不,你不能选择你可以运行脚本的.NET版本.
Jos*_*nig 16
...不,您无法选择可以运行脚本的.NET版本 - George Howarth
哇,那不是真的!您可以指定PowerShell使用的.NET版本.关键是.NET标准应用程序配置文件,其格式为[appname] .exe.config.您可以将其放在与大多数.NET应用程序相同的目录中 - 包括PowerShell和PowerShell ISE可执行文件 - 并且CLR将自动加载配置文件中指定的任何可识别选项.其中一个选项是您希望应用程序使用的CLR版本.
这在问题中有详细记录:如何使用.NET 4运行时运行PowerShell?.特别是,看看Emperor XLII的帖子.
可以从mscorlib的版本推断出.NET版本.因此,您可以在PowerShell中执行以下操作以输出当前版本的.NET:
$a = [System.Reflection.Assembly]::Load("mscorlib")
$a.GetName().Version
Run Code Online (Sandbox Code Playgroud)
这是一个旧线程,我现在要发布的答案不适用于 2017 年左右之前的 .NET 版本。
有一个新的FrameworkDescription财产。
尝试:
[System.Runtime.InteropServices.RuntimeInformation]::FrameworkDescription
Run Code Online (Sandbox Code Playgroud)
但请注意,在某些版本的Windows PowerShell 上(不确定较新的 PowerShell 6 和 7 等),不同的System.Runtime.InteropServices.RuntimeInformation程序集中有两种不同的类型!而且只有其中一人拥有该财产。因此,您必须符合以下条件:
[System.Runtime.InteropServices.RuntimeInformation, Microsoft.Powershell.PSReadline]::FrameworkDescription # does not exist
[System.Runtime.InteropServices.RuntimeInformation, mscorlib]::FrameworkDescription # good
Run Code Online (Sandbox Code Playgroud)