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

问题描述

我正在使用 Boe Prox's script 打印目录中所有文件文件夹的列表。我需要 Prox 的脚本(而不是其他 Windows 打印目录命令),因为它使用 robocopy 打印长度超过 260 个字符的文件路径。

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

我做过的研究:

尝试解决问题:

  • 我还尝试修改 filehash 的语法,使其更符合脚本的其余部分,即。 Hash = $matches.Hash (这将返回所有空白代替文件哈希)
  • 我尝试去掉似乎指定项目而不是项目内容的正则表达式部分,即:If ($_.Trim() -match "^(?<Children>\d+)\s+") { (这会导致错误代码 WARNING: Cannot bind argument to parameter 'Path' because it is null.

我非常希望这能发生: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 '.*\.(.*)','$1'
                                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)"