Skip to content

Instantly share code, notes, and snippets.

@mcc85s
Last active November 10, 2022 15:08
Show Gist options
  • Save mcc85s/5a8f25313ba0cb7e622e0aaab5d27ce5 to your computer and use it in GitHub Desktop.
Save mcc85s/5a8f25313ba0cb7e622e0aaab5d27ce5 to your computer and use it in GitHub Desktop.
For downloading any file, assuming a default location, and immediately obtaining and displaying the SHA256 output of said file once it is downloaded
Function Gather-Download
{
[CmdLetBinding()]Param(
[ValidateNotNullOrEmpty()]
[Parameter(Position=0,Mandatory)][String]$URL,
[Parameter(Position=1)][String]$Path="$Home\Downloads",
[Parameter(Position=2)][String]$Info=$URL,
[Parameter(Position=3)][Switch]$Hash)
Import-Module BitsTransfer
$Current = [Net.ServicePointManager]::SecurityProtocol
[Net.ServicePointManager]::SecurityProtocol = 3072
$File = @{
Source = $URL
Destination = "$Path\{0}" -f $URL.Split('/')[-1]|
Description = $Info
}
Start-BitsTransfer @File
$File | ? JobState -match "(Transferring|Connecting)" | % { Start-Sleep -M 150 }
Switch ($File.JobState)
{
Transferred
{
Complete-BitsTransfer -BitsJob $File
}
Error
{
$File | Format-List
}
}
[Net.ServicePointManager]::SecurityProtocol = $Current
Return [Ordered]@{
Item = $File.Description
URL = $File.Source
Save = $File.Destination
Hash = If ($Hash) { Get-FileHash $File.Destination -Algorithm SHA256 | % Hash } Else { $Null }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment