-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGet-RepositoryDigest.ps1
132 lines (118 loc) · 5.22 KB
/
Get-RepositoryDigest.ps1
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/local/bin/pwsh
#Requires -Version 4
#========================================
# NAME : Get-RepositoryDigest.ps1
# LANGUAGE : PowerShell
# AUTHOR : Bryan Dady
# UPDATED : 03/14/2019
# COMMENT : Returns a collection for per-file hash values for offline comparison of repositories
#========================================
[CmdletBinding()]
param()
Set-StrictMode -Version latest
# Uncomment the following 2 lines for testing profile scripts with Verbose output
#'$VerbosePreference = ''Continue'''
#$VerbosePreference = 'Continue'
Write-Verbose -Message 'Detect -Verbose $VerbosePreference'
switch ($VerbosePreference) {
Stop { $IsVerbose = $True }
Inquire { $IsVerbose = $True }
Continue { $IsVerbose = $True }
SilentlyContinue { $IsVerbose = $False }
Default { if ('Verbose' -in $PSBoundParameters.Keys) {$IsVerbose = $True} else {$IsVerbose = $False} }
}
Write-Verbose -Message ('$VerbosePreference = ''{0}'' : $IsVerbose = ''{1}''' -f $VerbosePreference, $IsVerbose)
Write-Verbose -Message 'Importing function Get-RepositoryDigest'
function Get-RepositoryDigest {
[CmdletBinding(SupportsShouldProcess)]
param (
[parameter(Position = 0, ValueFromPipelineByPropertyName )]
[ValidateScript({
try {
$Folder = Get-Item $_ -ErrorAction Stop
} catch [System.Management.Automation.ItemNotFoundException] {
Throw [System.Management.Automation.ItemNotFoundException] "${_}"
}
if ($Folder.PSIsContainer) {
$True
} else {
Throw [System.Management.Automation.ValidationMetadataException] "Path '${_}' is not a container (Directory)."
}
})]
[Alias('Root')]
[string]
$Path = (Join-Path -Path $HOME -ChildPath "*\*PowerShell" -Resolve),
[parameter(Position = 1, ValueFromPipelineByPropertyName )]
[string]
$Filter = '*.ps*1',
[parameter(Position = 2, ValueFromPipelineByPropertyName )]
[switch]
$Recurse,
# Algorithm is passed through to the Get-FileHash cmdlet and the supported values are based on what that cmdlet Supports
[parameter(ValueFromPipelineByPropertyName)]
[ValidateSet('SHA1', 'SHA256', 'SHA384', 'SHA512', 'MD5')]
[string]
$Algorithm = 'MD5'
)
begin {
Write-Verbose -Message ('Root for comparison ($Path) is: {0}' -f $Path)
# Transpose function parameters into $GCIparams, e.g. -Path . -File -Filter *.ps*1 -Recurse -Depth 1
# Declare Hashtable variable for later Splatting through Get-ChildItem cmdlet
$GCIparams = @{
Path = $Path
Filter = $Filter
File = $true
Recurse = $Recurse
# Depth = '1'
}
Write-Verbose -Message ('$GCIparams: {0}' -f (($GCIparams.Keys | ForEach-Object -Process {'-{0} {1}' -f $_, $GCIparams.$_}) -join ' '))
Write-Verbose -Message ('$Algorithm: {0}' -f $Algorithm)
}
process {
# For each file, return a custom object to the collection
Get-ChildItem @GCIparams | ForEach-Object -Process {
$objectAttributes = [ordered]@{
'Name' = $PSItem.Name
'FullName' = $PSItem.FullName
'Path' = $PSItem.FullName -replace ((Resolve-Path -Path $Path).Path -replace '\\','\\'),'[root]'
'Length' = $PSItem.Length
'Hash' = (Get-FileHash -Path $PSItem.FullName -Algorithm $Algorithm).Hash
'LastWriteTime' = $PSItem.LastWriteTime
}
$File = New-Object -TypeName PSObject -Property $objectAttributes
# Add AliasProperties to match property and alias names of FileInfo Type objects
Add-Member -InputObject $File -MemberType AliasProperty -Name Size -Value Length -SecondValue System.ValueType
Add-Member -InputObject $File -MemberType AliasProperty -Name DateModified -Value LastWriteTime -SecondValue System.String
return $File
}
}
end {
Remove-Variable -Name GCIparams -ErrorAction SilentlyContinue
Remove-Variable -Name File -ErrorAction SilentlyContinue
}
<#
.SYNOPSIS
Short description
.DESCRIPTION
Long description
.EXAMPLE
PS C:\> <example usage>
Explanation of what the example does
.INPUTS
Inputs (if any)
.OUTPUTS
Output (if any)
.NOTES
General notes
Collect PowerShell artifact information into a variable
$Digest = Get-RepositoryDigest -Path ~\Documents\WindowsPowerShell -Recurse
Export to JSON file for offline comparison
$Digest | ConvertTo-Json | Out-File -FilePath .\RepositoryDigest.json -Encoding utf8
Import from JSON file for comparison
$CompareDigest = (Get-Content -Path .\RepositoryDigest.json) -join "`n" | ConvertFrom-Json
Compare 2 digest objects by their relative 'Path' property
$Comparison = Compare-Object -ReferenceObject $Digest -DifferenceObject $CompareDigest -Property Path -PassThru -IncludeEqual
Review comparison results by SideIndicator group
$Comparison | Group-Object -Property SideIndicator
#>
}