Last active
October 13, 2022 10:59
-
-
Save jessestricker/cab4da0992a836591d6246f9de6e6ddb to your computer and use it in GitHub Desktop.
Windows PowerShell Profile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Test-FileHash { | |
[CmdletBinding()] | |
param ( | |
[Parameter(Mandatory)] | |
[string] $Path, | |
[Parameter(Mandatory)] | |
[string] $Hash, | |
[ValidateSet("SHA1", "SHA256", "SHA512")] | |
[string] $Algorithm = "SHA256" | |
) | |
$ExpectedHash = $Hash.ToLowerInvariant() | |
$ActualHash = (Get-FileHash -Path $Path -Algorithm $Algorithm).Hash.ToLowerInvariant() | |
$Match = $ActualHash -ceq $ExpectedHash | |
$Result = [PSCustomObject]@{ | |
Path = $Path | |
Match = $Match | |
ExpectedHash = $ExpectedHash | |
ActualHash = $ActualHash | |
Algorithm = $Algorithm | |
} | |
Write-Output $Result | |
} | |
function Test-CheckSumFile { | |
[CmdletBinding()] | |
param ( | |
[Parameter(Mandatory)] | |
[string] $Path | |
) | |
$SUPPORTED_ALGORITHMS = "SHA1", "SHA256", "SHA512" | |
$Algorithm = [System.IO.Path]::GetExtension($Path).Substring(1).ToUpperInvariant() | |
if ($Algorithm -cnotin $SUPPORTED_ALGORITHMS) { | |
throw "The file extension does not match a supported algorithm." | |
} | |
$PathParent = Split-Path -Path $Path -Parent -Resolve | |
$Lines = Get-Content -Path $Path | |
foreach ($Line in $Lines) { | |
$Fields = -split $Line | |
$ExpectedHash = $Fields[0] | |
$FileName = $Fields[1] | |
if ($FileName.StartsWith([char] "*")) { | |
$FileName = $FileName.Substring(1) | |
} | |
try { | |
$FilePath = Join-Path -Path $PathParent -ChildPath $FileName -Resolve -ErrorAction Stop | |
} | |
catch { | |
Write-Warning "Can not get file path for file name '$FileName'" | |
continue | |
} | |
$Result = Test-FileHash -Path $FilePath -Hash $ExpectedHash -Algorithm $Algorithm | |
Write-Output $Result | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment