Updated Add-Type logic to account for Powershell Core Edition

This commit is contained in:
Michael Simons (VISUAL STUDIO) 2016-08-08 15:03:36 -05:00
parent a34e93c340
commit b53e254fbd

View file

@ -115,6 +115,16 @@ function Get-Version-Info-From-Version-Text([string]$VersionText) {
return $VersionInfo 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) { function Get-Latest-Version-Info([string]$AzureFeed, [string]$AzureChannel, [string]$CLIArchitecture) {
Say-Invocation $MyInvocation Say-Invocation $MyInvocation
@ -128,7 +138,7 @@ function Get-Latest-Version-Info([string]$AzureFeed, [string]$AzureChannel, [str
try { try {
# HttpClient is used vs Invoke-WebRequest in order to support Nano Server which doesn't support the Invoke-WebRequest cmdlet. # HttpClient is used vs Invoke-WebRequest in order to support Nano Server which doesn't support the Invoke-WebRequest cmdlet.
Add-Type -Assembly System.Net.Http | Out-Null Load-Assembly -Assembly System.Net.Http
$HttpClient = New-Object System.Net.Http.HttpClient $HttpClient = New-Object System.Net.Http.HttpClient
$Response = $HttpClient.GetAsync($VersionFileUrl).Result $Response = $HttpClient.GetAsync($VersionFileUrl).Result
$StringContent = $Response.Content.ReadAsStringAsync().Result $StringContent = $Response.Content.ReadAsStringAsync().Result
@ -292,13 +302,7 @@ function Get-List-Of-Directories-And-Versions-To-Unpack-From-Dotnet-Package([Sys
function Extract-Dotnet-Package([string]$ZipPath, [string]$OutPath) { function Extract-Dotnet-Package([string]$ZipPath, [string]$OutPath) {
Say-Invocation $MyInvocation Say-Invocation $MyInvocation
try { Load-Assembly -Assembly System.IO.Compression.FileSystem
Add-Type -Assembly System.IO.Compression.FileSystem | Out-Null
}
catch {
# Loading FileSystem is unnecessary and will fail on Nano Server
}
Set-Variable -Name Zip Set-Variable -Name Zip
try { try {
$Zip = [System.IO.Compression.ZipFile]::OpenRead($ZipPath) $Zip = [System.IO.Compression.ZipFile]::OpenRead($ZipPath)
@ -328,7 +332,7 @@ function Extract-Dotnet-Package([string]$ZipPath, [string]$OutPath) {
function DownloadFile([Uri]$Uri, [string]$OutPath) { function DownloadFile([Uri]$Uri, [string]$OutPath) {
try { try {
# HttpClient is used vs Invoke-WebRequest in order to support Nano Server which doesn't support the Invoke-WebRequest cmdlet. # HttpClient is used vs Invoke-WebRequest in order to support Nano Server which doesn't support the Invoke-WebRequest cmdlet.
Add-Type -Assembly System.Net.Http | Out-Null Load-Assembly -Assembly System.Net.Http
$HttpClient = New-Object System.Net.Http.HttpClient $HttpClient = New-Object System.Net.Http.HttpClient
$Stream = $HttpClient.GetAsync($Uri).Result.Content.ReadAsStreamAsync().Result $Stream = $HttpClient.GetAsync($Uri).Result.Content.ReadAsStreamAsync().Result
$File = [System.IO.File]::Create($OutPath) $File = [System.IO.File]::Create($OutPath)