Functions/TestNetInstalled.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
#ther should be some comments here!
Function Test-NetInstalled {
    
    param(
        [Parameter(Position = 1, mandatory = $false)]
        [String] $DotNetVersion
    )
    [Int] $RegEditDotNet | Out-Null
    [bool] $RequiredVersion = $true
    $OSVersion = (get-itemproperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion" -Name ProductName).ProductName

    
    $dWord = Get-ChildItem "hklm:SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full\" | Get-ItemPropertyValue -Name Release 
    if ($DotNetVersion) {
        switch ($DotNetVersion) {
            "4.5" { $RegEditDotNet = 378389} 
            "4.5.1" { $RegEditDotNet = 378675}
            "4.5.2" { $RegEditDotNet = 379893}
            "4.6" { $RegEditDotNet = 393295}
            "4.6.1" { $RegEditDotNet = 394254}
            "4.6.2" { $RegEditDotNet = 394802}
            "4.7" { $RegEditDotNet = 460798}
            "4.7.1" {
                if ($OSVersion -like "%2016%") {
                    $RegEditDotNet = 461310
                } 
                else {
                    $RegEditDotNet = 461308
                }
            }
            default {$RegEditDotNet = 0}
        }
        if ($dWord -lt $RegEditDotNet -or $RegEditDotNet -eq 0 ) {
            Write-Error "You must have .NET $DotNetVersion installed on this machine to continue!"
            $RequiredVersion = $false
        }
        else {
            Write-Host "At least $DotNetVersion is installed!" -ForegroundColor White -BackgroundColor Red
        }
    }

    switch ($dWord) {
        378389 { $DotNetVersion = "4.5"  }
        378675 { $DotNetVersion = "4.5.1"}
        379893 { $DotNetVersion = "4.5.2" }
        393295 { $DotNetVersion = "4.6"   }
        394254 { $DotNetVersion = "4.6.1" }
        394802 { $DotNetVersion = "4.6.2" }
        460798 { $DotNetVersion = "4.7"   }
        461308 { $DotNetVersion = "4.7.1" }
        461310 { $DotNetVersion = "4.7.1" }
    }
    $DotNetInfo = @{ DotNetVersion = $DotNetVersion; DWORD = $dWord[0]; RequiredVersion = $RequiredVersion}
    return $DotNetInfo
}