计算长度超过 256 个字符的文件的哈希值?

Calculate the hash of a file longer than 256 characters?

我正在使用 Boe Prox's script 打印目录中所有文件和文件夹的列表。我需要 Prox 的脚本(与其他 windows 打印目录命令相反),因为它使用 robocopy 来打印超过 260 个字符的文件路径。

我的问题是,我还希望将 filehash 与文件名一起打印出来。我已经修改了脚本以获取哈希值(请参见下文)并且它通常可以正常工作,除非文件路径超过 260 个字符。长文件路径在最终输出的散列列中出现空白。

我做过的研究:

尝试解决问题:

我非常希望这会发生:Boe 原始脚本中的评论包括以下行:“版本 1.1 - 增加了计算文件哈希值的能力”

这是我的(部分可用的脚本):

Function Get-FolderItem {

    [cmdletbinding(DefaultParameterSetName='Filter')]
    Param (
        [parameter(Position=0,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
        [Alias('FullName')]
        [string[]]$Path = $PWD,
        [parameter(ParameterSetName='Filter')]
        [string[]]$Filter = '*.*',    
        [parameter(ParameterSetName='Exclude')]
        [string[]]$ExcludeFile,              
        [parameter()]
        [int]$MaxAge,
        [parameter()]
        [int]$MinAge
    )
    Begin {
        $params = New-Object System.Collections.Arraylist
        $params.AddRange(@("/L","/E","/NJH","/BYTES","/FP","/NC","/XJ","/R:0","/W:0","T:W"))
        If ($PSBoundParameters['MaxAge']) {
            $params.Add("/MaxAge:$MaxAge") | Out-Null
        }
        If ($PSBoundParameters['MinAge']) {
            $params.Add("/MinAge:$MinAge") | Out-Null
        }
    }
    Process {
        ForEach ($item in $Path) {
            Try {
                $item = (Resolve-Path -LiteralPath $item -ErrorAction Stop).ProviderPath
                If (-Not (Test-Path -LiteralPath $item -Type Container -ErrorAction Stop)) {
                    Write-Warning ("{0} is not a directory and will be skipped" -f $item)
                    Return
                }
                If ($PSBoundParameters['ExcludeFile']) {
                    $Script = "robocopy `"$item`" NULL $Filter $params /XF $($ExcludeFile  -join ',')"
                } Else {
                    $Script = "robocopy `"$item`" NULL $Filter $params"
                }
                Write-Verbose ("Scanning {0}" -f $item)
                Invoke-Expression $Script | ForEach {
                    Try {
                        If ($_.Trim() -match "^(?<Children>\d+)\s+(?<FullName>.*)") {
                           $object = New-Object PSObject -Property @{
                                FullName = $matches.FullName
                                Extension = $matches.fullname -replace '.*\.(.*)',''
                                FullPathLength = [int] $matches.FullName.Length
                                Length = [int64]$matches.Size
                                FileHash = (Get-FileHash -Path $matches.FullName).Hash
                                Created = (Get-Item $matches.FullName).creationtime
                                LastWriteTime = (Get-Item $matches.FullName).LastWriteTime
                            } 
                            $object.pstypenames.insert(0,'System.IO.RobocopyDirectoryInfo')
                            Write-Output $object
                        } Else {
                            Write-Verbose ("Not matched: {0}" -f $_)
                        }
                    } Catch {
                        Write-Warning ("{0}" -f $_.Exception.Message)
                        Return
                    }
                }
            } Catch {
                Write-Warning ("{0}" -f $_.Exception.Message)
                Return
            }
        }
    }
}


Get-FolderItem "C:\TestingFileFolders" 

Windows PowerShell中,您可以prepend \?\到完整路径并读取文件:

Get-FileHash -Path "\?$($matches.FullName)"