Skip to content

Commit

Permalink
(#2738) Move setup module to install .Net 4.8
Browse files Browse the repository at this point in the history
This moves the setup module used during install and upgrade to
check for and install .Net 4.8 instead of .Net 4.0

The timing of the check/install is moved to before the Chocolatey
installation directory is created. This allows for re-running the
install.ps1 script if a reboot is required without needing a manual
cleanup of the partial installation.

This also adds a check for the other reboot required exit code of
1641, and throws the install/upgrade if the installer requires
a reboot.
  • Loading branch information
TheCakeIsNaOH committed Dec 15, 2022
1 parent cdd1c00 commit 36c2389
Showing 1 changed file with 33 additions and 31 deletions.
64 changes: 33 additions & 31 deletions nuspec/chocolatey/chocolatey/tools/chocolateysetup.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ function Initialize-Chocolatey {
This will initialize the Chocolatey tool by
a) setting up the "chocolateyPath" (the location where all chocolatey nuget packages will be installed)
b) Installs chocolatey into the "chocolateyPath"
c) Installs .net 4.0 if needed
c) Installs .net 4.8 if needed
d) Adds Chocolatey to the PATH environment variable so you have access to the choco commands.
.PARAMETER ChocolateyPath
Allows you to override the default path of (C:\ProgramData\chocolatey\) by specifying a directory chocolatey will install nuget packages.
Expand All @@ -124,6 +124,8 @@ param(
$installModule = Join-Path $thisScriptFolder 'chocolateyInstall\helpers\chocolateyInstaller.psm1'
Import-Module $installModule -Force

Install-DotNet48IfMissing

if ($chocolateyPath -eq '') {
$programData = [Environment]::GetFolderPath("CommonApplicationData")
$chocolateyPath = Join-Path "$programData" 'chocolatey'
Expand Down Expand Up @@ -195,7 +197,6 @@ Creating Chocolatey folders if they do not already exist.
}

Add-ChocolateyProfile
Install-DotNet4IfMissing
Invoke-Chocolatey-Initial
if ($env:ChocolateyExitCode -eq $null -or $env:ChocolateyExitCode -eq '') {
$env:ChocolateyExitCode = 0
Expand Down Expand Up @@ -735,58 +736,59 @@ if (Test-Path($ChocolateyProfile)) {
}
}

$netFx4InstallTries = 0
$netFx48InstallTries = 0

function Install-DotNet4IfMissing {
function Install-DotNet48IfMissing {
param(
$forceFxInstall = $false
)
# we can't take advantage of any chocolatey module functions, because they
# haven't been unpacked because they require .NET Framework 4.0
# haven't been unpacked because they require .NET Framework 4.8

Write-Debug "Install-DotNet4IfMissing called with `$forceFxInstall=$forceFxInstall"
Write-Debug "Install-DotNet48IfMissing called with `$forceFxInstall=$forceFxInstall"
$NetFxArch = "Framework"
if ([IntPtr]::Size -eq 8) {$NetFxArch="Framework64" }

$NetFx4ClientUrl = 'https://download.microsoft.com/download/5/6/2/562A10F9-C9F4-4313-A044-9C94E0A8FAC8/dotNetFx40_Client_x86_x64.exe'
$NetFx4FullUrl = 'https://download.microsoft.com/download/9/5/A/95A9616B-7A37-4AF6-BC36-D6EA96C8DAAE/dotNetFx40_Full_x86_x64.exe'
$NetFx4Url = $NetFx4FullUrl
$NetFx4Path = "$tempDir"
$NetFx4InstallerFile = 'dotNetFx40_Full_x86_x64.exe'
$NetFx4Installer = Join-Path $NetFx4Path $NetFx4InstallerFile

if ((!(Test-Path "$env:SystemRoot\Microsoft.Net\$NetFxArch\v4.0.30319") -and !(Test-Path "C:\Windows\Microsoft.Net\$NetFxArch\v4.0.30319")) -or $forceFxInstall) {
Write-Output "'$env:SystemRoot\Microsoft.Net\$NetFxArch\v4.0.30319' was not found or this is forced"
if (!(Test-Path $NetFx4Path)) {
Write-Output "Creating folder `'$NetFx4Path`'"
$null = New-Item -Path "$NetFx4Path" -ItemType Directory
$NetFx48Url = 'https://download.visualstudio.microsoft.com/download/pr/2d6bb6b2-226a-4baa-bdec-798822606ff1/8494001c276a4b96804cde7829c04d7f/ndp48-x86-x64-allos-enu.exe'
$NetFx48Path = "$tempDir"
$NetFx48InstallerFile = 'ndp48-x86-x64-allos-enu.exe'
$NetFx48Installer = Join-Path $NetFx48Path $NetFx48InstallerFile

if (((Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full" -ErrorAction SilentlyContinue).Release -lt 528040) -or $forceFxInstall) {
Write-Output "The registry key for .Net 4.8 was not found or this is forced"

if (Test-Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\RebootPending") {
Write-Warning "A reboot is required. `n If you encounter errors, reboot the system and attempt the operation again"
}

$netFx4InstallTries += 1
$netFx48InstallTries += 1

if (!(Test-Path $NetFx4Installer)) {
Write-Output "Downloading `'$NetFx4Url`' to `'$NetFx4Installer`' - the installer is 40+ MBs, so this could take a while on a slow connection."
(New-Object Net.WebClient).DownloadFile("$NetFx4Url","$NetFx4Installer")
if (!(Test-Path $NetFx48Installer)) {
Write-Output "Downloading `'$NetFx48Url`' to `'$NetFx48Installer`' - the installer is 100+ MBs, so this could take a while on a slow connection."
(New-Object Net.WebClient).DownloadFile("$NetFx48Url","$NetFx48Installer")
}

$psi = New-Object System.Diagnostics.ProcessStartInfo
$psi.WorkingDirectory = "$NetFx4Path"
$psi.FileName = "$NetFx4InstallerFile"
$psi.WorkingDirectory = "$NetFx48Path"
$psi.FileName = "$NetFx48InstallerFile"
# https://msdn.microsoft.com/library/ee942965(v=VS.100).aspx#command_line_options
# http://blogs.msdn.com/b/astebner/archive/2010/05/12/10011664.aspx
# For the actual setup.exe (if you want to unpack first) - /repair /x86 /x64 /ia64 /parameterfolder Client /q /norestart
$psi.Arguments = "/q /norestart /repair"
$psi.Arguments = "/q /norestart"

Write-Output "Installing `'$NetFx4Installer`' - this may take awhile with no output."
Write-Output "Installing `'$NetFx48Installer`' - this may take awhile with no output."
$s = [System.Diagnostics.Process]::Start($psi);
$s.WaitForExit();
if ($s.ExitCode -ne 0 -and $s.ExitCode -ne 3010) {
if ($netFx4InstallTries -ge 2) {
if ($s.ExitCode -eq 1641 -or $s.ExitCode -eq 3010) {
throw ".NET Framework 4.8 was installed, but a reboot is required. `n Please reboot the system and try to install/upgrade Chocolatey again."
}
if ($s.ExitCode -ne 0) {
if ($netFx48InstallTries -ge 2) {
Write-ChocolateyError ".NET Framework install failed with exit code `'$($s.ExitCode)`'. `n This will cause the rest of the install to fail."
throw "Error installing .NET Framework 4.0 (exit code $($s.ExitCode)). `n Please install the .NET Framework 4.0 manually and then try to install Chocolatey again. `n Download at `'$NetFx4Url`'"
throw "Error installing .NET Framework 4.8 (exit code $($s.ExitCode)). `n Please install the .NET Framework 4.8 manually and reboot the system `n and then try to install/upgrade Chocolatey again. `n Download at `'$NetFx48Url`'"
} else {
Write-ChocolateyWarning "Try #$netFx4InstallTries of .NET framework install failed with exit code `'$($s.ExitCode)`'. Trying again."
Install-DotNet4IfMissing $true
Write-ChocolateyWarning "Try #$netFx48InstallTries of .NET framework install failed with exit code `'$($s.ExitCode)`'. Trying again."
Install-DotNet48IfMissing $true
}
}
}
Expand Down

0 comments on commit 36c2389

Please sign in to comment.