Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用于在计算机上返回.NET Framework版本的PowerShell脚本?_.net_Powershell_Version - Fatal编程技术网

用于在计算机上返回.NET Framework版本的PowerShell脚本?

用于在计算机上返回.NET Framework版本的PowerShell脚本?,.net,powershell,version,.net,Powershell,Version,要在计算机上返回.NET Framework版本,PowerShell脚本是什么 我的第一个猜测涉及WMI。有更好的吗 每次安装.NET[每行]时只返回最新版本应该是一行代码。我不太懂PowerShell语法,但我想你可以打电话给我。这将以字符串形式返回版本(我认为类似于v2.0.50727) 提供当前PSH副本正在使用的CLR的实例(如文档所示)。不太好。绝对不漂亮: ls $Env:windir\Microsoft.NET\Framework | ? { $_.PSIsContainer }

要在计算机上返回.NET Framework版本,PowerShell脚本是什么

我的第一个猜测涉及WMI。有更好的吗


每次安装.NET[每行]时只返回最新版本应该是一行代码。

我不太懂PowerShell语法,但我想你可以打电话给我。这将以字符串形式返回版本(我认为类似于
v2.0.50727

提供当前PSH副本正在使用的CLR的实例(如文档所示)。

不太好。绝对不漂亮:

ls $Env:windir\Microsoft.NET\Framework | ? { $_.PSIsContainer } | select -exp Name -l 1
这可能有效,也可能无效。但就最新版本而言,这应该是相当可靠的,因为旧版本(1.0、1.1)基本上都有空文件夹,而新版本则没有——这些文件夹只有在安装了相应的框架后才会出现


不过,我怀疑一定有更好的方法。

对于所有平台和体系结构,使用简单的脚本来实现这一点是不可靠的。如果你想学习如何可靠地做这件事,从博客文章开始


如果安装了4.5,则此答案不会返回4.5。下面是@Jaykul和使用recurse给出的答案。

如果要使用注册表,则必须使用recurse才能获得4.x框架的完整版本。前面的答案都返回了.NET3.0系统上的根编号(其中嵌套在3.0下的WCF和WPF编号更高——我无法解释这一点),也没有返回任何4.0版本的根编号

编辑:对于.Net 4.5及更高版本,这一点再次发生了轻微的变化,因此现在有一个很好的解释如何将发布值转换为.Net版本号,这是一个完全的火车失事:-(

在我看来,这是正确的(请注意,它在3.0上为WCF和WPF输出单独的版本号。我不知道这是怎么回事)。它在4.0上还输出客户端和完整版本(如果您都安装了它们):

根据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 }}
"@
正确语法:

[System.Runtime.InteropServices.RuntimeEnvironment]::GetSystemVersion()
#or
$PSVersionTable.CLRVersion
函数返回如下字符串:

v2.0.50727        #PowerShell v2.0 in Win 7 SP1
Major  Minor  Build  Revision
-----  -----  -----  --------
4      0      30319  18444   
PS> Get-DotNetVersion -LocalHost -nosummary


ComputerName : localhost
>=4.x        : 4.5.2
v4\Client    : Installed
v4\Full      : Installed
v3.5         : Installed
v3.0         : Installed
v2.0.50727   : Installed
v1.1.4322    : Not installed (no key)
Ping         : True
Error        :
还是像这样

v4.0.30319        #PowerShell v3.0 (Windows Management Framework 3.0) in Win 7 SP1
是只读对象。CLRVersion属性是一个结构化版本号,如下所示:

v2.0.50727        #PowerShell v2.0 in Win 7 SP1
Major  Minor  Build  Revision
-----  -----  -----  --------
4      0      30319  18444   
PS> Get-DotNetVersion -LocalHost -nosummary


ComputerName : localhost
>=4.x        : 4.5.2
v4\Client    : Installed
v4\Full      : Installed
v3.5         : Installed
v3.0         : Installed
v2.0.50727   : Installed
v1.1.4322    : Not installed (no key)
Ping         : True
Error        :

以下是总体思路:

获取.NET Framework目录中名称匹配的容器的子项 图案v编号点编号。按名称降序排列,取第一个对象, 并返回其name属性

以下是脚本:

(Get-ChildItem -Path $Env:windir\Microsoft.NET\Framework | Where-Object {$_.PSIsContainer -eq $true } | Where-Object {$_.Name -match 'v\d\.\d'} | Sort-Object -Property Name -Descending | Select-Object -First 1).Name
Get-ChildItem 'HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP' -recurse |
Get-ItemProperty -name Version,Release -EA 0 |
Where { $_.PSChildName -match '^(?![SW])\p{L}'} |
Select PSChildName, Version, Release, @{
  name="Product"
  expression={
      switch -regex ($_.Release) {
        "378389" { [Version]"4.5" }
        "378675|378758" { [Version]"4.5.1" }
        "379893" { [Version]"4.5.2" }
        "393295|393297" { [Version]"4.6" }
        "394254|394271" { [Version]"4.6.1" }
        "394802|394806" { [Version]"4.6.2" }
        "460798|460805" { [Version]"4.7" }
        "461308|461310" { [Version]"4.7.1" }
        "461808|461814" { [Version]"4.7.2" }
        "528040|528049" { [Version]"4.8" }
        {$_ -gt 528049} { [Version]"Undocumented version (> 4.8), please update script" }
      }
    }
}
请参阅本页


那里的脚本对于在网络上查找多台计算机的.NET版本可能很有用。

我是通过powershell for osx中的制表符补全找到的:

[System.Runtime.InteropServices.RuntimeInformation]::get_FrameworkDescription()

.NET Core 4.6.25009.03

为脚本添加了v4.8支持:

(Get-ChildItem -Path $Env:windir\Microsoft.NET\Framework | Where-Object {$_.PSIsContainer -eq $true } | Where-Object {$_.Name -match 'v\d\.\d'} | Sort-Object -Property Name -Descending | Select-Object -First 1).Name
Get-ChildItem 'HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP' -recurse |
Get-ItemProperty -name Version,Release -EA 0 |
Where { $_.PSChildName -match '^(?![SW])\p{L}'} |
Select PSChildName, Version, Release, @{
  name="Product"
  expression={
      switch -regex ($_.Release) {
        "378389" { [Version]"4.5" }
        "378675|378758" { [Version]"4.5.1" }
        "379893" { [Version]"4.5.2" }
        "393295|393297" { [Version]"4.6" }
        "394254|394271" { [Version]"4.6.1" }
        "394802|394806" { [Version]"4.6.2" }
        "460798|460805" { [Version]"4.7" }
        "461308|461310" { [Version]"4.7.1" }
        "461808|461814" { [Version]"4.7.2" }
        "528040|528049" { [Version]"4.8" }
        {$_ -gt 528049} { [Version]"Undocumented version (> 4.8), please update script" }
      }
    }
}

我会在PowerShell中尝试这个: 为我工作

(获取ItemProperty“HKLM:Software\Microsoft\NET Framework Setup\NDP\v4\Full”)。版本

Nice解决方案 尝试使用可下载的(基于注册表信息和某个版本到市场营销版本的查找表)

使用的方法如下:

v2.0.50727        #PowerShell v2.0 in Win 7 SP1
Major  Minor  Build  Revision
-----  -----  -----  --------
4      0      30319  18444   
PS> Get-DotNetVersion -LocalHost -nosummary


ComputerName : localhost
>=4.x        : 4.5.2
v4\Client    : Installed
v4\Full      : Installed
v3.5         : Installed
v3.0         : Installed
v2.0.50727   : Installed
v1.1.4322    : Not installed (no key)
Ping         : True
Error        :
或者,如果您只想测试某个.NET framework>=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) 因此,以下代码和以下(提取的)用法示例在这里很有用(基于此处的其他答案):


如果已在计算机上安装Visual Studio,请打开Visual Studio Developer命令提示符并键入以下命令: 克莱弗


它将列出该计算机上安装的所有.NET Framework版本。

以下是我对这个问题的看法:


此示例适用于所有PowerShell版本,并将永久运行,因为4.8是最后一个.NET Framework版本。

这是对上一篇文章的衍生,但在我的测试中,这是最新版本的.NET Framework 4

get-itemproperty -name version,release "hklm:\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\FULL"
这将允许您调用远程计算机的命令:

invoke-command -computername server01 -scriptblock {get-itemproperty -name version,release "hklm:\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\FULL" | select pscomputername,version,release} 
它使用ADModule和命名约定前缀设置此可能性:

get-adcomputer -Filter 'name -like "*prefix*"' | % {invoke-command -computername $_.name -scriptblock {get-itemproperty -name version,release "hklm:\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\FULL" | select pscomputername,version,release} | ft

这纯粹是因为我必须花时间制作/编辑本应广泛使用的内容,所以我将其提供给其他所有人

下面的脚本将向TEMP输出两个CSV文件,其中包含所选(代码中)OU中每台计算机的版本和漏洞状态。您将能够远程“安全审计”机器的OU

连接测试线需要Powershell 7.0 RSAT需要获得AD模块 获取powershell 7.0(在win7上)所需的Visual Studio代码

当您阅读本文时,文件中的版本列表可能已过期。请使用此网站添加较新的dotnet条目。它只是“DotNet4Builds”中的一组键值

如果在ConvertizedCheck.csv中,一台机器显示为=0,则它是