无法从 get-filehash 获取输出 [英] Unable to get output from get-filehash

查看:23
本文介绍了无法从 get-filehash 获取输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种可靠的命令行方法来获取 Windows 中文件的 SHA256 哈希值.我的理解是,这样做的方法是在 PowerShell 下通过 Microsoft 的 Get-FileHash cmdlet.我看过几个带有示例的网站,并查看了 Microsoft 自己的文档.以下语法似乎适用于 Windows Server 2012:

I am looking for a reliable command-line method of getting SHA256 hashes for files in Windows. My understanding is that the way to do this is via Microsoft's Get-FileHash cmdlet under PowerShell. I have seen several web sites with examples and reviewed Microsoft's own documentation. It appears that the following syntax should work on Windows Server 2012:

Get-FileHash myfile.txt -Algorithm SHA256

命令运行没有错误,但是没有输出.如果我将输出发送到文件,则创建的文件没有内容.我还看到了将输出通过管道传输到 Format-List 的示例;我试过了,但仍然没有.我也试过用无效参数运行命令,但又没有.

The command runs without error, but there is no output. If I send the output to a file, the file is created with no content. I have also seen examples which pipe the output to Format-List; I tried that, but still nothing. I have also tried running the command with invalid arguments, and again nothing.

我愿意使用不同的程序,但由于业务要求,它需要支持下载.

I am open to using a different program, but due to business requirements, it would need to be a supported download.

推荐答案

我使用的是 PowerShell 4.0,但刚遇到 Get-FileHash 输出为空的相同问题.我的问题的原因与 OP 不同,但我找到了解决我的问题的方法,我想我会为任何来到此页面试图解决 Get 的空输出(或看似不正确的输出)问题的人发布我的发现-FileHash.

I'm using PowerShell 4.0 and I just encountered the same problem of null output from Get-FileHash. The cause of my problem is different than the OP but I have found a solution to my problem and I figured I would post my findings for anyone who came to this page trying to solve the problem of null output (or seemingly incorrect output) from Get-FileHash.

只有当目标文件的路径包含方括号 [ ] 并且这些方括号包含零个字符或 2 个或更多字符时,才会出现问题(对我而言).

The problem only happens (for me) when the path to the target file contains brackets [ ] and those brackets contain either zero characters or 2 or more characters.

我现在明白为什么会这样了.该字符串被解释为正则表达式 (RegEx),因此方括号 [] 具有其特殊的 RegEx 含义.-LiteralPath 告诉 PowerShell 将字符串解释为简单匹配(无正则表达式).

I now understand WHY this happens. The string is interpreted as Regular Expression (RegEx) so the square brackets [ ] take on their special RegEx meaning. The -LiteralPath tells PowerShell to interpret the string as a simple match (no RegEx).

考虑以下引用 4 个现有文本文件的路径(假设):

Consider the following paths which refer to 4 existing text files (hypothetically):

C:\Test\My Text.txt
C:\Test\My [Text].txt
C:\Test\My [Te]xt.txt
C:\Test\My Text[].txt

以下命令产生正常输出:

The following command produces normal output:

Get-FileHash "C:\Test\My Text.txt"

但是如果使用以下命令会出现空输出:

but there will be null output if using the following commands:

Get-FileHash "C:\Test\My [Text].txt"
Get-FileHash "C:\Test\My [Te]xt.txt"
Get-FileHash "C:\Test\My Text[].txt"

这可以通过使用 -LiteralPath 开关来解决.例如:

This can be solved by using the -LiteralPath switch. For example:

Get-FileHash -LiteralPath "C:\Test\My [Text].txt"

使用 -LiteralPath 开关时,变量会正常展开.例如:

Variables are expanded normally when using the -LiteralPath switch. For example:

(Get-ChildItem C:\Test).FullName | ForEach {
Get-FileHash -LiteralPath $_
}

如果括号之间正好有 1 个字符,则使用 Get-FileHash 时会忽略括号.

If there is exactly 1 character between the brackets, the brackets will be ignored when using Get-FileHash.

考虑以下引用 3 个现有文本文件(假设)的路径,每个文件都有唯一的哈希值:

Consider the following paths which refer to 3 existing text files (hypothetically), each with unique hash values:

C:\Test\My Text.txt
C:\Test\My Tex[t].txt
C:\Test\My[ ]Text.txt

Get-FileHash 以完全相同的方式解释以下所有三个命令(路径被解释为 C:\Test\My Text.txt )因此每个命令都有完全相同的输出,尽管每个文件都有自己独特的哈希值:

Get-FileHash interprets all three of the following commands in exactly the same way ( the path is interpreted as C:\Test\My Text.txt ) and therefore each command has the exact same output despite each file having it's own unique hash value:

Get-FileHash "C:\Test\My Text.txt"
Get-FileHash "C:\Test\My Tex[t].txt"
Get-FileHash "C:\Test\My[ ]Text.txt"

附言我是一个非常新的程序员,请原谅我对术语的错误使用.

P.S. I'm a very new programmer, please forgive me for any poor usage of terminology.

这篇关于无法从 get-filehash 获取输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆