【问题标题】:Win10 - How to pipe the output from one command as an argument of another to check file hash?Win10 - 如何将一个命令的输出作为另一个命令的参数来检查文件哈希?
【发布时间】:2021-02-08 03:14:06
【问题描述】:

假设我需要根据网站提供的哈希验证文件的 md5 哈希。

我在 powershell 中使用certutil -hashfile .\amazon-corretto-11.0.10.9.1-windows-x64.msi md5 或Get-FileHash -Path .\amazon-corretto-11.0.10.9.1-windows-x64.msi -Algorithm md5 来获取哈希,然后我从下载网站查看哈希,并逐个字母进行比较。有没有办法让命令行做比较?

我试过certutil -hashfile .\amazon-corretto-11.0.10.9.1-windows-x64.msi md5 | echo == "website_hash",但没用。

欢迎使用命令行和 powershell 中的解决方案。

【问题讨论】:

  • 比较哈希值是很常见的事情,网络上有很多样本。您正在混合命令行 (cmd.exe) 并尝试使用 PowerShell PipeLine。您正在做的事情不起作用,因为如果您在 PowerShell 实例中执行此操作,则该命令是错误的。管道`'|'` 符号在cmd.exe 和powershell.exe 中表示不同的含义。当您从 PowerShell 调用可执行文件时,可执行文件处于控制之中。使用 Get-help -Name Get-FileHash -Full 和 Compare-Object 的 Powershell 帮助文件; PowerShell 中的用例。
  • 请不要编辑您的问题以包含答案。您可以将问题的答案作为单独的帖子发布。

标签: powershell hash compare command-line-tool


【解决方案1】:

如果您希望其输出充当运算符的操作数,例如-eq、equality operator,您可以简单地将命令包含在(...)、grouping operator 中:

(certutil -hashfile .\amazon-corretto-11.0.10.9.1-windows-x64.msi md5)[1] -eq "website_hash"

注意:postanote 指出 certutil 输出 多个 行,感兴趣的哈希在 second 行,[1] 索引访问返回,基于 PowerShell 将外部程序的输出行作为字符串数组返回。

【讨论】:

  • 谢谢,@postanote(我目前无权访问certutil)-答案已相应更新。
  • @fall: 至于为什么你没有看到输出:使用 array 作为 LHS,-eq 充当 filter,即返回匹配项的子数组而不是单个$true或$false值。没有接收到输出意味着您的 RHS 操作数不匹配 LHS 数组的任何元素,这反过来意味着哈希不匹配。
【解决方案2】:

Continuing from my comment。试试这个:

Get-FileHash -Path 'D:\temp\book1.txt' -Algorithm MD5 | 
Format-Table -AutoSize
# Results
<#
Algorithm Hash                             Path             
--------- ----                             ----             
MD5       D572724F26BD600773F708AB264BE45B D:\temp\book1.txt
#>


$CompareObjectSplat = @{
    DifferenceObject = (Get-FileHash -Path 'D:\temp\book1.txt' -Algorithm MD5).Hash
    ReferenceObject  = (Get-FileHash -Path 'D:\temp\book1.txt' -Algorithm MD5).Hash
}
Compare-Object @compareObjectSplat -IncludeEqual

# Results
<#
InputObject                      SideIndicator
-----------                      -------------
D572724F26BD600773F708AB264BE45B == 
#>

# Get specifics for a module, cmdlet, or function
(Get-Command -Name Get-FileHash).Parameters
(Get-Command -Name Get-FileHash).Parameters.Keys
Get-help -Name Get-FileHash -Examples
Get-help -Name Get-FileHash -Full
Get-help -Name Get-FileHash -Online


(Get-Command -Name Compare-Object).Parameters
(Get-Command -Name Compare-Object).Parameters.Keys
Get-help -Name Compare-Object -Examples
Get-help -Name Compare-Object -Full
Get-help -Name Compare-Object -Online

根据 mklement0 对您的有用回复和您的后续查询对此进行标记。

# Command line - executable
(certutil -hashfile 'D:\temp\book1.txt' md5)
# Results
<#
MD5 hash of D:\temp\book1.txt:
d572724f26bd600773f708ab264be45b
CertUtil: -hashfile command completed successfully.
#>

(certutil -hashfile 'D:\temp\book1.txt' md5)[1] -eq (certutil -hashfile 'D:\temp\book1.txt' md5)[1]
# Results
<#
True
#>

【讨论】:

    【解决方案3】:

    根据 @postanote 和 @mklement0 的回答,这里有 2 个用于非常文件哈希的 powpershell 单行代码,均忽略大小写。

    证书实用程序:

    (certutil -hashfile ./amazon-corretto-11.0.10.9.1-windows-x64.msi MD5)[1] -eq ("EE569A19016F233B2050CC11219EBBFD")
    

    获取文件哈希:

    Compare-Object -ReferenceObject (Get-FileHash -Path '.\amazon-corretto-11.0.10.9.1-windows-x64.msi' -Algorithm MD5).Hash -DifferenceObject ("EE569A19016F233B2050CC11219EBBFD") -IncludeEqual
    

    【讨论】:

      猜你喜欢
      • 2012-10-25
      • 2019-08-26
      • 1970-01-01
      • 2016-01-30
      • 1970-01-01
      • 2021-09-11
      • 2013-11-08
      • 2015-08-18
      相关资源
      最近更新 更多