Get-InstalledNetVersion

on Monday, March 4, 2019

There are number of resources online which show how to get the install .NET version from a server.

This is just a combination of couple of them into easy to use functions that can run on remote servers.

<#
.SYNOPSIS
Checks the .NET versions installed on a computer/server.
.PARAMETER ServerName
The server to check.
.EXAMPLE
Get-InstalledNetVersion
#>
Function Get-InstalledNetVersion {
Param (
[Parameter(ValueFromPipelineByPropertyName = $true)]
[string] $ServerName = $env:COMPUTERNAME
)
Process {
$scriptBlock = {
$ServerName = $args[0]
# .net v1, v2, and v3
$versions = New-Object System.Collections.Generic.List[PSObject]
$35minus = dir "HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP\"
$root = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\"
$rootLen = $root.Length
$35minus = $35minus |? {
$_.Name.Substring($rootLen).StartsWith("v2") `
-or $_.Name.Substring($rootLen).StartsWith("v3")
}
foreach($net in $35minus) {
$name = $net.Name.Substring($rootLen)
$version = (Get-ItemProperty $net.PSPath Version).Version
$release = $version.Split(".")[3]
$props = @{
Name = $name
Version = $version
Release = $release
Description = $name
}
$obj = New-PsType -PsType "NetVersion" -Property $props
$versions.Add($obj)
}
# .net v4
$4 = dir "HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full"
$root = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full"
$rootLen = $root.Length
$description = ""
# https://msdn.microsoft.com/en-us/library/hh925568(v=vs.110).aspx?f=255&MSPPError=-2147217396
$release = (Get-ItemProperty $4.PSPath Release).Release
switch($release) {
"378389" { $description = "v4.5" }
"378675" { $description = "v4.5.1 - Win8.1 or Win2012R2" }
"378758" { $description = "v4.5.1 - Win7 SP1 to Win8" }
"379893" { $description = "v4.5.2" }
"393273" { $description = "v4.6 RC" }
"393295" { $description = "v4.6 - Win10" }
"393297" { $description = "v4.6" }
"394254" { $description = "v4.6.1 - Win10" }
"394271" { $description = "v4.6.1" }
"394802" { $description = "v4.6.2 - Win10 An. Update" }
"394806" { $description = "v4.6.2" }
"460798" { $description = "v4.7 - Win10 Creators Update" }
"460805" { $description = "v4.7" }
"461308" { $description = "v4.7.1 - Win10 Fall Creators Update" }
"461310" { $description = "v4.7.1" }
"461808" { $description = "v4.7.2 - Win10 April 2018 Update" }
"461814" { $description = "v4.7.2" }
default { $description = "unknown ... check https://msdn.microsoft.com/en-us/library/hh925568(v=vs.110).aspx?f=255&MSPPError=-2147217396" }
}
$props = @{
Name = "v4"
Version = (Get-ItemProperty $4.PSPath Version).Version
Release = $release
Description = $description
}
$obj = New-PsType -PsType "NetVersion" -Property $props
$versions.Add($obj)
# .net core
$cores = Get-InstalledNetCoreVersion -ServerName $ServerName
foreach($c in $cores) {
$versions.Add($c);
}
return $versions
}
$versions = Invoke-Command -ComputerName $ServerName -ScriptBlock $scriptBlock -ArgumentList $ServerName
return $versions
}
}
<#
.SYNOPSIS
Checks the .NET versions installed on a computer/server.
.PARAMETER ServerName
The server to check.
.EXAMPLE
Get-InstalledNetVersion
#>
function Get-InstalledNetCoreVersion {
Param (
[Parameter(ValueFromPipelineByPropertyName = $true)]
[string] $ServerName = $env:COMPUTERNAME
)
Process {
$scriptBlock = {
$ServerName = $args[0]
# https://stackoverflow.com/questions/38567353/how-to-determine-if-net-core-is-installed
$search = (dir (Get-Command dotnet).Path.Replace('dotnet.exe', 'shared\Microsoft.NETCore.App')).Name
$versions = @()
foreach($version in $search) {
$props = @{
Name = "core-$version"
Version = $version
Release = ""
Description = "core $version"
}
$obj = New-PsType -PsType "NetVersion" -Property $props
$versions += @($obj)
}
return $versions
}
$versions = Invoke-Command -ComputerName $ServerName -ScriptBlock $scriptBlock -ArgumentList $ServerName
return $versions
}
}
Function New-PsType {
[CmdletBinding()]
Param (
[Parameter(Mandatory = $true)]
[string] $PsType,
[System.Collections.IDictionary] $Property
)
# http://stackoverflow.com/questions/59819/how-do-i-create-a-custom-type-in-powershell-for-my-scripts-to-use
if($Property) {
$m = New-Object PSObject -Property $Property
} else {
$m = New-Object PSObject
}
$null = $m.PSObject.TypeNames.Insert(0,$PsType)
return $m;
}

0 comments:

Post a Comment


Creative Commons License
This site uses Alex Gorbatchev's SyntaxHighlighter, and hosted by herdingcode.com's Jon Galloway.