PowerShell 脚本在机器上返回 .NET Framework 的版本是什么?
我的第一个猜测是涉及 WMI。有更好的吗?
对于每个安装的 .NET [在每一行上],只返回最新版本应该是单行的。
如果要使用注册表,则必须递归才能获得 4.x 框架的完整版本。较早的答案都返回我系统上 .NET 3.0 的根编号(其中嵌套在 3.0 下的 WCF 和 WPF 编号更高——我无法解释),并且无法返回 4.0 的任何内容。 .
编辑:对于 .Net 4.5 及更高版本,这又略有变化,所以现在有一篇很好的MSDN 文章在这里解释了如何将 Release 值转换为 .Net 版本号,这完全是火车残骸:-(
这对我来说是正确的(请注意,它在 3.0 上为 WCF 和 WPF 输出单独的版本号。我不知道那是什么)。它还在 4.0 上同时输出 Client 和 Full (如果您同时安装了它们):
Get-ChildItem 'HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP' -recurse | Get-ItemProperty -name Version,Release -EA 0 | Where { $_.PSChildName -match '^(?!S)\p{L}'} | Select PSChildName, Version, Release
根据 MSDN 文章,您可以构建一个查找表并返回 4.5 之后版本的营销产品版本号:
$Lookup = @{ 378389 = [version]'4.5' 378675 = [version]'4.5.1' 378758 = [version]'4.5.1' 379893 = [version]'4.5.2' 393295 = [version]'4.6' 393297 = [version]'4.6' 394254 = [version]'4.6.1' 394271 = [version]'4.6.1' 394802 = [version]'4.6.2' 394806 = [version]'4.6.2' 460798 = [version]'4.7' 460805 = [version]'4.7' 461308 = [version]'4.7.1' 461310 = [version]'4.7.1' 461808 = [version]'4.7.2' 461814 = [version]'4.7.2' 528040 = [version]'4.8' 528049 = [version]'4.8' } # For One True framework (latest .NET 4x), change the Where-Object match # to PSChildName -eq "Full": Get-ChildItem 'HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP' -Recurse | Get-ItemProperty -name Version, Release -EA 0 | Where-Object { $_.PSChildName -match '^(?!S)\p{L}'} | Select-Object @{name = ".NET Framework"; expression = {$_.PSChildName}}, @{name = "Product"; expression = {$Lookup[$_.Release]}}, Version, Release
事实上,由于我必须不断更新这个答案,这里有一个脚本,用于从该网页的降价源生成上面的脚本(有一点额外的)。这可能会在某个时候中断,所以我将当前副本保留在上面。
# Get the text from github $url = "https://raw.githubusercontent.com/dotnet/docs/master/docs/framework/migration-guide/how-to-determine-which-versions-are-installed.md" $md = Invoke-WebRequest $url -UseBasicParsing $OFS = "`n" # Replace the weird text in the tables, and the padding # Then trim the | off the front and end of lines $map = $md -split "`n" -replace " installed [^|]+" -replace "\s+\|" -replace "\|$" | # Then we can build the table by looking for unique lines that start with ".NET Framework" Select-String "^.NET" | Select-Object -Unique | # And flip it so it's key = value # And convert ".NET FRAMEWORK 4.5.2" to [version]4.5.2 ForEach-Object { [version]$v, [int]$k = $_ -replace "\.NET Framework " -split "\|" " $k = [version]'$v'" } # And output the whole script @" `$Lookup = @{ $map } # For extra effect we could get the Windows 10 OS version and build release id: try { `$WinRelease, `$WinVer = Get-ItemPropertyValue "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion" ReleaseId, CurrentMajorVersionNumber, CurrentMinorVersionNumber, CurrentBuildNumber, UBR `$WindowsVersion = "`$(`$WinVer -join '.') (`$WinRelease)" } catch { `$WindowsVersion = [System.Environment]::OSVersion.Version } Get-ChildItem 'HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP' -Recurse | Get-ItemProperty -name Version, Release -EA 0 | # For The One True framework (latest .NET 4x), change match to PSChildName -eq "Full": Where-Object { `$_.PSChildName -match '^(?!S)\p{L}'} | Select-Object @{name = ".NET Framework"; expression = {`$_.PSChildName}}, @{name = "Product"; expression = {`$Lookup[`$_.Release]}}, Version, Release, # Some OPTIONAL extra output: PSComputerName and WindowsVersion # The Computer name, so output from local machines will match remote machines: @{ name = "PSComputerName"; expression = {`$Env:Computername}}, # The Windows Version (works on Windows 10, at least): @{ name = "WindowsVersion"; expression = { `$WindowsVersion }} "@