用于返回计算机上 .NET Framework 版本的 PowerShell 脚本?
返回计算机上 .NET Framework 版本的 PowerShell 脚本是什么?
我的第一个猜测是涉及 WMI 的东西。还有更好的吗?
它应该是一行,为每次 .NET 安装[在每一行]仅返回最新版本。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(18)
如果您要使用注册表,则必须递归才能获取 4.x 框架的完整版本。早期的答案都返回我系统上 .NET 3.0 的根编号(其中嵌套在 3.0 下的 WCF 和 WPF 编号更高 - 我无法解释这一点),并且无法返回 4.0 的任何内容。 编辑:
对于 .Net 4.5 及更高版本,这再次略有变化,因此现在有一个很好的 MSDN这里的文章解释了如何将 Release 值转换为 .Net 版本号,这简直就是一场灾难:-(
这对我来说看起来很正确(请注意,它为 WCF 输出单独的版本号& 3.0 上的 WPF 我不知道那是什么)。它还在 4.0 上输出 Client 和 Full(如果您安装了它们)
: MSDN 文章中,您可以构建一个查找表并返回 4.5 之后版本的营销产品版本号:
事实上,由于我必须不断更新此答案,因此这里有一个脚本用于从该网页的 Markdown 源。这可能会在某个时候中断,所以我保留上面的当前副本。
If you're going to use the registry you have to recurse in order to get the full version for the 4.x Framework. The earlier answers both return the root number on my system for .NET 3.0 (where the WCF and WPF numbers, which are nested under 3.0, are higher -- I can't explain that), and fail to return anything for 4.0 ...
EDIT: For .Net 4.5 and up, this changed slightly again, so there's now a nice MSDN article here explaining how to convert the Release value to a .Net version number, it's a total train wreck :-(
This looks right to me (note that it outputs separate version numbers for WCF & WPF on 3.0. I don't know what that's about). It also outputs both Client and Full on 4.0 (if you have them both installed):
Based on the MSDN article, you could build a lookup table and return the marketing product version number for releases after 4.5:
In fact, since I keep having to update this answer, here's a script to generate the script above (with a little extra) from the markdown source for that web page. This will probably break at some point, so I'm keeping the current copy above.
为脚本添加了 v4.8 支持:
Added v4.8 support to the script:
为您提供
Version
的实例对于当前 PSH 副本正在使用的 CLR(如此处记录< /a>)。Gives you an instance of
Version
for the CLR the current copy of PSH is using (as documented here).如果已安装,此答案不会返回 4.5。下面来自 @Jaykul 的答案和使用递归确实如此。
This answer doesn't return 4.5 if that is installed. The answer below from @Jaykul and using recurse does.
正确的语法:
GetSystemVersion
函数返回一个像这样的字符串:或者像这样
$PSVersionTable
< /a> 是只读对象。 CLRVsion 属性是一个结构化版本号,如下所示:Correct syntax:
The
GetSystemVersion
function returns a string like this:or like this
$PSVersionTable
is a read-only object. The CLRVersion property is a structured version number like this:我通过 osx 的 powershell 中的选项卡补全找到了这一点:
[System.Runtime.InteropServices.RuntimeInformation]::get_FrameworkDescription()
.NET Core 4.6.25009.03
I found this through tab completion in powershell for osx:
[System.Runtime.InteropServices.RuntimeInformation]::get_FrameworkDescription()
.NET Core 4.6.25009.03
不错的解决方案
尝试使用可下载的 DotNetVersionLister 模块 (基于注册表信息和一些版本到营销版本的查找表)。
可以像这样使用:
或者像这样,如果您只想测试某些 .NET 框架 >= 4.*:
但它不会工作(安装/导入),例如 >PS v2.0(Win 7、Win Server 2010 标准)由于不兼容...
下面的“遗留”功能的动机
(您可以跳过阅读并使用下面的代码)
我们必须在某些计算机上使用PS 2.0,并且无法安装/导入上述DotNetVersionLister。
在其他计算机上,我们想要更新(从PS 2.0)到PS 5.1(这又需要.NET Framework >= 4.5)两个公司自定义
Install-DotnetLatestCompany
和Install-PSLatestCompany
的帮助。为了很好地指导管理员完成安装/更新过程,我们必须确定所有计算机上这些功能的 .NET 版本以及现有的 PS 版本。
因此,我们还使用以下函数在所有环境中更安全地确定它们...
旧 PS 环境的函数(例如 PS v2.0)
因此以下代码和下面(提取的)使用示例非常有用此处(基于此处的其他答案):
示例用法:
Nice solution
Try using the downloadable DotNetVersionLister module (based on registry infos and some version-to-marketing-version lookup table).
Which would be used like this:
Or like this if you just want to test it for some .NET framework >= 4.*:
But it will not work (install/import) e.g. with PS v2.0 (Win 7, Win Server 2010 standard) due to incompatibility...
Motivation for "legacy" functions below
(You could skip reading this and use code below)
We had to work with PS 2.0 on some machines and could not install/import the above DotNetVersionLister.
On other machines we wanted to update (from PS 2.0) to PS 5.1 (which in turn needs .NET Framework >= 4.5) with the help of two company-custom
Install-DotnetLatestCompany
andInstall-PSLatestCompany
.To guide admins nicely through the install/update process we would have to determine the .NET version in these functions on all machines and PS versions existing.
Thus we used also the below functions to determine them more safely in all environments...
Functions for legacy PS environments (e.g. PS v2.0)
So the following code and below (extracted) usage examples are useful here (based on other answers here):
Example usage:
没有可靠的方法可以使用简单的脚本对所有平台和架构执行此操作。如果您想了解如何可靠地完成此操作,请从博客文章更新了示例 .NET Framework 检测代码,可进行更深入的检查。
There's no reliable way to do this for all platforms and architectures using a simple script. If you want to learn how to do it reliably, start at the blog post Updated sample .NET Framework detection code that does more in-depth checking.
请参阅页面用于查找远程工作站上安装的 .NET 版本的脚本。
那里的脚本可能有助于查找网络上多台计算机的 .NET 版本。
Refer to the page Script for finding which .NET versions are installed on remote workstations.
The script there might be useful to find the .NET version for multiple machines on a network.
以下是我对这个问题的看法 msft 文档:
此示例适用于所有 PowerShell 版本,并且将永久有效,因为 4.8 是最后一个 .NET Framework 版本。
Here's my take on this question following the msft documentation:
This example works with all PowerShell versions and will work in perpetuity as 4.8 is the last .NET Framework version.
这纯粹是因为当它应该广泛可用时我不得不花时间制作/编辑它,所以我将它提供给其他人。
以下脚本将向 TEMP 输出几个 CSV 文件,其中包含所选(代码中)OU 中每台计算机的版本和漏洞状态。您将能够远程“安全审核”计算机的 OU。
连接测试线需要Powershell 7.0
获取AD模块需要RSAT
Visual Studio Code 需要获取 powershell 7.0(在 win7 上)
当您阅读本文时,文件中的版本列表可能已经过时。使用此网站 https://learn.microsoft .com/en-us/dotnet/framework/migration-guide/versions-and-dependencies 添加较新的 dotnet 条目。它只是“DotNet4Builds”中的一堆关键值。
如果在 CompromishedCheck.csv 中,一台机器显示为 =0,则表明它的安全性已手动关闭,您应该提出是否是供应商干的,或者是可疑员工干的。
我希望这可以帮助那些正在寻找其业务的人。
This is purely because I had to spend time making/editing this when it should be widely available, so I'm providing it to everyone else.
The below script will Output a couple of CSV files to TEMP with the versions and vulnerability status of each machine in a selected (in the code) OU. You'll be able to remotely "security audit" an OU of machines.
Powershell 7.0 needed for the connection test line
RSAT needed to get the AD module
Visual Studio Code needed to get powershell 7.0 (on win7)
By the time you read this, the version list will probably be out of date within the file. Use this website https://learn.microsoft.com/en-us/dotnet/framework/migration-guide/versions-and-dependencies to add newer dotnet entries. It's just a bunch of key values in "DotNet4Builds"
If within CompromisedCheck.csv a machine shows as =0, it's had it's security turned off manually, and you should raise whether the supplier did it, or a suspect employee.
I hope this helps someone searching for it for their business.
不漂亮。 绝对不太好:
这可能有效,也可能无效。但就最新版本而言,这应该是相当可靠的,因为旧版本(1.0、1.1)基本上是空文件夹,但新版本没有——这些文件夹只有在安装了适当的框架后才会出现。
不过,我怀疑一定有更好的方法。
Not pretty. Definitely not pretty:
This may or may not work. But as far as the latest version is concerned this should be pretty reliable, as there are essentially empty folders for old versions (1.0, 1.1) but not newer ones – those only appear once the appropriate framework is installed.
Still, I suspect there must be a better way.
目前,从 v8x 开始,
dotnet.exe --info
返回当前 shell 的版本。Well currently as of v8x
dotnet.exe --info
returns a version for the current shell.我会在 PowerShell 中尝试这个:
为我工作!
(Get-ItemProperty "HKLM:Software\Microsoft\NET Framework Setup\NDP\v4\Full").Version
I would try this one in PowerShell:
Worked for me!
(Get-ItemProperty "HKLM:Software\Microsoft\NET Framework Setup\NDP\v4\Full").Version
总体思路如下:
获取 .NET Framework 目录中的子项,这些子项是名称匹配的容器
图案v号点号。按名称降序对它们进行排序,取第一个对象,
并返回其 name 属性。
这是脚本:
Here's the general idea:
Get child items in the .NET Framework directory that are containers whose names match
the pattern v number dot number. Sort them by descending name, take the first object,
and return its name property.
Here's the script:
如果您的计算机上已安装 Visual Studio,则打开 Visual Studio 开发人员命令提示符并键入以下命令:
clrver
它将列出该计算机上所有已安装的 .NET Framework 版本。
If you have installed Visual Studio on your machine then open the Visual Studio Developer Command Prompt and type the following command:
clrver
It will list all the installed versions of .NET Framework on that machine.
这是上一篇文章的衍生版本,但在我的测试中它获取了最新版本的 .net Framework 4。
这将允许您调用命令到远程计算机:
这使用 ADModule 和命名约定前缀设置了这种可能性:
This is a derivite of previous post, but this gets the latest version of the .net framework 4 in my tests.
Which will allow you to invoke-command to remote machine:
Which sets up this possibility with ADModule and naming convention prefix:
我不熟悉 PowerShell 语法,但我认为您可以调用 System.Runtime.InteropServices.RuntimeEnvironment.GetSystemVersion()。这将以字符串形式返回版本(我认为类似于
v2.0.50727
)。I'm not up on my PowerShell syntax, but I think you could just call System.Runtime.InteropServices.RuntimeEnvironment.GetSystemVersion(). This will return the version as a string (something like
v2.0.50727
, I think).