Skip to content

Commit

Permalink
v0.5.0.0
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jdhitsolutions committed Jan 26, 2017
1 parent cef403b commit 8fbb24a
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 31 deletions.
7 changes: 7 additions & 0 deletions Changelog.txt
Original file line number Diff line number Diff line change
@@ -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

Expand Down
7 changes: 3 additions & 4 deletions Docs/Save-PSReleaseAsset.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Save-PSReleaseAsset [[-Path] <String>] -Name <String[]> [-Passthru] [-WhatIf] [-

### File
```
Save-PSReleaseAsset [[-Path] <String>] -Filename <String> -Size <String> -URL <String> [-Passthru] [-WhatIf]
Save-PSReleaseAsset [[-Path] <String>] -Filename <String> -Hash <String> -URL <String> [-Passthru] [-WhatIf]
[-Confirm] [<CommonParameters>]
```

Expand Down Expand Up @@ -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
Expand Down
Binary file modified PSReleaseTools.psd1
Binary file not shown.
57 changes: 37 additions & 20 deletions PSReleaseTools.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -168,28 +170,36 @@ Process {
}

if ($data.assets) {

#parse out file names and hashes
[regex]$rx="(?<file>[p|P]ower[s|S]hell[-|_]\d.*)\s+-\s+(?<hash>\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" {
#download individual release files
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


Expand All @@ -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 {
Expand Down Expand Up @@ -242,6 +252,13 @@ Process {
Try {
$data = Invoke-Restmethod -uri $uri -Method Get -ErrorAction Stop

#parse out file names and hashes
[regex]$rx="(?<file>[p|P]ower[s|S]hell[-|_]\d.*)\s+-\s+(?<hash>\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={
Expand All @@ -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}},
Expand All @@ -267,7 +284,7 @@ Process {
else {
$assets
}
} #Trye
} #Try
catch {
Throw $_
}
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ I also realized you might run `Get-PSReleaseAsset`, perhaps to examine details b

<a href="http://jdhitsolutions.com/blog/wp-content/uploads/2017/01/image-5.png"><img style="background-image: none; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border: 0px;" title="image" src="http://jdhitsolutions.com/blog/wp-content/uploads/2017/01/image_thumb-5.png" alt="image" width="644" height="251" border="0" /></a>

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.
Expand All @@ -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*
72 changes: 67 additions & 5 deletions en-US/PSReleaseTools-help.xml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,65 @@ in81-x64.zip".</dev:code>
</command:relatedLinks>
</command:command>
<command:command xmlns:maml="http://schemas.microsoft.com/maml/2004/10" xmlns:command="http://schemas.microsoft.com/maml/dev/command/2004/10" xmlns:dev="http://schemas.microsoft.com/maml/dev/2004/10" xmlns:MSHelp="http://msdn.microsoft.com/mshelp">
<command:details><command:name>Get-PSReleaseCurrent</command:name>
<command:verb>Get</command:verb>
<command:noun>PSReleaseCurrent</command:noun>
<maml:description><maml:para>Get the current PowerShell v6 release
</maml:para>
</maml:description>
</command:details>
<maml:description><maml:para>This command will query the GitHub repository for the latest release and write an object to the pipeline.
</maml:para>
<maml:para>If you are running v6 the LocalVersion property will reflect the GitCommitID so you can accurately compare and determine if you need to update.
</maml:para>
</maml:description>
<command:syntax><command:syntaxItem><maml:name>Get-PSReleaseCurrent</maml:name>
</command:syntaxItem>
</command:syntax>
<command:parameters></command:parameters>
<command:inputTypes><command:inputType><dev:type><maml:name>None</maml:name>
</dev:type>
<maml:description><maml:para>
</maml:para>
</maml:description>
</command:inputType>
</command:inputTypes>
<command:returnValues><command:returnValue><dev:type><maml:name>System.Object</maml:name>
</dev:type>
<maml:description><maml:para>
</maml:para>
</maml:description>
</command:returnValue>
</command:returnValues>
<maml:alertSet><maml:alert><maml:para>Learn more about PowerShell: http://jdhitsolutions.com/blog/essential-powershell-resources/
</maml:para>
</maml:alert>
</maml:alertSet>
<command:examples><command:example><maml:title>Example 1</maml:title>
<dev:code>PS C:\&gt; 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</dev:code>
<dev:remarks><maml:para>This gets the current release from a Windows platform.
</maml:para>
</dev:remarks>
</command:example>
<command:example><maml:title>Example 2</maml:title>
<dev:code>PS C:\&gt; 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</dev:code>
<dev:remarks><maml:para>This gets the current release from an ubuntu platform.
</maml:para>
</dev:remarks>
</command:example>
</command:examples>
<command:relatedLinks><maml:navigationLink><maml:linkText>Get-PSReleaseSummary</maml:linkText>
<maml:uri></maml:uri>
</maml:navigationLink>
</command:relatedLinks>
</command:command>
<command:command xmlns:maml="http://schemas.microsoft.com/maml/2004/10" xmlns:command="http://schemas.microsoft.com/maml/dev/command/2004/10" xmlns:dev="http://schemas.microsoft.com/maml/dev/2004/10" xmlns:MSHelp="http://msdn.microsoft.com/mshelp">
<command:details><command:name>Get-PSReleaseSummary</command:name>
<command:verb>Get</command:verb>
<command:noun>PSReleaseSummary</command:noun>
Expand Down Expand Up @@ -209,7 +268,10 @@ PowerShell_6.0.0.14-alpha.14-win81-x64.msi 12/14/2016 8:48:23 PM 4
</dev:remarks>
</command:example>
</command:examples>
<command:relatedLinks><maml:navigationLink><maml:linkText>Invoke-Restmethod</maml:linkText>
<command:relatedLinks><maml:navigationLink><maml:linkText>Get-PSReleaseCurrent</maml:linkText>
<maml:uri></maml:uri>
</maml:navigationLink>
<maml:navigationLink><maml:linkText>Invoke-Restmethod</maml:linkText>
<maml:uri></maml:uri>
</maml:navigationLink>
</command:relatedLinks>
Expand Down Expand Up @@ -333,8 +395,8 @@ PowerShell_6.0.0.14-alpha.14-win81-x64.msi 12/14/2016 8:48:23 PM 4
<maml:uri /></dev:type>
<dev:defaultValue>None</dev:defaultValue>
</command:parameter>
<command:parameter required="true" variableLength="true" globbing="false" pipelineInput="True (ByPropertyName)" position="named" aliases="none"><maml:name>Size</maml:name>
<maml:Description><maml:para>The target size for the download release. If the actual download does not match this value you will get a warning.
<command:parameter required="true" variableLength="true" globbing="false" pipelineInput="True (ByPropertyName)" position="named" aliases="none"><maml:name>Hash</maml:name>
<maml:Description><maml:para>The SHA256 file hash. If the actual download does not match this value you will get a warning.
</maml:para>
</maml:Description>
<command:parameterValue required="true" variableLength="false">String</command:parameterValue>
Expand Down Expand Up @@ -413,8 +475,8 @@ PowerShell_6.0.0.14-alpha.14-win81-x64.msi 12/14/2016 8:48:23 PM 4
<maml:uri /></dev:type>
<dev:defaultValue>None</dev:defaultValue>
</command:parameter>
<command:parameter required="true" variableLength="true" globbing="false" pipelineInput="True (ByPropertyName)" position="named" aliases="none"><maml:name>Size</maml:name>
<maml:Description><maml:para>The target size for the download release. If the actual download does not match this value you will get a warning.
<command:parameter required="true" variableLength="true" globbing="false" pipelineInput="True (ByPropertyName)" position="named" aliases="none"><maml:name>Hash</maml:name>
<maml:Description><maml:para>The SHA256 file hash. If the actual download does not match this value you will get a warning.
</maml:para>
</maml:Description>
<command:parameterValue required="true" variableLength="false">String</command:parameterValue>
Expand Down

0 comments on commit 8fbb24a

Please sign in to comment.