将输出从 DIR 传输到 CERTUTIL

将输出从 DIR 传输到 CERTUTIL

在 Win10 中,寻找在命令提示符或批处理文件中通过管道传输 DIR 命令的输出以作为 CERTUTIL 命令的输入的方法。换句话说,我想获取与 DIR 命令匹配的所有文件的 MD5 哈希值。

以下命令会生成电子邮箱:\Temp文件夹:

C:\Users\RAS>dir "E:\Temp" /b

但是当我按照以下命令所示进行管道传输时,我收到一条错误消息:

C:\Users\RAS>dir "E:\Temp" /b | CertUtil -hashfile %~f1 MD5
CertUtil: -hashfile command FAILED: 0x80070002 (WIN32: 2 ERROR_FILE_NOT_FOUND)
CertUtil: The system cannot find the file specified.

用户1686 的回答Windows Vista 将 dir 输出管道传输到 attrib 命令表示某些命令(如 ATTRIB)不接受文件名作为输入,但我认为这与此无关。

Cliff Armstrong 的回答来自获取字符串的哈希值并最终将其与哈希值进行比较看起来很有希望,但我不知道如何将每个完全合格的文件名传递给 CERTUTIL。此外,我没有使用 PowerShell 的经验,如果可能的话,我想使用批处理文件来获得解决方案。

谢谢你,

德国之声

答案1

考虑一下Dir不会将自己的输出放在正确的参数中以供使用CertUtil,也不CertUtil会将Dir重定向的输入放在正确的位置(正确的位置/参数顺序)以供使用它。

如果您尝试使用for /f带有where命令的循环,而不是dir /b(它不会产生完整的文件路径),这将导致完整的文件路径,并且您可以在中使用输出循环变量Certutil

  • 在命令行中:
for /f tokens^=* %i in ('%__APPDIR__%where.exe "E:\Temp:*.*"')do @%__APPDIR__%CertUtil.exe -hashfile "%~i" MD5

rem :: Output  :: 

MD5 hash of E:\Temp\Turn Off LCD.exe:
3657b64bfa767cd1ce1ea3709053ea3b
CertUtil: -hashfile command completed successfully.
MD5 hash of F:\LngVar.exe:
fc82a6b8fa5c24f6cbcb0f0dcbf85a2e
CertUtil: -hashfile command completed successfully.
MD5 hash of F:\cocolor.exe:
d9a3def8f569afda41fb6e067c5f3df3
CertUtil: -hashfile command completed successfully.
MD5 hash of F:\aria2c.exe:
80f598187166a8f95d86985ba0244257
CertUtil: -hashfile command completed successfully.
MD5 hash of F:\cmdFocus.exe:
f90f8672fa57ba4e8f0a05dec3ede654
CertUtil: -hashfile command completed successfully.
MD5 hash of F:\where.exe:
7b6f5b80b4db4ca0c0472625bcd0c981
CertUtil: -hashfile command completed successfully.
MD5 hash of F:\SaveColor.exe:
0b24aa776ca4601bb39e6e529e73e7a6
CertUtil: -hashfile command completed successfully.
MD5 hash of F:\Windows-ISO-Downloader.exe:
11532e016f68ef22ca96fa03020de789
CertUtil: -hashfile command completed successfully.
MD5 hash of F:\sudoku.exe:
a3a946be19763b72f8aab6387079207a
CertUtil: -hashfile command completed successfully.
  • bat/cmd文件中也一样:
@echo off  

for /f tokens^=* %%i in ('%__APPDIR__%where.exe "E:\Temp:*.*
')do %__APPDIR__%CertUtil.exe -hashfile "%%~i" MD5"

rem :: Output :: 
MD5 hash of E:\Temp\Turn Off LCD.exe:
3657b64bfa767cd1ce1ea3709053ea3b
CertUtil: -hashfile command completed successfully.
MD5 hash of F:\LngVar.exe:
fc82a6b8fa5c24f6cbcb0f0dcbf85a2e
CertUtil: -hashfile command completed successfully.
MD5 hash of F:\cocolor.exe:
d9a3def8f569afda41fb6e067c5f3df3
CertUtil: -hashfile command completed successfully.
MD5 hash of F:\aria2c.exe:
80f598187166a8f95d86985ba0244257
CertUtil: -hashfile command completed successfully.
MD5 hash of F:\cmdFocus.exe:
f90f8672fa57ba4e8f0a05dec3ede654
CertUtil: -hashfile command completed successfully.
MD5 hash of F:\where.exe:
7b6f5b80b4db4ca0c0472625bcd0c981
CertUtil: -hashfile command completed successfully.
MD5 hash of F:\SaveColor.exe:
0b24aa776ca4601bb39e6e529e73e7a6
CertUtil: -hashfile command completed successfully.
MD5 hash of F:\Windows-ISO-Downloader.exe:
11532e016f68ef22ca96fa03020de789
CertUtil: -hashfile command completed successfully.
MD5 hash of F:\sudoku.exe:
a3a946be19763b72f8aab6387079207a
CertUtil: -hashfile command completed successfully.

如果只需要输出中的 MD5 字符串,请添加|find/v ":"

  • 在命令行中:
@for /f tokens^=* %i in ('%__APPDIR__%where.exe "E:\Temp:*.*"')do @%__APPDIR__%CertUtil.exe -hashfile "%~i" MD5|find/v ":"

rem :: Output  :: 

3657b64bfa767cd1ce1ea3709053ea3b
fc82a6b8fa5c24f6cbcb0f0dcbf85a2e
d9a3def8f569afda41fb6e067c5f3df3
80f598187166a8f95d86985ba0244257
f90f8672fa57ba4e8f0a05dec3ede654
7b6f5b80b4db4ca0c0472625bcd0c981
0b24aa776ca4601bb39e6e529e73e7a6
11532e016f68ef22ca96fa03020de789
a3a946be19763b72f8aab6387079207a
  • 在 bat/cmd 文件中:
@echo off 

for /f tokens^=* %%i in ('%__APPDIR__%where.exe "E:\Temp:*.*"
')do %__APPDIR__%CertUtil.exe -hashfile "%%~i" MD5|find/v ":"

rem :: Output  :: 

3657b64bfa767cd1ce1ea3709053ea3b
fc82a6b8fa5c24f6cbcb0f0dcbf85a2e
d9a3def8f569afda41fb6e067c5f3df3
80f598187166a8f95d86985ba0244257
f90f8672fa57ba4e8f0a05dec3ede654
7b6f5b80b4db4ca0c0472625bcd0c981
0b24aa776ca4601bb39e6e529e73e7a6
11532e016f68ef22ca96fa03020de789
a3a946be19763b72f8aab6387079207a

您还可以使用 For / r 以更简单和递归的方式执行此操作,用法与类似链接问题:

  • 在命令行中:
@for /r E:\temp %i in (*)do @%__APPDIR__%CertUtil.exe -hashfile "%~i" MD5|find/v ":"
  • 在 bat/cmd 文件中:
@echo off 

for /r E:\temp %%i in (*)do %__APPDIR__%CertUtil.exe -hashfile "%%~i" MD5|find/v ":"

答案2

您曾表示,

我想获取所有匹配的文件的 MD5 哈希值 [...]

我也尝试匹配哈希值,但也收到了错误

CertUtil:-hashfile 命令失败:0x80070002(WIN32:2 ERROR_FILE_NOT_FOUND)

我找到的解决方案是下载一个哈希检查器应用程序,它显然效果很好。它从文档中提取哈希值,将其与我提供的哈希值进行比较,发现它们匹配。

我看见一些这样的应用程序在 Microsoft App store 中。我得到的版本有几种哈希类型选项,包括 SHA256 和 MD5。

相关内容