15

I can install .NET Framework 4.5 on Windows Server 2012 R2 using PowerShell DSC via the WindowsFeature resource and the NET-Framework-45-Core feature. My question is, how do I use PowerShell DSC to ensure that .NET 4.5.2 is installed?

2

2 Answers 2

14

Not sure if this is still required by the OP, but I had the exact same challenge recently and found many problems with the installer itself, when trying to use just the package resource on a 2012 R2 Server. Ended-up having to write a script resource and use the web installer as the full package kept failing to decompress with a very generic error.

Anyway, here is a working script resource I ended up with:

Configuration Net452Install
{
    node "localhost"
    {

        LocalConfigurationManager
        {
            RebootNodeIfNeeded = $true
        }

        Script Install_Net_4.5.2
        {
            SetScript = {
                $SourceURI = "https://download.microsoft.com/download/B/4/1/B4119C11-0423-477B-80EE-7A474314B347/NDP452-KB2901954-Web.exe"
                $FileName = $SourceURI.Split('/')[-1]
                $BinPath = Join-Path $env:SystemRoot -ChildPath "Temp\$FileName"

                if (!(Test-Path $BinPath))
                {
                    Invoke-Webrequest -Uri $SourceURI -OutFile $BinPath
                }

                write-verbose "Installing .Net 4.5.2 from $BinPath"
                write-verbose "Executing $binpath /q /norestart"
                Sleep 5
                Start-Process -FilePath $BinPath -ArgumentList "/q /norestart" -Wait -NoNewWindow            
                Sleep 5
                Write-Verbose "Setting DSCMachineStatus to reboot server after DSC run is completed"
                $global:DSCMachineStatus = 1
            }

            TestScript = {
                [int]$NetBuildVersion = 379893

                if (Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full' | %{$_ -match 'Release'})
                {
                    [int]$CurrentRelease = (Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full').Release
                    if ($CurrentRelease -lt $NetBuildVersion)
                    {
                        Write-Verbose "Current .Net build version is less than 4.5.2 ($CurrentRelease)"
                        return $false
                    }
                    else
                    {
                        Write-Verbose "Current .Net build version is the same as or higher than 4.5.2 ($CurrentRelease)"
                        return $true
                    }
                }
                else
                {
                    Write-Verbose ".Net build version not recognised"
                    return $false
                }
            }

            GetScript = {
                if (Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full' | %{$_ -match 'Release'})
                {
                    $NetBuildVersion =  (Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full').Release
                    return $NetBuildVersion
                }
                else
                {
                    Write-Verbose ".Net build version not recognised"
                    return ".Net 4.5.2 not found"
                }
            }
        }
    }
}

Net452Install -OutputPath $env:SystemDrive:\DSCconfig
Set-DscLocalConfigurationManager -ComputerName localhost -Path $env:SystemDrive\DSCconfig -Verbose
Start-DscConfiguration -ComputerName localhost -Path $env:SystemDrive:\DSCconfig -Verbose -Wait -Force
3
  • Great answer, this approach also works for .net 4.6.1. I tried a variety of existing DSC integrations (chocolatey, cDotNetInstaller, etc...) and they all failed with the 4.6.1 installer. This is the first one that worked. Kudos.
    – swannee
    Jun 17, 2016 at 22:41
  • Agreed; nice answer. I want to mention this also works through Chef and the dsc_script resource. Oct 12, 2016 at 18:09
  • I also confirmed this works with .net 4.7, with NetBuildVersion = 461310 and grabbing download.microsoft.com/download/A/E/A/…
    – JohnnyFun
    Nov 14, 2017 at 20:09
2

According to this Microsoft Technet article, the name of the feature to install should be one from the Get-WindowsFeature command's result. Thus, if .NET 4.5.2 does not appear in the list, you can't ensure it's installed via DSC.

Name Indicates the name of the role or feature that you want to ensure is added or removed. This is the same as the Name property from the Get-WindowsFeature cmdlet, and not the display name of the role or feature.

So I guess you'll have to install the major version via DCS (4.5), and then figure out the best solution to update it to 4.5.2.

2
  • Seems like a fairly major oversight. New PowerShell DSC can install software as new as two-and-a-half years old!
    – Mark
    May 29, 2015 at 14:57
  • 1
    It's not an oversight. The .NET 4.5.2 runtime is not a Windows Feature, so it can't be installed using the WindowsFeature resource. It's available either through Windows Update or as a separate download. The script will have to download and execute the appropriate installer. Oct 6, 2015 at 11:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .