xInstallDotNet462Framework.psm1

[DscResource()]
class InstallDotNet462
{
    [string]$source = "http://download.microsoft.com/download/F/9/4/F942F07D-F26F-4F30-B4E3-EBD54FABA377/NDP462-KB3151800-x86-x64-AllOS-ENU.exe"
    [DscProperty(Key)]
    [string]$dest

    [void] Set()
    {
        # Download Installer File if needed
        if(!$this.IsExecutableAvailable())
        {
            $this.DownloadFile()
        }

        # Install DotNetFramework
        $proc = Start-Process -FilePath $this.dest -ArgumentList "/quiet /norestart /log C:\NDP462-KB3151800-x86-x64-AllOS-ENU_install.log" -PassThru -Wait
        
        # Cleanup Installer file
        if($this.IsExecutableAvailable())
        {
            Remove-Item $this.dest
        }
        
        Switch($proc.ExitCode)
        {
          0 {
            # Success
          }
          1603 {
            Throw "Failed installation"
          }
          1641 {
            # Restart required
            $global:DSCMachineStatus = 1                
          }
          3010 {
            # Restart required
            $global:DSCMachineStatus = 1                
          }
          5100 {
            Throw "Computer does not meet system requirements."
          }
          default {
            Throw "Unknown exit code $($proc.ExitCode)"
          }
        }
    }

    [bool] Test()
    {
        $dotNetFull = Get-ChildItem 'HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP' -recurse |
        Get-ItemProperty -name Version,Release -EA 0 |
        Where { $_.Version -match '4.6.01590' -and $_.PSChildName -eq 'Full' }
        return !($dotNetFull -eq $null)
    }

    [InstallDotNet462] Get()
    {
        return $this
    }

    [bool] IsExecutableAvailable()
    {
        return Test-Path $this.dest
    }

    [void] DownloadFile()
    {
        Invoke-WebRequest $this.source -OutFile $this.dest
    }
}