diff --git a/scripts/obtain/dotnet-install.ps1 b/scripts/obtain/dotnet-install.ps1 index a68031fe4..3cb45ad10 100644 --- a/scripts/obtain/dotnet-install.ps1 +++ b/scripts/obtain/dotnet-install.ps1 @@ -115,6 +115,16 @@ function Get-Version-Info-From-Version-Text([string]$VersionText) { return $VersionInfo } +function Load-Assembly([string] $Assembly) { + try { + Add-Type -Assembly $Assembly | Out-Null + } + catch { + # On Nano Server, Powershell Core Edition is used. Add-Type is unable to resolve base class assemblies because they are not GAC'd. + # Loading the base class assemblies is not unnecessary as the types will automatically get resolved. + } +} + function Get-Latest-Version-Info([string]$AzureFeed, [string]$AzureChannel, [string]$CLIArchitecture) { Say-Invocation $MyInvocation @@ -126,12 +136,23 @@ function Get-Latest-Version-Info([string]$AzureFeed, [string]$AzureChannel, [str $VersionFileUrl = "$AzureFeed/Sdk/$AzureChannel/latest.version" } - $Response = Invoke-WebRequest -UseBasicParsing $VersionFileUrl + try { + # HttpClient is used vs Invoke-WebRequest in order to support Nano Server which doesn't support the Invoke-WebRequest cmdlet. + Load-Assembly -Assembly System.Net.Http + $HttpClient = New-Object System.Net.Http.HttpClient + $Response = $HttpClient.GetAsync($VersionFileUrl).Result + $StringContent = $Response.Content.ReadAsStringAsync().Result - switch ($Response.Headers.'Content-Type'){ - { ($_ -eq "application/octet-stream") } { $VersionText = [Text.Encoding]::UTF8.GetString($Response.Content) } - { ($_ -eq "text/plain") } { $VersionText = $Response.Content } - default { throw "``$Response.Headers.'Content-Type'`` is an unknown .version file content type." } + switch ($Response.Content.Headers.ContentType) { + { ($_ -eq "application/octet-stream") } { $VersionText = [Text.Encoding]::UTF8.GetString($StringContent) } + { ($_ -eq "text/plain") } { $VersionText = $StringContent } + default { throw "``$Response.Content.Headers.ContentType`` is an unknown .version file content type." } + } + } + finally { + if ($HttpClient -ne $null) { + $HttpClient.Dispose() + } } @@ -281,7 +302,7 @@ function Get-List-Of-Directories-And-Versions-To-Unpack-From-Dotnet-Package([Sys function Extract-Dotnet-Package([string]$ZipPath, [string]$OutPath) { Say-Invocation $MyInvocation - Add-Type -Assembly System.IO.Compression.FileSystem | Out-Null + Load-Assembly -Assembly System.IO.Compression.FileSystem Set-Variable -Name Zip try { $Zip = [System.IO.Compression.ZipFile]::OpenRead($ZipPath) @@ -308,6 +329,26 @@ function Extract-Dotnet-Package([string]$ZipPath, [string]$OutPath) { } } +function DownloadFile([Uri]$Uri, [string]$OutPath) { + try { + # HttpClient is used vs Invoke-WebRequest in order to support Nano Server which doesn't support the Invoke-WebRequest cmdlet. + Load-Assembly -Assembly System.Net.Http + $HttpClient = New-Object System.Net.Http.HttpClient + $Stream = $HttpClient.GetAsync($Uri).Result.Content.ReadAsStreamAsync().Result + $File = [System.IO.File]::Create($OutPath) + $Stream.CopyTo($File) + $File.Close() + } + finally { + if ($HttpClient -ne $null) { + $HttpClient.Dispose() + } + if ($Stream -ne $null) { + $Stream.Dispose() + } + } +} + $AzureChannel = Get-Azure-Channel-From-Channel -Channel $Channel $CLIArchitecture = Get-CLIArchitecture-From-Architecture $Architecture $SpecificVersion = Get-Specific-Version-From-Version -AzureFeed $AzureFeed -AzureChannel $AzureChannel -CLIArchitecture $CLIArchitecture -Version $Version @@ -337,7 +378,7 @@ New-Item -ItemType Directory -Force -Path $InstallRoot | Out-Null foreach ($DownloadLink in $DownloadLinks) { $ZipPath = [System.IO.Path]::GetTempFileName() Say "Downloading $DownloadLink" - $resp = Invoke-WebRequest -UseBasicParsing $DownloadLink -OutFile $ZipPath + DownloadFile -Uri $DownloadLink -OutPath $ZipPath Say "Extracting zip from $DownloadLink" Extract-Dotnet-Package -ZipPath $ZipPath -OutPath $InstallRoot