-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGlobals.ps1
More file actions
106 lines (86 loc) · 2.24 KB
/
Globals.ps1
File metadata and controls
106 lines (86 loc) · 2.24 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#--------------------------------------------
# Declare Global Variables and Functions here
#--------------------------------------------
$currentVersion = 'v2.1'
$algorithmProperties = (
'MD5',
'SHA1',
'SHA-256',
'SHA-512'
)
#Sample function that provides the location of the script
function Get-ScriptDirectory
{
if($hostinvocation -ne $null)
{
Split-Path $hostinvocation.MyCommand.path
}
else
{
Split-Path $script:MyInvocation.MyCommand.Path
}
}
#Sample variable that provides the location of the script
[string]$ScriptDirectory = Get-ScriptDirectory
function Clear-DataGridView
{
# Clean up DataGridView
$dgv1.DataBindings.Clear()
$dgv1.DataSource = $null
$dgv1.Rows.Clear()
$dgv1.Columns.Clear()
}# function Clear-DataGridView
function Get-FileHashValue
{
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 = "MD5"
)
BEGIN
{
$hashAlgorithm = [System.Security.Cryptography.HashAlgorithm]::Create($algorithm)
}# BEGIN
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."
}# if
} catch
{
throw "Get-Hash - File not found or without permisions: [$file]. $_"
}# try/catch
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()
}# try/catch/finally
} else
{
$hashAlgorithm.ComputeHash($ue.GetBytes($text)) | % { [void] $md5StringBuilder.Append($_.ToString("x2")) }
}# else
return $md5StringBuilder.ToString()
}# PROCESS
}# function Get-Hash