forked from HanSolo71/Copy-and-Verify-Files-And-Folders
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CopyFilesAndFolders.ps1
115 lines (90 loc) · 3.32 KB
/
CopyFilesAndFolders.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
function Get-FolderHash ($folder) {
dir $folder -Recurse | ?{!$_.psiscontainer} | %{[Byte[]]$contents += [System.IO.File]::ReadAllBytes($_.fullname)}
$hasher = [System.Security.Cryptography.SHA1]::Create()
[string]::Join("",$($hasher.ComputeHash($contents) | %{"{0:x2}" -f $_}))
}
Remove-Item "\\FolderToClear" -Recurse
#Create Array To Store File Locations and hashes
$OriginalFilesLocation = @()
$OriginalFileHash = @()
$NewFilesHashArray = @()
$OriginalFilesHashArray = @()
$NewFileHash = @()
$NewFiles = @()
#Folder Locations
$OrgininalFolderLocations = @()
$OriginalFolderHash = @()
$NewFolderArray = @()
$OriginalFoldersHashArray = @()
$NewFoldersHash = @()
$NewFolders = @()
$NewFolderHashArray = @()
#Use text document to set files or locations to be copied
$OriginalFilesLocation = Get-Content "OriginalFiles.txt"
$OriginalFiles = Get-Item $OriginalFilesLocation
#Hash Original Files
Foreach ($OriginalFile in $OriginalFiles)
{
$OriginalFileHash = Get-FileHash $OriginalFile | Select Path,Hash
$OriginalFilesHashArray += $OriginalFileHash
}
#Use array to copy files
Foreach ($OriginalFileHashArray in $OriginalFilesHashArray)
{
$CopiedFile = Copy-Item -Path $OriginalFileHashArray.Path -Destination '\\EndLocation\' -Force -PassThru -Container -Recurse
Write-Host "I've Just Copied $($CopiedFile)"
}
#Hash New File Location
$NewFiles = Get-Item "\\EndLocation\*"
Foreach ($NewFile in $NewFiles)
{
$NewFileHash = Get-FileHash $NewFile | Select Path,Hash | Sort Hash
$NewFilesHashArray += $NewFileHash
}
#Compare New and Old Location
$diff = Compare-Object -ReferenceObject $OriginalFilesHashArray.hash -DifferenceObject $NewFilesHashArray.hash
if($diff)
{
$wshell = New-Object -ComObject Wscript.Shell
$wshell.Popup("Files Don't Match, Please Troubleshoot",0,"Done",0x1)
}
else
{
$wshell = New-Object -ComObject Wscript.Shell
$wshell.Popup("File Copy Succesful",0,"Done",0x1)
}
#Use text document to set files or locations to be copied
$OriginalFolderLocations = Get-Content "OriginalFolders.txt"
#Hash Original Folders
Foreach ($OriginalFolderLocation in $OriginalFolderLocations)
{
$OriginalFolderHash = Get-FolderHash $OriginalFolderLocation | Out-String
$OriginalFoldersHashArray += $OriginalFolderHash
}
#Use array to copy files
Foreach ($OriginalFolderLocation in $OriginalFolderLocations)
{
$CopiedFolder = Copy-Item -Path $OriginalFolderLocation -Destination '\\EndLocation\' -Force -PassThru -Container -Recurse
Write-Host "I've Just Copied $($CopiedFolder)"
}
#Hash New File Location
$NewFolders = Get-ChildItem "\\EndLocation\*" -Directory
Foreach ($NewFolder in $NewFolders)
{
$NewFolderHash = Get-FolderHash $NewFolder
$NewFolderHashArray += $NewFolderHash | Out-String
}
#Compare New and Old Location
$diff = Compare-Object -ReferenceObject $OriginalFoldersHashArray -DifferenceObject $NewFolderHashArray
if($diff)
{
$wshell = New-Object -ComObject Wscript.Shell
$wshell.Popup("Folders Don't Match, Please Troubleshoot",0,"Done",0x1)
}
else
{
$wshell = New-Object -ComObject Wscript.Shell
$wshell.Popup("Folder Copy Succesful",0,"Done",0x1)
}
New-Item -ItemType directory -Path "\\EndLocation\Folder" -Force
Move-Item -Path "\\EndLocation\* SF Renewals.xlsx" -Destination "\\EndLocation\Folder" -Force