归档片段
Hash Verify
:::: code-group
::: code-group-item hash.ps1:active
function Test-FileHash {
param(
# FilePath
[string]$Path,
# Algorithm
[string]$Algorithm = "SHA256",
# Compare Hash
[string]$Hash
)
begin {
$file = Get-Item $Path
if ($Hash) { break }
$parent = $file.Directory
if ('hash.json' -notin ($parent.GetFiles().Name)) { break }
$fileList = Get-Content "hash.json" | ConvertFrom-Json
if ($file.Name -notin $fileList.FileName) { break }
$record = $fileList | Where-Object FileName -eq $file.Name
$Algorithm = $record.Algorithm
$Hash = $record.Hash
}
process {
$fileHash = Get-FileHash $Path -Algorithm $Algorithm
$result = @{
FileName = $Path
OriChecksum = $Hash
FileHash = $fileHash.Hash
Verify = $Hash.ToUpper() -eq $fileHash.Hash
}
}
end {
return $result
}
}
function Test-FilesHash {
$fileList = Get-Content "hash.json" | ConvertFrom-Json
$result = @()
$fileList | ForEach-Object {
if (Test-Path $_.FileName) {
$result += (Test-FileHash $_.FileName)
}
}
$result | Format-Table
}
Test-FilesHash
原创2022/4/1...小于 1 分钟