.NET - Check .NET Framework Version

Octopus.Script exported 2019-06-06 by hullscotty1986 belongs to ‘ASP.NET’ category.

Check if given .NET framework version (or greater) is installed.

Parameters

When steps based on the template are included in a project’s deployment process, the parameters below can be set.

Target .NET framework version

TargetVersion = 4.5.2

The target .NET framework version you expect to be installed in the machine. For example, 4.5.2.

Exact

Exact = false

If you check “Exact”, it means the installed .NET framework version MUST match target version.

Otherwise, as long as the installed .NET framework version is greater than or equal to target version, the check will pass.

Script body

Steps based on this template will execute the following PowerShell script.

# This script is based on MSDN: https://msdn.microsoft.com/en-us/library/hh925568

$releaseVersionMapping = @{
    378389 = '4.5'     # 4.5
    378675 = '4.5.1'   # 4.5.1 installed with Windows 8.1
    378758 = '4.5.1'   # 4.5.1 installed on Windows 8, Windows 7 SP1, or Windows Vista SP2
    379893 = '4.5.2'   # 4.5.2
    393295 = '4.6'     # 4.6 installed with Windows 10
    393297 = '4.6'     # 4.6 installed on all other Windows OS versions
    394254 = '4.6.1'   # 4.6.1 installed on Windows 10
    394271 = '4.6.1'   # 4.6.1 installed on all other Windows OS versions
    394802 = '4.6.2'   # 4.6.2 installed on Windows 10
    394806 = '4.6.2'   # 4.6.2 installed on all other Windows OS versions
    460798 = '4.7'     # 4.7 installed on Windows 10
    460805 = '4.7'     # 4.7 installed on all other Windows OS versions
    461308 = '4.7.1'   # 4.7.1 installed on Windows 10
    461310 = '4.7.1'   # 4.7.1 installed on all other Windows OS versions
    461808 = '4.7.2'   # 4.7.2 installed on Windows 10
    461814 = '4.7.2'   # 4.7.2 installed on all other Windows OS versions
    528040 = '4.8'     #4.8 installed on Windows 10
    528049 = '4.8'     #4.8 installed on all other Windows OS versions1
    528372 = '4.8'     # 4.8 Windows 10 May 2020 Update
}

function Get-DotNetFrameworkVersions() {
    $dotNetVersions = @()
    if ($baseKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', '')) {
        # To find .NET Framework versions (.NET Framework 1-4)$dotNetVersions
        if ($ndpKey = $baseKey.OpenSubKey('SOFTWARE\Microsoft\NET Framework Setup\NDP')) {
            foreach ($versionKeyName in $ndpKey.GetSubKeyNames()) {
                if ($versionKeyName -match 'v[2|3]') {
                    $versionKey = $ndpKey.OpenSubKey($versionKeyName)
                    if ($versionKey.GetValue('Version', '') -ne '') {
                        $version = [version] ($versionKey.GetValue('Version'))
                        $dotNetVersions += "$($version.Major).$($version.Minor)"
                    }
                }
            }
        
            # for .NET 4.0
            if ($ndp40Key = $ndpKey.OpenSubKey("v4.0")) {
                foreach ($subKeyName in $ndp40Key.GetSubKeyNames()) {
                    $versionKey = $ndp40Key.OpenSubKey($subKeyName)
                    $version = [version]($versionKey.GetValue('Version', ''))
                    $dotNetVersions += "$($version.Major).$($version.Minor)"
                }
            }
        }

        # To find .NET Framework versions (.NET Framework 4.5 and later)
        if ($ndp4Key = $baseKey.OpenSubKey('SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full')) {
            $releaseKey = $ndp4Key.GetValue('Release')
            $dotNetVersions += $releaseVersionMapping[$releaseKey]
        }
    }
    return $dotNetVersions
}

$targetVersion = $OctopusParameters['TargetVersion'].Trim()
$exact = [boolean]::Parse($OctopusParameters['Exact'])

$matchedVersions = Get-DotNetFrameworkVersions | Where-Object { if ($exact) { $_ -eq $targetVersion } else { $_ -ge $targetVersion } }
if (!$matchedVersions) { 
    throw "Can't find .NET $targetVersion installed in the machine."
}
$matchedVersions | foreach { Write-Host "Found .NET $_ installed in the machine." }

Provided under the Apache License version 2.0.

Report an issue

To use this template in Octopus Deploy, copy the JSON below and paste it into the Library → Step templates → Import dialog.

{
  "Id": "b15c6b3d-20a1-4059-b0e4-75bef1ff41d5",
  "Name": ".NET - Check .NET Framework Version",
  "Description": "Check if given .NET framework version (or greater) is installed.",
  "Version": 8,
  "ExportedAt": "2019-06-06T07:39:33.409Z",
  "ActionType": "Octopus.Script",
  "Author": "hullscotty1986",
  "Parameters": [
    {
      "Id": "e902b08b-2fc3-4f62-aec0-5225533cace8",
      "Name": "TargetVersion",
      "Label": "Target .NET framework version",
      "HelpText": "The target .NET framework version you expect to be installed in the machine. For example, 4.5.2.",
      "DefaultValue": "4.5.2",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      },
      "Links": {}
    },
    {
      "Id": "205e297b-c72c-4d9b-a6fe-9a20ab62d5b0",
      "Name": "Exact",
      "Label": "Exact",
      "HelpText": "If you check \"Exact\", it means the installed .NET framework version MUST match target version.\n\nOtherwise, as long as the installed .NET framework version is greater than or equal to target version, the check will pass.",
      "DefaultValue": "false",
      "DisplaySettings": {
        "Octopus.ControlType": "Checkbox"
      },
      "Links": {}
    }
  ],
  "Properties": {
    "Octopus.Action.Script.Syntax": "PowerShell",
    "Octopus.Action.Script.ScriptSource": "Inline",
    "Octopus.Action.Script.ScriptBody": "# This script is based on MSDN: https://msdn.microsoft.com/en-us/library/hh925568\n\n$releaseVersionMapping = @{\n    378389 = '4.5'     # 4.5\n    378675 = '4.5.1'   # 4.5.1 installed with Windows 8.1\n    378758 = '4.5.1'   # 4.5.1 installed on Windows 8, Windows 7 SP1, or Windows Vista SP2\n    379893 = '4.5.2'   # 4.5.2\n    393295 = '4.6'     # 4.6 installed with Windows 10\n    393297 = '4.6'     # 4.6 installed on all other Windows OS versions\n    394254 = '4.6.1'   # 4.6.1 installed on Windows 10\n    394271 = '4.6.1'   # 4.6.1 installed on all other Windows OS versions\n    394802 = '4.6.2'   # 4.6.2 installed on Windows 10\n    394806 = '4.6.2'   # 4.6.2 installed on all other Windows OS versions\n    460798 = '4.7'     # 4.7 installed on Windows 10\n    460805 = '4.7'     # 4.7 installed on all other Windows OS versions\n    461308 = '4.7.1'   # 4.7.1 installed on Windows 10\n    461310 = '4.7.1'   # 4.7.1 installed on all other Windows OS versions\n    461808 = '4.7.2'   # 4.7.2 installed on Windows 10\n    461814 = '4.7.2'   # 4.7.2 installed on all other Windows OS versions\n    528040 = '4.8'     #4.8 installed on Windows 10\n    528049 = '4.8'     #4.8 installed on all other Windows OS versions1\n    528372 = '4.8'     # 4.8 Windows 10 May 2020 Update\n}\n\nfunction Get-DotNetFrameworkVersions() {\n    $dotNetVersions = @()\n    if ($baseKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', '')) {\n        # To find .NET Framework versions (.NET Framework 1-4)$dotNetVersions\n        if ($ndpKey = $baseKey.OpenSubKey('SOFTWARE\\Microsoft\\NET Framework Setup\\NDP')) {\n            foreach ($versionKeyName in $ndpKey.GetSubKeyNames()) {\n                if ($versionKeyName -match 'v[2|3]') {\n                    $versionKey = $ndpKey.OpenSubKey($versionKeyName)\n                    if ($versionKey.GetValue('Version', '') -ne '') {\n                        $version = [version] ($versionKey.GetValue('Version'))\n                        $dotNetVersions += \"$($version.Major).$($version.Minor)\"\n                    }\n                }\n            }\n        \n            # for .NET 4.0\n            if ($ndp40Key = $ndpKey.OpenSubKey(\"v4.0\")) {\n                foreach ($subKeyName in $ndp40Key.GetSubKeyNames()) {\n                    $versionKey = $ndp40Key.OpenSubKey($subKeyName)\n                    $version = [version]($versionKey.GetValue('Version', ''))\n                    $dotNetVersions += \"$($version.Major).$($version.Minor)\"\n                }\n            }\n        }\n\n        # To find .NET Framework versions (.NET Framework 4.5 and later)\n        if ($ndp4Key = $baseKey.OpenSubKey('SOFTWARE\\Microsoft\\NET Framework Setup\\NDP\\v4\\Full')) {\n            $releaseKey = $ndp4Key.GetValue('Release')\n            $dotNetVersions += $releaseVersionMapping[$releaseKey]\n        }\n    }\n    return $dotNetVersions\n}\n\n$targetVersion = $OctopusParameters['TargetVersion'].Trim()\n$exact = [boolean]::Parse($OctopusParameters['Exact'])\n\n$matchedVersions = Get-DotNetFrameworkVersions | Where-Object { if ($exact) { $_ -eq $targetVersion } else { $_ -ge $targetVersion } }\nif (!$matchedVersions) { \n    throw \"Can't find .NET $targetVersion installed in the machine.\"\n}\n$matchedVersions | foreach { Write-Host \"Found .NET $_ installed in the machine.\" }",
    "Octopus.Action.RunOnServer": "false"
  },
  "Category": "ASP.NET",
  "HistoryUrl": "https://github.com/OctopusDeploy/Library/commits/master/step-templates//opt/buildagent/work/75443764cd38076d/step-templates/windows-check-net-framework-version.json",
  "Website": "/step-templates/b15c6b3d-20a1-4059-b0e4-75bef1ff41d5",
  "Logo": "iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAMAAACahl6sAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAwBQTFRF////ZsTuFHO0FVSaFJTYAAAAY8z0FH3AWcfziMfsF4zTFHq9GYrQTZjOG4jOHKLiB5vhDYPLHIXJ2vH7NJzdecvxRqni5PX8HK3renp6JY3SLKnmWsPuMbLrjbDaM6rlKq3pJK3pD3a+KZHVpNv1qOP5O7LpR6TcDn3FVLzs1uj0S7vsKnOwedP2D3nAFIHFDKPmE63tLaDei9LyFIbJktTzCpbbBJncndn1K5vbS8X0G4HFEHG4IZLUFliddb3nxOv6mdXzqt72EqbnNK7oJKXkEW618/r+y+37hNLzV6XXFlyhDK3tI4PLFlKWMaThG3m9mpqaFFqgQrXqkMzuJYnOfcPqC4rSTLboeLTcEWqxUqzhOIjEsOX6suH3g87xGW2xGnW5YbPjG33BOKHfIannInvDNLvxGnG1vOr7Xb/tb8XuE2KpEmasE2WiFW+1FWClBaHlGWm8GWaqGGKlD53eGWmtFWitPaXgW7DiLKTiGGSzFV2kF1+rFXa4IpvbKX++arrnK5bYFp/gO6vlstzzZL3pUMLtE1+lEHW9o9byFWqwFmOuLZLPEYvQi9n3k934O5LNEmivE16amd33P8HzE2CncsruRa/lt+P3EHO7EmSqLYPDZrfmP5rUJYPGFliaFWOoFWesS7LmE26tE3CvE2ilMpTSE2yqm9Lvveb4E2qoOp3bRbjsBJfYFW2yHKjnFXe8web4br3oltf0abXgFWWqEWuzE2KeF2CjF1yfFWayfcrvv7+/qs7oCpHXM7bgDnvDBJXXLozK0e/7L67pGmW49/z+M5jatNPqEnK8ElqeQLzm7/j8Ojo6E2+5FGu2FWm039/f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92RfQch9krtvPtYTFKo5HrdN3fbnnxPLI6jPAMW3q3iIhy6AQgZ3y0ZXeMP3aNiOQNnLqLxeuOKS/x79Gg12i8dSeIAonxFwgRxCPgWS4RQ97UakGRCaWW3USfXsJ45K09yvJ41WbOnBWMh6en53HPf1k0TPqCh3mWXj9Wp2u3BA+GtReR9mf3sLB4gZhYTfgeotQyyXShawsXLjSZLInu1ttzZcGF6abZ2/Fzk754ztPi+HPHeZjB6/XeOl3/7tt4DPlcGKYL+/KZVzFfaEXABMiJTkJ+OL1wYafJkoVLkjqZf+HCse7x49zxhnPnGj3P8SBSqS/T6XSbukSurQXOC+Hfgn5G93jL3xFVwtDkH9uSLE0awOm8PPx5SdL9pPv3k3ZtunCk37cWLBGLc56e5zx/vUhjpb4a/ujO1CdZMt2lTlf18xaVG+28XBxdHO3DvheW/vurr7o9Tpt+WWJ5P2nypgsXf/oz+zQU+eK5STyIuFZKcckHFBh+uHbt2AmdX9kveAFh0C273z2Nb82yDK18EsTxX9/YEzVx4DG1q4QvbpeVKX7haxR3LSgUCoVCoVAoFAqFQqFQKBQKhUKhUCgUCoVCoVAoFAqFQqFQKBQKhUKhUCgUCoVCeSb+L8AAULotxGyLK9IAAAAASUVORK5CYII=",
  "$Meta": {
    "Type": "ActionTemplate"
  }
}

History

Page updated on Thursday, June 6, 2019