如何在 PowerShell 中检查在线文件的文件哈希?

How do I check the filehash of a file thats online in PowerShell?

好吧,我正在向 Chris Titus Tech 的 Ultimate Windows 工具包发出拉取请求,我想做一些检查它是否已更新的东西。但是当我尝试 运行:

Get-FileHash -Algorithm SHA256 https://raw.githubusercontent.com/fgclue/etcher/master/desktop.ini

它只是说:

Get-FileHash: Cannot find drive. A drive with the name 'https' does not exist. And I want to make it look something like this:

$hash = Get-FileHash -Algorithm SHA256 URL
$chash = Get-FileHash -Algorithm SHA256 win10debloat.ps1

if ($hash -ne $chash){
     Write-Host "There is an update!"
     Write-Host "Update?"
     $Opt = Read-Host "Choice"     
     if (Opt -ne y){
          exit
     }
     if (Opt -ne yn){
          Start-Process "https://github.com/ChrisTitusTech/win10script/"
          Write-Host Please download the new version and close this windows.
     }
}

我不完全确定你到底想比较什么,但这里是你如何在不下载任何东西到你的磁盘的情况下测试你拥有的东西是否是最新的,在这种情况下我相信你需要使用 IO.MemoryStream 获取远程文件的哈希值。

$uri = 'https://raw.githubusercontent.com/fgclue/etcher/master/desktop.ini'
$theHashIHave = Get-FileHash myfilehere.ext -Algorithm SHA256

try {
    $content = Invoke-RestMethod $uri
    $memstream = [System.IO.MemoryStream]::new($content.ToCharArray())
    $thisFileHash = Get-FileHash -InputStream $memstream -Algorithm SHA256
    if($theHashIhave.Hash -eq $thisFileHash.Hash) {
        "all good"
    }
    else {
        "should update here"
    }
}
finally {
    $memstream.foreach('Dispose')
}

如果您需要使用特定的编码来获取哈希值,例如UTF8 Encoding with a BOM,您可以使用以下方法:

$memstream = [System.IO.MemoryStream]::new(
    [System.Text.Encoding]::UTF8.GetBytes($content)
)

然而,通过这种方式您需要确保您使用的编码是正确的,即:如果您需要 UTF8 无 BOM,请改用 [System.Text.UTF8Encoding]::new().GetBytes(...),等等.