About Powershell
原创2022/4/1...小于 1 分钟
About Powershell
归档片段
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:::
::: code-group-item hash.json
[
{
"FileName": "manjaro-xfce-19.0.2-200311-linux54.iso",
"Algorithm": "SHA1",
"Hash": "7483e7911a8a2b36b70b507342bb7458ad9ca0d3"
}
]:::
::::