Get-DotNetv4FrameworkVersion.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<#
.SYNOPSIS
Gets the installed .NET Framework v4 version from the computer's registry.
 
.DESCRIPTION
Gets the installed .NET Framework v4 version from the computer's registry.
 
.NOTES
Created by: Jason Wasser @wasserja
Modified: 6/21/2017 01:53:27 PM
 
Changelog:
* Added support for .NET Framework 4.7
 
.PARAMETER ComputerName
Enter a computer name to check the .Net Framework 4 version installed.
 
.EXAMPLE
Get-DotNetFrameworkVersions -ComputerName server1
Retreives the .NET framework 4 version information from computer Server1.
 
.LINK
 
#>

function Get-DotNetv4FrameworkVersion {
    [CmdletBinding()]
    Param
    (
        [Parameter(Mandatory = $false,
            ValueFromPipeline = $true,
            ValueFromPipelineByPropertyName = $true,
            Position = 0)]
        [string[]]$ComputerName = $env:COMPUTERNAME
    )

    Begin {

    
        # Helper function to do the work of gathering the correct values from the registry.
        function Get-DotNetVersionfromRegistry {
            $DotNetv4FullKey = 'HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full'
            $DotNetv45VersionTable = @{
                '4.0.30319' = '.NET Framework 4'
                '378389'    = '.NET Framework 4.5'
                '378675'    = '.NET Framework 4.5.1'
                '378758'    = '.NET Framework 4.5.1'
                '379893'    = '.NET Framework 4.5.2'
                '393295'    = '.NET Framework 4.6'
                '393297'    = '.NET Framework 4.6'
                '394254'    = '.NET Framework 4.6.1'
                '394271'    = '.NET Framework 4.6.1'
                '394802'    = '.NET Framework 4.6.2'
                '394806'    = '.NET Framework 4.6.2'
                '460805'    = '.NET Framework 4.7'
                '460798'    = '.NET Framework 4.7'
                '461308'    = '.NET Framework 4.7.1'
                '461310'    = '.NET Framework 4.7.1'
                '461814'    = '.NET Framework 4.7.2'
                '461808'    = '.NET Framework 4.7.2'
            }    
        
            if (Test-Path $DotNetv4FullKey) {
                Write-Verbose '.Net Framework 4 Full registry Key Exists'
            
                # For versions of .NET Framework >= 4.5
                $DotNetv4FullReleaseNumber = (Get-Item $DotNetv4FullKey).GetValue('Release')
            
                if ($DotNetv4FullReleaseNumber) {
                    $DotNetv4FullReleaseNumber = $DotNetv4FullReleaseNumber.ToString()
                }
                else {
                    # For .NET framework version < 4.5
                    $DotNetv4FullReleaseNumber = (Get-Item $DotNetv4FullKey).GetValue('Version')
                }
            
            
                if ($DotNetv45VersionTable.($DotNetv4FullReleaseNumber).tostring()) {
                    Write-Verbose 'Found a valid .net 4 version'
                    $DotNetv4FullVersionName = $DotNetv45VersionTable.$DotNetv4FullReleaseNumber
                }
                else {
                    Write-Verbose 'Did not find a matching .net version release number.'
                    $DotNetv4FullVersionName = 'Unknown'
                }
                $DotNetv4VersionProperties = @{
                    ComputerName              = $env:COMPUTERNAME
                    DotNetv4FullVersionName   = $DotNetv4FullVersionName
                    DotNetv4FullReleaseNumber = $DotNetv4FullReleaseNumber
                }
                $DotNetv4FullVersion = New-Object -TypeName PSCustomObject -Property $DotNetv4VersionProperties
                $DotNetv4FullVersion
            }

            else {
                Write-Verbose '.Net Framework 4 Full registry key does not exist.'

                $DotNetv4VersionProperties = @{
                    ComputerName              = $env:COMPUTERNAME
                    DotNetv4FullVersionName   = 'Not Installed'
                    DotNetv4FullReleaseNumber = 'Not Installed'
                }
                $DotNetv4FullVersion = New-Object -TypeName PSCustomObject -Property $DotNetv4VersionProperties
                $DotNetv4FullVersion
            }
        }
    

    }
    Process {

        foreach ($Computer in $ComputerName) {
            Write-Verbose -Message "Checking .NET Framework 4 version on local computer $Computer"
            if ($Computer -eq $env:COMPUTERNAME) {
                Get-DotNetVersionfromRegistry
            }
            else {
                Write-Verbose -Message "Checking .NET Framework 4 version on remote computer $Computer"
                Invoke-Command -ComputerName $Computer -ScriptBlock ${function:Get-DotNetVersionfromRegistry}
            }
        }
    }
    End {
    }
}