Имя пользователя:
Пароль:  
Помощь | Регистрация | Забыли пароль?  

Показать сообщение отдельно

Ветеран


Сообщения: 1259
Благодарности: 861

Профиль | Отправить PM | Цитировать


PowerShell 3.0 и выше:

Код: Выделить весь код
Function Get-FileHash
{
    [CmdletBinding(DefaultParameterSetName = "Path")]
    param(
        [Parameter(Mandatory, ParameterSetName="Path", Position = 0)]
        [System.String[]]
        $Path,

        [Parameter(Mandatory, ParameterSetName="LiteralPath", ValueFromPipelineByPropertyName = $true)]
        [Alias("PSPath")]
        [System.String[]]
        $LiteralPath,

        [ValidateSet("SHA1", "SHA256", "SHA384", "SHA512", "MACTripleDES", "MD5", "RIPEMD160")]
        [System.String]
        $Algorithm="SHA256"
    )

    begin
    {
        # Construct the strongly-typed crypto object
        $hasher = [System.Security.Cryptography.HashAlgorithm]::Create($Algorithm)
    }

    process
    {
        $pathsToProcess = @()

        if($PSCmdlet.ParameterSetName  -eq "LiteralPath")
        {
            $pathsToProcess += Resolve-Path -LiteralPath $LiteralPath | Foreach-Object ProviderPath
        }
        else
        {
            $pathsToProcess += Resolve-Path $Path | Foreach-Object ProviderPath
        }

        foreach($filePath in $pathsToProcess)
        {
            if(Test-Path -LiteralPath $filePath -PathType Container)
            {
                continue
            }

            try
            {
                # Read the file specified in $FilePath as a Byte array
                [system.io.stream]$stream = [system.io.file]::OpenRead($FilePath)

                # Compute file-hash using the crypto object
                [Byte[]] $computedHash = $hasher.ComputeHash($stream)
            }
            catch [Exception]
            {
                $errorMessage = [Microsoft.PowerShell.Commands.UtilityResources]::FileReadError -f $FilePath, $_
                Write-Error -Message $errorMessage -Category ReadError -ErrorId "FileReadError" -TargetObject $FilePath
                return
            }
            finally
            {
                if($stream)
                {
                    $stream.Close()
                }
            }

            # Convert to hex-encoded string
            [string] $hash = [BitConverter]::ToString($computedHash) -replace '-',''

            $retVal = [PSCustomObject] @{
                Algorithm = $Algorithm.ToUpperInvariant()
                Hash = $hash
                Path = $filePath
            }
            $retVal.psobject.TypeNames.Insert(0, "Microsoft.Powershell.Utility.FileHash")

            $retVal
        }
    }
}

Get-ChildItem D:\1,D:\2 -File | Select Name,FullName,@{n="Hash";e={(Get-FileHash $_.FullName -Algorithm MD5).Hash}} | 
    Group Name,Hash | Where Count -ge 2 | Remove-Item -Path {$_.Group.FullName -match "D:\\2\\"}
Это сообщение посчитали полезным следующие участники:

Отправлено: 11:35, 11-11-2013 | #2