Saturday, April 2, 2016

PowerShell v3 Function Get ObjectHash

The following function is based on Lee Holmes Get-FileHash:
http://poshcode.org/2154
I simply added a switch for strings to accompany the current file option when calculating hashes.  So, this is really Lees function with a little extra cut-and-paste code.
function Get-ObjectHash
{
    ##############################################################################
    ##
    ## Get-ObjectHash
    ##
    ## Adapted From Windows PowerShell Cookbook (OReilly)
    ## by Lee Holmes (http://www.leeholmes.com/guide)
    ##
    ##############################################################################

    <#

        .SYNOPSIS

            Get the hash of an input file or string.

        .EXAMPLE

        Get-FileHash myFile.txt
        Gets the hash of a specific file

        .EXAMPLE

        dir | Get-FileHash
        Gets the hash of files from the pipeline

        .EXAMPLE

        Get-FileHash myFile.txt -Hash SHA1
        Gets the has of myFile.txt, using the SHA1 hashing algorithm
           
            .EXAMPLE
           
            Get-ObjectHash string
            Gets the MD5 hash of the string string.
     
            .EXAMPLE
           
            Get-ObjectHash -String @(test,string,MD5) -HashAlgorithm SHA256
            Gets the SHA256 hash of an array of strings.
    #>

      [CmdletBinding(
            DefaultParameterSetName = String
      )]
    param(
        ## The string to check
            [Parameter(
                  ParameterSetName = String
            )]
            [String[]]
            $String,
           
            ## The path of the file to check
            [Parameter(
                  ParameterSetName = Path
            )]
            [String[]]       
        $Path,

        ## The algorithm to use for hash computation
        [ValidateSet("MD5", "SHA1", "SHA256", "SHA384", "SHA512")]
        $HashAlgorithm = "MD5"
    )

    Set-StrictMode -Version Latest

    ## Create the hash object that calculates the hash of our file.
    $hashType = [Type] "System.Security.Cryptography.$HashAlgorithm"
    $hasher = $hashType::Create()

    if($PSBoundParameters.ContainsKey(Path))
      {
            ## Create an array to hold the list of files
          $files = @()

          ## If they specified the file name as a parameter, add that to the list
          ## of files to process
          if($path)
          {
              $files += $path
          }
          ## Otherwise, take the files that they piped in to the script.
          ## For each input file, put its full name into the file list
          else
          {
              $files += @($input | Foreach-Object { $_.FullName })
          }

          ## Go through each of the items in the list of input files
          foreach($file in $files)
          {
              ## Skip the item if it is not a file
              if(-not (Test-Path $file -Type Leaf)) { continue }

              ## Convert it to a fully-qualified path
              $filename = (Resolve-Path $file).Path

              ## Use the ComputeHash method from the hash object to calculate
              ## the hash
              $inputStream = New-Object IO.StreamReader $filename
              $hashBytes = $hasher.ComputeHash($inputStream.BaseStream)
              $inputStream.Close()

              ## Convert the result to hexadecimal
              $builder = New-Object System.Text.StringBuilder
              $hashBytes | Foreach-Object { [void] $builder.Append($_.ToString("X2")) }

              ## Return a custom object with the important details from the
              ## hashing
              $output = New-Object PsObject -Property @{
                  Path

Related Post:

0 comments:

Post a Comment

 
Copyright 2009 Information Blog
Powered By Blogger