lib/mixlib/install/generator/powershell/scripts/helpers.ps1 in mixlib-install-1.1.0 vs lib/mixlib/install/generator/powershell/scripts/helpers.ps1 in mixlib-install-1.2.0

- old
+ new

@@ -1,18 +1,24 @@ function Get-PlatformVersion { - switch -regex ((get-wmiobject win32_operatingsystem).version) { - '10\.0\.\d+' {$platform_version = '2012r2'} + switch -regex ((Get-WMIQuery win32_operatingsystem).version) { + '10\.0\.\d+' {$platform_version = '2016'} '6\.3\.\d+' {$platform_version = '2012r2'} '6\.2\.\d+' {$platform_version = '2012'} '6\.1\.\d+' {$platform_version = '2008r2'} '6\.0\.\d+' {$platform_version = '2008'} } + + if(Test-Path 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Server\ServerLevels') { + $levels = Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Server\ServerLevels' + if($levels.NanoServer -eq 1) { $platform_version += 'nano' } + } + return $platform_version } function Get-PlatformArchitecture { - if ((get-wmiobject win32_operatingsystem).osarchitecture -like '64-bit') { + if ((Get-WMIQuery win32_operatingsystem).osarchitecture -like '64-bit') { $architecture = 'x86_64' } else { $architecture = 'i386' } return $architecture @@ -20,31 +26,28 @@ function New-Uri { param ($baseuri, $newuri) try { - new-object System.Uri $baseuri, $newuri + $base = new-object System.Uri $baseuri + new-object System.Uri $base, $newuri } catch [System.Management.Automation.MethodInvocationException]{ Write-Error "$($_.exception.message)" throw $_.exception } } function Get-WebContent { param ($uri, $filepath) - $proxy = New-Object -TypeName System.Net.WebProxy - $wc = new-object System.Net.WebClient - $proxy.Address = $env:http_proxy - $wc.Proxy = $proxy try { - if ([string]::IsNullOrEmpty($filepath)) { - $wc.downloadstring($uri) + if($PSVersionTable.PSEdition -eq 'Core') { + Get-WebContentOnCore $uri $filepath } else { - $wc.downloadfile($uri, $filepath) + Get-WebContentOnFullNet $uri $filepath } } catch { $exception = $_.Exception Write-Host "There was an error: " @@ -54,21 +57,65 @@ } while ($exception) throw "Failed to download from $uri." } } +function Get-WebContentOnFullNet { + param ($uri, $filepath) + + $proxy = New-Object -TypeName System.Net.WebProxy + $wc = new-object System.Net.WebClient + $proxy.Address = $env:http_proxy + $wc.Proxy = $proxy + + if ([string]::IsNullOrEmpty($filepath)) { + $wc.downloadstring($uri) + } + else { + $wc.downloadfile($uri, $filepath) + } +} + +function Get-WebContentOnCore { + param ($uri, $filepath) + + $handler = New-Object System.Net.Http.HttpClientHandler + $client = New-Object System.Net.Http.HttpClient($handler) + $client.Timeout = New-Object System.TimeSpan(0, 30, 0) + $cancelTokenSource = [System.Threading.CancellationTokenSource]::new() + $responseMsg = $client.GetAsync([System.Uri]::new($uri), $cancelTokenSource.Token) + $responseMsg.Wait() + if (!$responseMsg.IsCanceled) { + $response = $responseMsg.Result + if ($response.IsSuccessStatusCode) { + if ([string]::IsNullOrEmpty($filepath)) { + $response.Content.ReadAsStringAsync().Result + } + else { + $downloadedFileStream = [System.IO.FileStream]::new($filepath, [System.IO.FileMode]::Create, [System.IO.FileAccess]::Write) + $copyStreamOp = $response.Content.CopyToAsync($downloadedFileStream) + $copyStreamOp.Wait() + $downloadedFileStream.Close() + if ($copyStreamOp.Exception -ne $null) { + throw $copyStreamOp.Exception + } + } + } + } +} + function Test-ProjectPackage { [cmdletbinding()] param ($Path, $Algorithm = 'SHA256', $Hash) - if (-not (get-command get-filehash)) { + if (-not (get-command get-filehash -ErrorAction 'SilentlyContinue')) { function disposable($o){($o -is [IDisposable]) -and (($o | get-member | foreach-object {$_.name}) -contains 'Dispose')} function use($obj, [scriptblock]$sb){try {& $sb} catch [exception]{throw $_} finally {if (disposable $obj) {$obj.Dispose()}} } function Get-FileHash ($Path, $Algorithm) { $Path = (resolve-path $path).providerpath $hash = @{Algorithm = $Algorithm; Path = $Path} - use ($c = New-Object -TypeName Security.Cryptography.SHA256Managed) { + use ($c = Get-SHA256Converter) { use ($in = (gi $path).OpenRead()) { $hash.Hash = ([BitConverter]::ToString($c.ComputeHash($in))).Replace("-", "").ToUpper() } } new-object PSObject -Property $hash @@ -81,6 +128,26 @@ $Valid = $ActualHash -eq $Hash if (-not $Valid) { Write-Error "Failed to validate the downloaded installer. The expected $Algorithm hash was '$Hash' and the actual hash was '$ActualHash' for $path" } return $Valid +} + +function Get-SHA256Converter { + if($PSVersionTable.PSEdition -eq 'Core') { + [System.Security.Cryptography.SHA256]::Create() + } + else { + New-Object -TypeName Security.Cryptography.SHA256Managed + } +} + +function Get-WMIQuery { + param ($class) + + if(Get-Command -Name Get-CimInstance -ErrorAction SilentlyContinue) { + Get-CimInstance $class + } + else { + Get-WmiObject $class + } }