From 8fbb24a82ceb9c94d3fb220d0b94774ea7e88870 Mon Sep 17 00:00:00 2001 From: jdhitsolutions Date: Thu, 26 Jan 2017 13:02:00 -0500 Subject: [PATCH] v0.5.0.0 modified download to pull file hashes from summary and compare them to downloaded files. This is BREAKING CHANGE. Updated Get-PSReleaseAsset to include the SHA256 hash Updated help updated README --- Changelog.txt | 7 ++++ Docs/Save-PSReleaseAsset.md | 7 ++-- PSReleaseTools.psd1 | Bin 7348 -> 7348 bytes PSReleaseTools.psm1 | 57 +++++++++++++++++---------- README.md | 4 +- en-US/PSReleaseTools-help.xml | 72 +++++++++++++++++++++++++++++++--- 6 files changed, 116 insertions(+), 31 deletions(-) diff --git a/Changelog.txt b/Changelog.txt index 2c56c28..73d3942 100644 --- a/Changelog.txt +++ b/Changelog.txt @@ -1,5 +1,12 @@ #Changelog for PSReleaseTools +v0.5.0.0 +modified download to pull file hashes from summary +and compare them to downloaded files. This is BREAKING CHANGE. +Updated Get-PSReleaseAsset to include the SHA256 hash +Updated help +updated README + v0.4.0.2 fixed semantic versioning in the manifest diff --git a/Docs/Save-PSReleaseAsset.md b/Docs/Save-PSReleaseAsset.md index e7884d9..92f70ee 100644 --- a/Docs/Save-PSReleaseAsset.md +++ b/Docs/Save-PSReleaseAsset.md @@ -23,7 +23,7 @@ Save-PSReleaseAsset [[-Path] ] -Name [-Passthru] [-WhatIf] [- ### File ``` -Save-PSReleaseAsset [[-Path] ] -Filename -Size -URL [-Passthru] [-WhatIf] +Save-PSReleaseAsset [[-Path] ] -Filename -Hash -URL [-Passthru] [-WhatIf] [-Confirm] [] ``` @@ -119,9 +119,8 @@ Accept pipeline input: True (ByPropertyName) Accept wildcard characters: False ``` -### -Size -The target size for the download release. -If the actual download does not match this value you will get a warning. +### -Hash +The SHA256 file hash. If the actual download does not match this value you will get a warning. ```yaml Type: String diff --git a/PSReleaseTools.psd1 b/PSReleaseTools.psd1 index 31de84ad8d5cf70f967cfbdb4a144f0e099811b3..8e9166c4132ef23299367c1059c93b24f829412f 100644 GIT binary patch delta 22 bcmdmDxy5qBAx2J920aD?5Z-*6@ud_1Qg;T* delta 22 ccmdmDxy5qBAx2IU20aD?AU4{3n(?I+08(`Z%K!iX diff --git a/PSReleaseTools.psm1 b/PSReleaseTools.psm1 index 8edd4f5..330bff3 100644 --- a/PSReleaseTools.psm1 +++ b/PSReleaseTools.psm1 @@ -113,7 +113,7 @@ else { [Parameter(ParameterSetName="File",Mandatory,ValueFromPipelineByPropertyName)] [string]$Filename, [Parameter(ParameterSetName="File",Mandatory,ValueFromPipelineByPropertyName)] -[string]$Size, +[string]$Hash, [Parameter(ParameterSetName="File",Mandatory,ValueFromPipelineByPropertyName)] [string]$URL, [switch]$Passthru @@ -129,26 +129,28 @@ Begin { Write-Verbose "[BEGIN ] Starting: $($MyInvocation.Mycommand)" #display PSBoundparameters formatted nicely for Verbose output [string]$pb = ($PSBoundParameters | Format-Table -AutoSize | Out-String).TrimEnd() - Write-Verbose "[BEGIN ] PSBoundparameters: `n$($pb.split("`n").Foreach({"$("`t"*2)$_"}) | Out-String) `n" + Write-Verbose "[BEGIN ] PSBoundparameters: `n$($pb.split("`n").Foreach({"$("`t"*2)$_"}) | + Out-String) `n" $uri = "https://api.github.com/repos/powershell/powershell/releases/latest" #define an internal function to download the file Function DL { [cmdletbinding(SupportsShouldProcess)] - Param([string]$Source,[string]$Destination,[int32]$Size,[switch]$Passthru) + Param([string]$Source,[string]$Destination,[string]$hash,[switch]$Passthru) Write-Verbose "[DL] $Source to $Destination" if ($pscmdlet.ShouldProcess($Destination,"Downloading $source")) { Invoke-Webrequest -Uri $source -UseBasicParsing -DisableKeepAlive -OutFile $Destination - $f = get-item -Path $Destination - if ($f.Length -ne $size) { - Write-Warning "$Destination may be incomplete" + Write-Verbose "[DL] Comparing file hash to $hash" + $f = Get-FileHash -Path $Destination -Algorithm SHA256 + if ($f.hash -ne $hash) { + Write-Warning "Hash mismatch. $Destination may be incomplete" } if ($passthru) { - $f + get-item $Destination } } #should process } #DL @@ -168,13 +170,21 @@ Process { } if ($data.assets) { + + #parse out file names and hashes + [regex]$rx="(?[p|P]ower[s|S]hell[-|_]\d.*)\s+-\s+(?\w+)" + $r = $rx.Matches($data.body) + $r | foreach -Begin {$h=@{}} -process { + $h.add($_.groups["file"].value.trim(),$_.groups["hash"].value.trim()) + } + Switch ($PSCmdlet.ParameterSetName) { "All" { Write-Verbose "[PROCESS] Downloading all releases to $Path" foreach ($asset in $data.assets) { Write-Verbose "[PROCESS] ...$($Asset.name)" $target = Join-Path -Path $path -ChildPath $asset.Name - DL -source $asset.browser_download_url -Destination $Target -Size $asset.size -passthru:$passthru + DL -source $asset.browser_download_url -Destination $Target -hash $h.item($asset.name) -passthru:$passthru } } "Name" { @@ -182,14 +192,14 @@ Process { Foreach ($item in $name) { Switch ($item) { - "Win7-x86" { $assets = $data.assets.where({$_.name -match 'Win7-x86'})} - "Win7-x64" { $assets = $data.assets.where({$_.name -match 'Win7-x64'}) } - "Win81" { $assets = $data.assets.where({$_.name -match 'Win81'})} - "Win10" { $assets = $data.assets.where({$_.name -match 'Win10'}) } - "MacOS" { $assets = $data.assets.where({$_.name -match 'pkg'}) } - "Ubuntu14" { $assets = $data.assets.where({$_.name -match 'ubuntu.*14'}) } - "Ubuntu16" { $assets = $data.assets.where({$_.name -match 'ubuntu.*16'}) } - "CentOS" { $assets = $data.assets.where({$_.name -match 'centos'}) } + "Win7-x86" { $assets = $data.assets.where({$_.name -match 'Win7-x86'})} + "Win7-x64" { $assets = $data.assets.where({$_.name -match 'Win7-x64'}) } + "Win81" { $assets = $data.assets.where({$_.name -match 'Win81'})} + "Win10" { $assets = $data.assets.where({$_.name -match 'Win10'}) } + "MacOS" { $assets = $data.assets.where({$_.name -match 'pkg'}) } + "Ubuntu14" { $assets = $data.assets.where({$_.name -match 'ubuntu.*14'}) } + "Ubuntu16" { $assets = $data.assets.where({$_.name -match 'ubuntu.*16'}) } + "CentOS" { $assets = $data.assets.where({$_.name -match 'centos'}) } } #Switch @@ -202,14 +212,14 @@ Process { foreach ($asset in $Assets) { Write-Verbose "[PROCESS] ...$($asset.name)" $target = Join-Path -Path $path -ChildPath $asset.Name - DL -source $asset.browser_download_url -Destination $Target -Size $asset.size -passthru:$passthru + DL -source $asset.browser_download_url -Destination $Target -hash $h.item($asset.name) -passthru:$passthru } #foreach asset } #foreach name } "File" { Write-Verbose "[PROCESS] ...$($filename)" $target = Join-Path -Path $path -ChildPath $fileName - DL -source $url -Destination $Target -Size $size -passthru:$passthru + DL -source $url -Destination $Target -hash $h.item($filename) -passthru:$passthru } } #switch parameter set name } else { @@ -242,6 +252,13 @@ Process { Try { $data = Invoke-Restmethod -uri $uri -Method Get -ErrorAction Stop + #parse out file names and hashes + [regex]$rx="(?[p|P]ower[s|S]hell[-|_]\d.*)\s+-\s+(?\w+)" + $r = $rx.Matches($data.body) + $r | foreach -Begin {$h=@{}} -process { + $h.add($_.groups["file"].value.trim(),$_.groups["hash"].value.trim()) + } + Write-Verbose "[PROCESS] Found $($data.assets.count) downloads" $assets = $data.assets | Select @{Name="FileName";Expression={$_.Name}}, @{Name="Family";Expression={ @@ -255,7 +272,7 @@ Process { @{Name="Format";Expression={ $_.name.split(".")[-1] }}, - Size, + @{Name="Hash";Expression = {$h.item($_.name)}}, @{Name="Created";Expression={$_.Created_at -as [datetime]}}, @{Name="Updated";Expression={$_.Updated_at -as [datetime]}}, @{Name="URL";Expression={$_.browser_download_Url}}, @@ -267,7 +284,7 @@ Process { else { $assets } - } #Trye + } #Try catch { Throw $_ } diff --git a/README.md b/README.md index 983c055..876dc84 100644 --- a/README.md +++ b/README.md @@ -41,7 +41,7 @@ I also realized you might run `Get-PSReleaseAsset`, perhaps to examine details b image -By the way, I'm using the file size to test if the file downloaded completely. I know there are hashes in the release page but I didn't feel like trying to parse them out and I can't find them anywhere else. Using the file size seems to work just fine. +The current version of this module uses regular expression named captures to pull out the file name and corresponding SHA256 hashes. The save command then calls `Get-FileHash` to get the current hash and compares them. ## PowerShell Gallery This module has been published to the PowerShell Gallery. You should be able to run these commands to find and install it. @@ -61,4 +61,4 @@ NOT UNDERSTAND WHAT THIS CODE DOES OR HOW IT WORKS, DO NOT USE OUTSIDE OF A SECURE, TEST SETTING. **************************************************************** -*Last updated 12 January 2017* +*Last updated 26 January 2017* diff --git a/en-US/PSReleaseTools-help.xml b/en-US/PSReleaseTools-help.xml index 578ecfe..994be54 100644 --- a/en-US/PSReleaseTools-help.xml +++ b/en-US/PSReleaseTools-help.xml @@ -101,6 +101,65 @@ in81-x64.zip". +Get-PSReleaseCurrent +Get +PSReleaseCurrent +Get the current PowerShell v6 release + + + +This command will query the GitHub repository for the latest release and write an object to the pipeline. + +If you are running v6 the LocalVersion property will reflect the GitCommitID so you can accurately compare and determine if you need to update. + + +Get-PSReleaseCurrent + + + +None + + + + + + +System.Object + + + + + + +Learn more about PowerShell: http://jdhitsolutions.com/blog/essential-powershell-resources/ + + + +Example 1 +PS C:\> Get-PSReleaseCurrent +Name Version Released LocalVersion +---- ------- -------- ------------ +v6.0.0-alpha.14 release of PowerShell v6.0.0-alpha.14 12/15/2016 2:51:53 PM 5.0.10586.117 +This gets the current release from a Windows platform. + + + +Example 2 +PS C:\> Get-PSReleaseCurrent +Name Version Released LocalVersion +---- ------- -------- ------------ +v6.0.0-alpha.14 release of PowerShell v6.0.0-alpha.14 12/15/2016 2:51:53 PM v6.0.0-alpha.9 +This gets the current release from an ubuntu platform. + + + + +Get-PSReleaseSummary + + + + + Get-PSReleaseSummary Get PSReleaseSummary @@ -209,7 +268,10 @@ PowerShell_6.0.0.14-alpha.14-win81-x64.msi 12/14/2016 8:48:23 PM 4 -Invoke-Restmethod +Get-PSReleaseCurrent + + +Invoke-Restmethod @@ -333,8 +395,8 @@ PowerShell_6.0.0.14-alpha.14-win81-x64.msi 12/14/2016 8:48:23 PM 4 None -Size -The target size for the download release. If the actual download does not match this value you will get a warning. +Hash +The SHA256 file hash. If the actual download does not match this value you will get a warning. String @@ -413,8 +475,8 @@ PowerShell_6.0.0.14-alpha.14-win81-x64.msi 12/14/2016 8:48:23 PM 4 None -Size -The target size for the download release. If the actual download does not match this value you will get a warning. +Hash +The SHA256 file hash. If the actual download does not match this value you will get a warning. String