<#.SYNOPSISGets the hash value of a file or string.DESCRIPTIONGets the hash value of a file or stringIt 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)Based on: http://blog.brianhartsock.com/2008/12/13/using-powershell-for-md5-checksums/ and some ideas on Microsoft Online HelpBe aware, to avoid confusions, that if you use the pipeline, the behaviour is the same as using -Text, not -File.PARAMETER FileFile to get the hash from..PARAMETER TextText string to get the hash from.PARAMETER AlgorithmType of hash algorithm to use. Default is SHA1.EXAMPLEC:\PS> Get-Hash "hello_world.txt"Gets the SHA1 from myFile.txt file. When there's no explicit parameter, it uses -File.EXAMPLEGet-Hash -File "C:\temp\hello_world.txt"Gets the SHA1 from myFile.txt file.EXAMPLEC:\PS> Get-Hash -Algorithm "MD5" -Text "Hello Wold!"Gets the MD5 from a string.EXAMPLEC:\PS> "Hello Wold!" | Get-HashWe can pass a string throught the pipeline.EXAMPLEGet-Content "c:\temp\hello_world.txt" | Get-HashIt gets the string from Get-Content.EXAMPLEGet-ChildItem "C:\temp\*.txt" | %{ Write-Output "File: $($_) has this hash: $(Get-Hash $_)" }This is a more complex example gets the hash of all "*.tmp" files.NOTESDBA daily stuff (http://dbadailystuff.com) by Josep Martínez VilàLicensed under a Creative Commons Attribution 3.0 Unported License.LINK#>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 -literalpath $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() }}
По теме:
http://xaegr.wordpress.com и http://xaegr.wordpress.com и http://dbadailystuff.com