PowerShell 文件和目录操作全解析
1. 计算文件哈希值
在处理文件时,有时需要计算文件的哈希值来验证文件的完整性。以下是一段用于计算文件哈希值的 PowerShell 代码:
$files += @($input | Foreach-Object { $_.FullName })
foreach($file in $files)
{
if(-not (Test-Path $file -Type Leaf)) { continue }
$filename = (Resolve-Path $file).Path
$inputStream = New-Object IO.StreamReader $filename
$hashBytes = $hasher.ComputeHash($inputStream.BaseStream)
$inputStream.Close()
$builder = New-Object System.Text.StringBuilder
$hashBytes | Foreach-Object { [void] $builder.Append($_.ToString("X2")) }
$output = New-Object PsObject -Property @{
Path = ([IO.Path]::GetFileName($file));
HashAlgorithm = $hashAlgorithm;
HashValue = $builder.ToString()
}
超级会员免费看
订阅专栏 解锁全文
1045

被折叠的 条评论
为什么被折叠?



