Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.9k views
in Technique[技术] by (71.8m points)

powershell - Unable to get output from get-filehash

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

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.

EDIT: 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).

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

C:TestMy Text.txt
C:TestMy [Text].txt
C:TestMy [Te]xt.txt
C:TestMy Text[].txt

The following command produces normal output:

Get-FileHash "C:TestMy Text.txt"

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

Get-FileHash "C:TestMy [Text].txt"
Get-FileHash "C:TestMy [Te]xt.txt"
Get-FileHash "C:TestMy Text[].txt"

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

Get-FileHash -LiteralPath "C:TestMy [Text].txt"

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

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

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

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

C:TestMy Text.txt
C:TestMy Tex[t].txt
C:TestMy[ ]Text.txt

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

Get-FileHash "C:TestMy Text.txt"
Get-FileHash "C:TestMy Tex[t].txt"
Get-FileHash "C:TestMy[ ]Text.txt"

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...