如何解决Win10 - 如何通过管道将一个命令的输出作为另一个命令的参数来检查文件哈希?
假设我需要根据网站提供的哈希值验证文件的 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 中的解决方案。
解决方法
如果您希望命令的输出充当运算符的操作数,例如 (...)
、{{3} }:
-eq
注意:grouping operator 指出 (certutil -hashfile .\amazon-corretto-11.0.10.9.1-windows-x64.msi md5)[1] -eq "website_hash"
输出多行行,感兴趣的散列位于第二行,{ {1}} 索引访问返回,基于 PowerShell 将来自外部程序的输出行作为字符串数组返回。
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
#>
,
根据@postanote 和@mklement0 的回答,这里有 2 个 powpershell one-liners 到非常文件哈希,都忽略大小写。
证书工具:
(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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。