Skip to content

Instantly share code, notes, and snippets.

@patrick-steele
Last active April 22, 2019 21:31
Show Gist options
  • Save patrick-steele/59637ec6fc4299856e3c8efbde4d8bbb to your computer and use it in GitHub Desktop.
Save patrick-steele/59637ec6fc4299856e3c8efbde4d8bbb to your computer and use it in GitHub Desktop.
#Requires -Version 3.0
<#
.DESCRIPTION
Install .Net Framework 4.7.2 and then install .NET Core runtime
#>
[CmdletBinding()]
Param(
[switch]$norestart
)
Set-StrictMode -Version Latest
$logFile = Join-Path $env:TEMP -ChildPath "InstallNetFx472ScriptLog.txt"
# Check if the latest NetFx472 version exists
$netFxKey = Get-ItemProperty -Path "HKLM:\SOFTWARE\\Microsoft\\NET Framework Setup\\NDP\\v4\\Full\\" -ErrorAction Ignore
$ret = -1
if($netFxKey -and $netFxKey.Release -ge 461808) {
"$(Get-Date): The machine already has NetFx 4.7.2 or later version installed. Skipping." | Tee-Object -FilePath $logFile -Append
$ret = 0
} else {
# Download the latest NetFx472
$setupFileSourceUri = "https://download.microsoft.com/download/0/5/C/05C1EC0E-D5EE-463B-BFE3-9311376A6809/NDP472-KB4054531-Web.exe"
$setupFileLocalPath = Join-Path $env:TEMP -ChildPath "NDP472-KB4054531-Web.exe"
"$(Get-Date): Start to download NetFx 4.7.2 to $setupFileLocalPath." | Tee-Object -FilePath $logFile -Append
if(Test-Path $setupFileLocalPath)
{
Remove-Item -Path $setupFileLocalPath -Force
}
$webClient = New-Object System.Net.WebClient
$retry = 0
do
{
try {
$webClient.DownloadFile($setupFileSourceUri, $setupFileLocalPath)
break
}
catch [Net.WebException] {
$retry++
if($retry -gt 3) {
"$(Get-Date): Download failed as the network connection issue. Exception detail: $_" | Tee-Object -FilePath $logFile -Append
break
}
$waitInSecond = $retry * 30
"$(Get-Date): It looks the Internet network is not available now. Simply wait for $waitInSecond seconds and try again." | Tee-Object -FilePath $logFile -Append
Start-Sleep -Second $waitInSecond
}
} while ($true)
if(!(Test-Path $setupFileLocalPath))
{
"$(Get-Date): Failed to download NetFx 4.7.2 setup package." | Tee-Object -FilePath $logFile -Append
exit -1
}
# Install NetFx472
$setupLogFilePath = Join-Path $env:TEMP -ChildPath "NetFx472SetupLog.txt"
if($norestart) {
$arguments = "/q /norestart /serialdownload /log $setupLogFilePath"
}
else {
$arguments = "/q /serialdownload /log $setupLogFilePath"
}
"$(Get-Date): Start to install NetFx 4.7.2" | Tee-Object -FilePath $logFile -Append
$process = Start-Process -FilePath $setupFileLocalPath -ArgumentList $arguments -Wait -PassThru
if(-not $process) {
"$(Get-Date): Install NetFx failed." | Tee-Object -FilePath $logFile -Append
}
else {
$exitCode = $process.ExitCode
# 0, 1641 and 3010 indicate success. See https://msdn.microsoft.com/en-us/library/ee390831(v=vs.110).aspx for detail.
if($exitCode -eq 0 -or $exitCode -eq 1641 -or $exitCode -eq 3010) {
"$(Get-Date): Install NetFx succeeded with exit code : $exitCode." | Tee-Object -FilePath $logFile -Append
$ret = 0
}
else {
"$(Get-Date): Install NetFx failed with exit code : $exitCode." | Tee-Object -FilePath $logFile -Append
}
}
}
# now install .NET Core
& ./Install-DotnetVMSS.ps1
exit $ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment