Unable to get output from get-filehash

前端 未结 3 1355
[愿得一人]
[愿得一人] 2020-12-21 10:43

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

3条回答
  •  生来不讨喜
    2020-12-21 11:36

    Get-FileHash, requires Windows PowerShell 4.0 Based on your comments you are at version 3, which is default on Win 2012 (non R2) Here how to check you PS version

    You can update PS on Win 2012 (non R2) to version 4.0 or use Win 2012 R2

    If you just run Get-FileHash on a PS version 3 system you should get

    PS C:\> Get-FileHash
    Get-FileHash : The term 'Get-FileHash' is not recognized as the name of a cmdlet, function, script file, or operable
    program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
    At line:1 char:1
    + Get-FileHash
    + ~~~~~~~~~~~~
        + CategoryInfo          : ObjectNotFound: (Get-FileHash:String) [], CommandNotFoundException
        + FullyQualifiedErrorId : CommandNotFoundException
    

    On lower PS version you can use this function

    Function Get-FileHashTSO([String] $FileName,$HashName = "SHA1") 
    {
    $FileStream = New-Object System.IO.FileStream($FileName,[System.IO.FileMode]::Open) 
    $StringBuilder = New-Object System.Text.StringBuilder 
    [System.Security.Cryptography.HashAlgorithm]::Create($HashName).ComputeHash($FileStream)|%{[Void]$StringBuilder.Append($_.ToString("x2"))} 
    $FileStream.Close() 
    $StringBuilder.ToString() 
    }
    

    store it as .ps (e.g. Get-FileHashTSO.ps1) file and call it like this

    powershell -command "& { . C:\myScripts\Get-FileHashTSO.ps1 ; Get-FileHashTSO "C:\someLocation\someFile.iso" "SHA1" }"
    

提交回复
热议问题