Powershell:包含要使用参数调用的函数的哈希表

时间:2016-11-23 01:11:08

标签: function powershell variables hashtable

我有这样的字典:

function HashHandlerSHA256
{
    param($Path, $Checksum)

    $csp = new-object -TypeName System.Security.Cryptography.SHA256CryptoServiceProvider
    $ComputedHash = $csp.ComputeHash([System.IO.File]::ReadAllBytes($Path))
    $ComputedHash = [System.BitConverter]::ToString($ComputedHash).Replace("-", "").ToLower()

    $result = $ComputedHash.CompareTo($Checksum)

    return $result -eq 0
}

$HashHandler = @{"SHA256" = HashHandlerSHA256}

包含验证算法和要调用的函数。这些函数都应该具有相同的参数和返回类型。

现在我有:

$Checksums = @{"SHA256" = "..."}

我想根据我可用的算法和值来调用正确的函数。在这种情况下,我有一个有效的sha256哈希。

现在我想做:

function Validate
{
    param($Path, $Checksums)

    foreach($Hash in $Checksums) {
        $Type = $Hash.Name
        $Value = $Hash.Value

        if ($HashHandler.ContainsKey($Type)) {
            $Handler = $HashHandler.Get_Item($Type)
            if (-Not ($Handler -Path $Path -Checksum $Value)) {
                return $FALSE
            }
        }
    }

    return $TRUE
}

我得到的错误消息是:

At C:\Users\username\Desktop\hashtest.ps1:27 char:23
+             if (-Not ($Handler -Path $Path -Checksum $Value)) {
+                                ~~~~~
Unexpected token '-Path' in expression or statement.

我对PowerShell比较陌生。我知道如何使用参数调用函数,但是当存储在变量中时,我没有设法解决这个问题,当在线搜索时,我没有得到我需要的答案。

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

如果我理解你想要这样的东西

function HashHandlerSHA256
{
    param($Path, $Checksum)

    $csp = new-object -TypeName System.Security.Cryptography.SHA256CryptoServiceProvider
    $ComputedHash = $csp.ComputeHash([System.IO.File]::ReadAllBytes($Path))
    $ComputedHash = [System.BitConverter]::ToString($ComputedHash).Replace("-", "").ToLower()
    $result = $ComputedHash.CompareTo($Checksum)

    return $result -eq 0
}

function Validate
{
    param($Path, $Checksums)

        foreach($Hashkey in $Checksums.Keys) 
        {
            $Value = $Checksums[$Hashkey]

            if ($script:HashHandler.ContainsKey($Hashkey)) 
            {
                if (-Not (&$script:HashHandler[$Hashkey] -Path $Path -Checksum $Value)) { return $false}
            }
        }

    return $TRUE
}


#add here your couples of algo/function
$script:HashHandler = @{"SHA256" = 'HashHandlerSHA256'}

#checksum to test
$Checksums=@{}
$Checksums["SHA256"]= 'd6a0a09fb1a7971b497674675d5b5621d865d6020e384137548de9c4ac6d4994'
$Checksums["MD5"]= 'xxxx'

#test list checksum and algo
Validate -Path "c:\temp\hello.csv" -Checksums $Checksums

另一种解决方案

$file="C:\temp\exludevalue.txt"
$Checksums=@{}
$Checksums["SHA256"]= 'd6a0a09fb1a7971b497674675d5b5621d865d6020e384137548de9c4ac6d4994'
$Checksums["MD5k"]= '11A8D99F80F9B29FCF6A995D2F17B2E3'

$Checksums.Keys |   
%{
    if ($(gcm Get-FileHash).Parameters.Algorithm.Attributes.ValidValues -contains $_)
    {
        $algocalc=(Get-FileHash  -path $file -Algorithm $_).Hash;
    }
    else
    {
        $algocalc='ALGO NOT FOUNDED'
    }

    new-object psobject -Property @{ 
                        Algo=$_
                        OldValue=$Checksums[$_]
                        CalculedValue=$algocalc
                        ResultComparison= $algocalc -eq $Checksums[$_]
                        }
}