简体   繁体   English

Win10 - 如何从一个命令中将 pipe output 作为另一个命令的参数来检查文件 hash?

[英]Win10 - How to pipe the output from one command as an argument of another to check file hash?

Lets say that I need to verify the md5 hash of a file against the hash provided by the website.假设我需要根据网站提供的 hash 验证文件的 md5 hash。

I use certutil -hashfile.\amazon-corretto-11.0.10.9.1-windows-x64.msi md5 or Get-FileHash -Path.\amazon-corretto-11.0.10.9.1-windows-x64.msi -Algorithm md5 in powershell to get the hash, then I look at the hash from the download website, and compare them letter by letter.我使用certutil -hashfile.\amazon-corretto-11.0.10.9.1-windows-x64.msi md5Get-FileHash -Path.\amazon-corretto-11.0.10.9.1-windows-x64.msi -Algorithm md5 powershell 得到 hash,然后我从下载网站上查看 hash,逐字比较。 Is there a way to let the command line do the comparison?有没有办法让命令行进行比较?

I tried certutil -hashfile.\amazon-corretto-11.0.10.9.1-windows-x64.msi md5 | echo == "website_hash"我试过certutil -hashfile.\amazon-corretto-11.0.10.9.1-windows-x64.msi md5 | echo == "website_hash" certutil -hashfile.\amazon-corretto-11.0.10.9.1-windows-x64.msi md5 | echo == "website_hash" , but it didn't work. certutil -hashfile.\amazon-corretto-11.0.10.9.1-windows-x64.msi md5 | echo == "website_hash" ,但它没有用。

Solutions in command line and powershell are both welcomed.欢迎使用命令行和 powershell 解决方案。

You can simply enclose a command in (...) , the grouping operator , if you want its output to act as an operand to an operator, such as -eq , the equality operator :如果您希望其output充当运算符的操作数,例如-eq相等运算符),您可以简单地将命令包含在(...)中:

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

Note: postanote points out that certutil outputs mutiple lines, with the hash of interest being on the second line, which the [1] index access returns, based on PowerShell returning the output lines from external programs as an array of strings .注意: postanote指出certutil输出行,感兴趣的 hash 在第二行, [1]索引访问返回,基于 PowerShell 从外部程序返回 Z78E6221F6393D1356681DB398字符串数组1 行的 Z78E6221F6393D1356681DB398。

Continuing from my comment . 继续我的评论 Try this:尝试这个:

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

Tagging on to this as per mklement0's helpful response to you and your follow-up query.根据 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
#>

Based on @postanote and @mklement0's answers, here are 2 powpershell one-liners to very file hash, both ignores cases.根据@postanote 和@mklement0 的回答,这里有2 个用于非常归档hash 的powerpershell 单行代码,它们都忽略了案例。

certutil:证书实用程序:

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

Get-FileHash:获取文件哈希:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 win10 命令提示符下使用 ffmpeg 连接来自不同子目录的视频文件? - How to concatenate video files from different sub directories using ffmpeg in win10 command prompt? 如何使用Powershell将参数从另一个函数的管道输出传递给函数 - How to pass argument to a function from a pipe output of another using powershell 在 Win10 上从 PowerShell 脚本内部打印 PNG 文件 - Printing a PNG-file from inside a PowerShell script on Win10 如何在不使用大内存缓冲区的情况下将一个命令的输出传递到另一个命令? - How can I pipe the output of one command into another without using a large memory buffer? 为什么从我从 win7 移到 win10 那天,powershell 代码将文件转换为 base64 所花费的时间从 10-50 秒不等? - Why is the time taken by powershell code to convert a file to base64 varying from 10-50s from the day I moved from win7 to win10? 有没有一种方法可以组合所有powershell命令来删除win10 bloatware,而不是一个一个地做呢? - Is there a way to combine all powershell commands to remove win10 bloatware rather than doing it one by one? 如何使用Powershell在win7和win10上找到可用的压缩工具以及zip文件 - how can I find the usable zipping tool on win7 and win10 and zip files using Powershell 在Win10上显示地理位置的输入 - Show inputs for Geolocation on Win10 可变路径PowerShell-Win10 - Variable path PowerShell - Win10 如何在没有PowerShell更改编码的情况下将命令输出传递给文件? - How to pipe the output of a command to a file without powershell changing the encoding?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM