从批处理文件运行时,powershell中的fet-filehash SHA256加密在最后4位



当我从powershell运行get-filehash时,它可以工作:

当我从批处理文件运行它时,它会删除最后4位数字。

powershell.exe -NoProfile -ExecutionPolicy -Command "Get-FileHash filename.edi -Algorithm SHA256 | Out-File c:apptestfileout.txt"
Algorithm       Hash                                                           
---------       ----                                                           
SHA256          3EFEC59BFB0573061C5CD2F72A684663B60CA3D0D91C67CBDBBE366A59FE...

如何使输出文件仅为:

3EFEC59BFB0573061C5CD2F72A684663B60CA3D0D91C67CBDBBE366A59FE4A8F

当我运行时,它给了我完整的powershell内部哈希:

Get-FileHash filename.edi -Algorithm SHA256 | Out-File c:apptestfileout.txt

我只需要没有标头的哈希以及最后的4位数字。

有人有一个已经执行此操作的脚本吗?

它返回一个对象,因此您需要做的就是将select-object与ExpandProperty:

使用:
powershell.exe -NoProfile -ExecutionPolicy -Command "Get-FileHash filename.edi -Algorithm SHA256 | Select-Object -ExpandProperty Hash | Out-File c:apptestfileout.txt"

或通过点符号引用对象属性:

powershell.exe -NoProfile -ExecutionPolicy -Command "(Get-FileHash filename.edi -Algorithm SHA256).Hash | Out-File c:apptestfileout.txt"

使用纯CMD,您可以这样做:

cmd /V /C for /F "tokens=*" %H in ('certutil -hashfile "filename.edi" SHA256 ^^^| find /V ":"') do @(set "HASH=%H" ^& echo !HASH: =!) > "C:apptestfileout.txt"

在批处理中,看起来像这样:

setlocal EnableDelayedExpansion
for /F "tokens=*" %%H in ('
    certutil -hashfile "filename.edi" SHA256 ^| find /V ":"
') do @(
    set "HASH=%%H" & echo !HASH: =!
) > "C:apptestfileout.txt"
endlocal

最新更新