PowerShell DSC: Script Resources to Download and Install .Net 4.6.2

Here’s a snippet from a PowerShell Desired State Configuration file that uses a Script Resource to download and install the .Net Framework 4.6.2. You can incorporate this into an existing DSC configuration file to ensure .Net 4.6.2 is installed.

    ....
    ....
    Script DownloadDotNet462
    {
      TestScript = {
        Test-Path "C:\WindowsAzure\NDP462-KB3151800-x86-x64-AllOS-ENU.exe"
      }
      SetScript ={
        $source = "http://download.microsoft.com/download/F/9/4/F942F07D-F26F-4F30-B4E3-EBD54FABA377/NDP462-KB3151800-x86-x64-AllOS-ENU.exe"
        $dest = "C:\WindowsAzure\NDP462-KB3151800-x86-x64-AllOS-ENU.exe"
        Invoke-WebRequest $source -OutFile $dest
      }
      GetScript = {@{Result = "DownloadDotNet462"}}
    }
    Script InstallDotNet462
    {
      TestScript = {
        $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' }
        If ($dotNetFull -eq $null) { $false } Else { $true }
      }
      SetScript ={
        $proc = Start-Process -FilePath "C:\WindowsAzure\NDP462-KB3151800-x86-x64-AllOS-ENU.exe" -ArgumentList "/quiet /norestart /log C:\WindowsAzure\NDP462-KB3151800-x86-x64-AllOS-ENU_install.log" -PassThru -Wait
        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)"
          }
        }
      }
      GetScript = {@{Result = "InstallDotNet462"}}
      DependsOn = "[Script]DownloadDotNet462"
    }
    ....
    ....

If you don’t mind reboots during the process, another helpful tip is to set the LocalConfigurationManager to reboot when needed, e.g.

    ....
    ....
    LocalConfigurationManager
    {        
      RebootNodeIfNeeded = $true # This is false by default
    }
    ....
    ....

Hope this helps!

This entry was posted in PowerShell, PowerShell DSC. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *