Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function Get-RSFileHash {
- <#
- .Synopsis
- Returns an MD5 filehash when given a file path
- .DESCRIPTION
- Returns an MD5 filehash when given a file path
- .EXAMPLE
- Get-RSFileHash -Filename c:\temp\filetohash.txt
- .EXAMPLE
- Get-ChildItem c:\temp\*.txt | get-rsfilehash
- #>
- [CmdletBinding()]
- [OutputType([string])]
- Param
- (
- [Parameter(Mandatory=$true,
- ValueFromPipeline=$true,
- Position=0,
- ParameterSetName="String")]
- [string[]]$Path,
- [Parameter(Mandatory=$true,
- ValueFromPipeline=$true,
- Position=0,
- ParameterSetName="FileInfo")]
- [IO.FileInfo[]]$InputObject
- )
- BEGIN {"------------"}
- Process
- {
- if($PSCmdlet.ParameterSetName -eq "string") {
- $file = $Path
- } else {
- $file = $InputObject.FullName
- }
- foreach($file in $file){
- if (test-path -Path $file -PathType Leaf) {
- try {
- $md5 = New-Object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider
- $stream = [System.IO.File]::Open($file, [System.IO.FileMode]::Open, [System.IO.FileAccess]::Read)
- $hash = [System.BitConverter]::ToString($md5.ComputeHash($stream))
- $stream.Close()
- Write-Debug "$file - $hash"
- Write-Output $hash.Replace('-',$null)
- }
- catch {
- throw "Unable to generate hash for $file - $($_)"
- }
- }
- }
- }
- }
- $someFilePath = "C:\ps\test.txt","C:\ps\test.tx3"
- $files = get-childitem -Path $someFilePath
- $files | Get-RSFileHash
- Get-RSFileHash -path $someFilePath[0]
- Get-RSFileHash -path $someFilePath
- Get-RSFileHash -InputObject $files[0]
- Get-RSFileHash -InputObject $files
- $someFilePath | Get-RSFileHash
Advertisement
Add Comment
Please, Sign In to add comment