-
Notifications
You must be signed in to change notification settings - Fork 0
/
FolderCompare.ps1
118 lines (113 loc) · 4.33 KB
/
FolderCompare.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
<#
.SYNOPSIS
The script compares deals with Filehashes from folders and Files
#>
#region Params
param(
[Parameter(Position=0, Mandatory=$false,ValueFromPipeline=$false)]
[ValidateScript({Test-Path -LiteralPath $_ -PathType 'Container'})]
[System.String]
$LeftDir="",
[Parameter(Position=1, Mandatory=$false,ValueFromPipeline=$false)]
[ValidateScript({Test-Path -LiteralPath $_ -PathType 'Container'})]
[System.String]
$RightDir="",
[Parameter(Position=2, Mandatory=$false,ValueFromPipeline=$true)]
[ValidateNotNullOrEmpty()]
[System.String]
$LeftHashFile="",
[Parameter(Position=3, Mandatory=$false,ValueFromPipeline=$false)]
[ValidateNotNullOrEmpty()]
[System.String]
$RightHashFile="",
[Parameter(Position=4, Mandatory=$false,ValueFromPipeline=$false)]
[ValidateNotNullOrEmpty()]
[System.String]
$LogFile=".\Backup-Files.log"
)
#endregion
#C:\Users\heinz\MagentaCLOUD\backupdata
#.\Test.xml
begin{
function GetHashFromDir {
param(
[Parameter(Mandatory=$false,ValueFromPipeline=$true)]
[ValidateScript({Test-Path -LiteralPath $_ -PathType 'Container'})]
[System.String]
$HashDir=""
)
Get-ChildItem $HashDir -Recurse | Get-FileHash | select @{Label="Path";Expression={$_.Path.Replace($HashDir,"")}},Hash
}
function GetHashFromFile {
param(
[Parameter(Mandatory=$false,ValueFromPipeline=$false)]
[ValidateNotNullOrEmpty()]
[System.String]
$HashFile=""
)
Import-Clixml $HashFile
}
function CompareLeftFileRightFolder{
param(
[Parameter(Mandatory=$false,ValueFromPipeline=$false)]
[ValidateNotNullOrEmpty()]
[System.String]
$HashFile="",
[Parameter(Mandatory=$false,ValueFromPipeline=$false)]
[ValidateScript({Test-Path -LiteralPath $_ -PathType 'Container'})]
[System.String]
$HashDir=""
)
$LeftSideHash = Import-Clixml $HashFile
$RightSideHash = foreach ($Line in $LeftSideHash) {
$File = $($HashDir + $Line.path)
write-verbose ("Bilde Hash von der Datei: " + $File)
Get-FileHash $File| select @{Label="Path";Expression={$_.Path.Replace($HashDir,"")}},Hash
}
$Result = Compare-Object $LeftSideHash $RightSideHash -Property Path,Hash
$Result
}
#0 - is OK. 1 - some error
$exitValue="Nichts gelaufen"
}
process{
try{
if ($LeftDir -ne "" -and $RightDir -eq "" -and $LeftHashFile -ne "" -and $RightHashFile -eq "") {
GetHashFromDir $LeftDir |Export-Clixml $LeftHashFile
$exitValue = 0
}
if ($LeftDir -eq "" -and $RightDir -ne "" -and $LeftHashFile -eq "" -and $RightHashFile -ne "") {
GetHashFromDir $RightDir |Export-Clixml $RightHashFile
$exitValue = 0
}
if ($LeftDir -eq "" -and $RightDir -ne "" -and $LeftHashFile -ne "" -and $RightHashFile -eq "") {
if ($LeftHashFile -match "\*") {
$cnt=0
$SourceFiles = gci $LeftHashFile
foreach ($file in $sourcefiles) {
write-verbose ("Verwende Hashfile" + $file)
$Return = CompareLeftFileRightFolder $File $RightDir
if ($Return) {$cnt++}
}
$Return = $cnt
}
else {
write-verbose ("Verwende Hashfile -------------- " + $LeftHashFile)
$Return = CompareLeftFileRightFolder $LeftHashFile $RightDir
}
If ($Return) {$exitValue = $Return} else {$exitValue = 0}
}
if ($LeftDir -ne "" -and $RightDir -ne "" -and $LeftHashFile -eq "" -and $RightHashFile -eq "") {
$Return = Compare-Object (GetHashFromDir $LeftDir) (GetHashFromDir $RightDir) -Property Path,Hash
If ($Return) {$exitValue = $Return} else {$exitValue = 0}
}
if ($LeftDir -eq "" -and $RightDir -eq "" -and $LeftHashFile -ne "" -and $RightHashFile -ne "") {
$Return = Compare-Object (GetHashFromFile $LeftHashFile) (GetHashFromFile $RightHashFile)
If ($Return) {$exitValue = $Return} else {$exitValue = 0}
}
$exitValue
}
catch {
}
}
end{exit $exitValue}