SHIFT

--- Sjoerd Hooft's InFormation Technology ---

User Tools

Site Tools


powerclimd5hash
Differences

This shows you the differences between two versions of the page.

Link to this comparison view

powerclimd5hash [2014/09/23 13:29] (current)
sjoerd created
Line 1: Line 1:
 += PowerCLI - Use MD5 Hashing =
  
 +This also can be used in regular powershell of course, but this is a short howto on using MD5 hashing in powercli scripts. It is based on [[https://​github.com/​Wintellect/​WintellectPowerShell/​blob/​master/​Code/​Get-Hash.psm1|this script from WinTellect]]. ​
 +
 +First step is to create the Get-Hash.psm1 file. This can be a regular textfile and should contain the following lines:
 +<code powershell>​
 +<#
 +.SYNOPSIS
 +    Gets the hash value
 +    ​
 +.DESCRIPTION
 +    Gets the hash value of a file or string
 +    It uses System.Security.Cryptography.HashAlgorithm (http://​msdn.microsoft.com/​en-us/​library/​system.security.cryptography.hashalgorithm.aspx)
 +    and FileStream Class (http://​msdn.microsoft.com/​en-us/​library/​system.io.filestream.aspx)
 +    ​
 +    Written by Josep Martínez Vilà: http://​dbadailystuff.com/​2013/​03/​11/​get-hash-a-powershell-hash-function/​
 +    Based on: http://​blog.brianhartsock.com/​2008/​12/​13/​using-powershell-for-md5-checksums/​ and some ideas on Microsoft Online Help
 +
 +.PARAMETER File
 +    File to get the hash from.
 +    ​
 +.PARAMETER Text
 +    Text string to get the hash from
 +    ​
 +.PARAMETER Algorithm
 +    Type of hash algorithm to use. Default is SHA1
 +    ​
 +.EXAMPLE
 +    C:\PS> Get-Hash "​myFile.txt"​
 +    Gets the SHA1 from myFile.txt file. When there'​s no explicit parameter, it uses -File
 +
 +.EXAMPLE ​   ​
 +    Get-Hash -File "​C:​\temp\myFile.txt"​
 +    Gets the SHA1 from myFile.txt file
 +    ​
 +.EXAMPLE
 +    C:\PS> Get-Hash -Algorithm "​MD5"​ -Text "Hello Wold!" ​
 +    Gets the MD5 from a string
 +       
 +.EXAMPLE
 +    C:\PS> "Hello Wold!" | Get-Hash
 +    We can pass a string throught the pipeline ​   ​
 +#>
 +function Get-Hash
 +{
 +    Param
 +    (
 +        [parameter(Mandatory=$true,​ ValueFromPipeline=$true,​ ParameterSetName="​set1"​)]
 +        [String]
 +        $text,
 +        [parameter(Position=0,​ Mandatory=$true,​ ValueFromPipeline=$false,​ ParameterSetName="​set2"​)]
 +        [String]
 +        $file = "",​
 +        [parameter(Mandatory=$false,​ ValueFromPipeline=$false)]
 +        [ValidateSet("​MD5",​ "​SHA",​ "​SHA1",​ "​SHA-256",​ "​SHA-384",​ "​SHA-512"​)]
 +        [String]
 +        $algorithm = "​SHA1"​
 +    )
 +
 +    Begin
 +    {
 +        $hashAlgorithm = [System.Security.Cryptography.HashAlgorithm]::​Create($algorithm)
 +    }
 +     ​Process
 +     {
 +        $md5StringBuilder = New-Object System.Text.StringBuilder 50
 +        $ue = New-Object System.Text.UTF8Encoding ​
 +
 +        if ($file){
 +            try {
 +                if (!(Test-Path $file)){
 +                    throw "​Test-Path returned false."​
 +                }
 +            }
 +            catch {
 +                throw "​Get-Hash - File not found or without permisions: [$file]. $_"
 +            } 
 +
 +            try {        ​
 +                [System.IO.FileStream]$fileStream = [System.IO.File]::​Open($file,​ [System.IO.FileMode]::​Open);​
 +                $hashAlgorithm.ComputeHash($fileStream) | % { [void] $md5StringBuilder.Append($_.ToString("​x2"​)) }
 +            }
 +            catch {
 +                throw "​Get-Hash - Error reading or hashing the file: [$file]"​
 +            } 
 +            finally {
 +                $fileStream.Close()
 +                $fileStream.Dispose()
 +            }
 +        }
 +        else {
 +            $hashAlgorithm.ComputeHash($ue.GetBytes($text)) | % { [void] $md5StringBuilder.Append($_.ToString("​x2"​)) }
 +        }
 +        ​
 +        return $md5StringBuilder.ToString()
 +    }
 +}
 +</​code>​
 +
 +Now you can use it in your script by first importing the script as a module:
 +<​code>​
 +# Add Get-Hash function
 +Import-Module g:​\scripts\get-hash.psm1
 +</​code>​
 +
 +Now you can create a string, for example based on the VM name with an extra string:
 +<code powershell>​
 +$stringtohash = $vm.toUpper() + "​-go4shift"​
 +$guestpw = (Get-Hash -Algorithm "​MD5"​ -text $stringtohash)
 +</​code>​
 +
 +or like this to add extra parameters after hashing:
 +<code powershell>​
 +$guestpw = (Get-Hash -Algorithm "​MD5"​ -text $stringtohash) + "​!!!"​
 +</​code>​
 +
 +Now you can use the password like you would normally do, for example when using the {{{Copy-VMGuestFile}}} commandlet:
 +<code powershell>​
 +Copy-VMGuestFile -Source $scriptlocation\$script -Destination $guestdir -Force -VM $vm -LocalToGuest -HostUser root -HostPassword $hostpw -GuestUser Administrator -GuestPassword $guestpw
 +</​code>​
 +
 +{{tag>​vmware scripts security}}
powerclimd5hash.txt · Last modified: 2014/09/23 13:29 by sjoerd