-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathash.ps1
More file actions
44 lines (43 loc) · 1.61 KB
/
ash.ps1
File metadata and controls
44 lines (43 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
function FileHash {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelinebyPropertyName = $true)]
[string]$FullName,
[ValidateSet("MD5","SHA1","SHA256","SHA384","SHA512")]
[string]$Hash = "SHA1"
)
begin {
function Test-FileLock ($file) {
$locked = $false
trap {
Set-Variable -name locked -value $true -scope 1
continue
}
$inputStream = New-Object system.IO.StreamReader $file
if ($inputStream) {$inputStream.Close()}
$locked
}
$Hasher = switch ($Hash) {
"MD5" {[System.Security.Cryptography.MD5]::Create()}
"SHA1" {[System.Security.Cryptography.SHA1]::Create()}
"SHA256" {[System.Security.Cryptography.SHA256]::Create()}
"SHA384" {[System.Security.Cryptography.SHA384]::Create()}
"SHA512" {[System.Security.Cryptography.SHA512]::Create()}
}
}
Process {
$file = gi (Resolve-Path $FullName) -Force
if ($file -is [IO.FileInfo]) {
if (Test-FileLock $file) {return}
$inputStream = New-Object System.IO.StreamReader ($file)
$hashBytes = $hasher.ComputeHash($inputStream.BaseStream)
$inputStream.Close()
$builder = New-Object System.Text.StringBuilder
$hashBytes | %{[void]$builder.Append($_.ToString("X2"))}
New-Object psobject -Property @{
FullName = $file.ToString()
Hash = $builder.ToString()
}
}
}
}