-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathGet-Files.psm1
147 lines (113 loc) · 5.11 KB
/
Get-Files.psm1
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
function Get-Files {
<#
.SYNOPSIS
Retreives files and provides a csv of the files with original metadata.
.DESCRIPTION
Retreives files and provides a csv of the files with original metadata.
.PARAMETER SourceList
Files and folder paths to retrieve, including support for wildcards.
.PARAMETER Destination
Place to store all files.
.EXAMPLE
Get-Files
.EXAMPLE
Get-Files |
Export-Csv -NoTypeInformation ("c:\temp\Files.csv")
.EXAMPLE
Invoke-Command -ComputerName remoteHost -ScriptBlock ${Function:Get-Files} |
Select-Object -Property * -ExcludeProperty PSComputerName,RunspaceID |
Export-Csv -NoTypeInformation ("c:\temp\Files.csv")
.EXAMPLE
$Targets = Get-ADComputer -filter * | Select -ExpandProperty Name
ForEach ($Target in $Targets) {
Invoke-Command -ComputerName $Target -ScriptBlock ${Function:Get-Files} |
Select-Object -Property * -ExcludeProperty PSComputerName,RunspaceID |
Export-Csv -NoTypeInformation ("c:\temp\" + $Target + "_Files.csv")
}
.NOTES
Updated: 2023-11-21
Contributing Authors:
Anthony Phipps
LEGAL: Copyright (C) 2023
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
.LINK
https://github.com/TonyPhipps
#>
[CmdletBinding()]
param(
[Parameter(ValueFromPipeline = $True, ValueFromPipelineByPropertyName = $True)]
[String[]]$SourceList = @(
"C:\Users\*\AppData\Roaming\Mozilla\Firefox\Profiles\*\places.sqlite",
"C:\Users\*\AppData\Roaming\Mozilla\Firefox\Profiles\*\formhistory.sqlite"
),
$Destination = "C:\Get-Files"
)
begin{
$DateScanned = ((Get-Date).ToUniversalTime()).ToString("yyyy-MM-dd hh:mm:ssZ")
Write-Information -InformationAction Continue -MessageData ("Started Get-Files at {0}" -f $DateScanned)
$stopwatch = New-Object System.Diagnostics.Stopwatch
$stopwatch.Start()
}
process{
# Prepare destination Folder
New-Item -ItemType Directory $Destination -ErrorAction SilentlyContinue
$ResolvedPaths = ForEach ($Path in [String[]]$SourceList) {
if ($Path -match '\*') {
$Paths = (Resolve-Path -Path $Path).Path
if ($Paths.Count -gt 0){
(Resolve-Path -Path $Path).Path
}
}
else{
$Path
}
}
$ResolvedFiles = ForEach ($Path in $ResolvedPaths) {
if ((Test-Path $Path -PathType Container) -and (Get-ChildItem -Path $Path -File -Recurse).Count -gt 0){
(Get-ChildItem -Path $Path -File -Recurse).FullName
}
else {
$Path
}
}
$ResultsArray = ForEach ($File in $ResolvedFiles) {
$PathSplit = Split-Path (Split-Path $File -NoQualifier) -Parent
New-Item -ItemType Directory ($Destination + $PathSplit) -ErrorAction SilentlyContinue | Out-Null
Copy-Item -Path $File -Destination ($Destination + $PathSplit) -Force
# Remove Empty Directories Recursively
Get-ChildItem $Destination -Directory -Recurse |
Foreach-Object { $_.FullName} |
Sort-Object -Descending |
Where-Object { !@(Get-ChildItem -force $_) } |
Remove-Item
try{
$FileHash = (Get-FileHash -Path $File -ErrorAction Stop).Hash
}
catch{
$FileHash = " "
}
$fileObject = Get-Item -Path $File
$fileObject | Add-Member -MemberType NoteProperty -Name "Hash" -Value $FileHash -Force
$fileObject
}
foreach ($Result in $ResultsArray) {
$Result | Add-Member -MemberType NoteProperty -Name "Host" -Value $env:COMPUTERNAME
$Result | Add-Member -MemberType NoteProperty -Name "DateScanned" -Value $DateScanned
}
$ResultsArray | Select-Object Host, DateScanned, FullName, Mode, Length, Hash, LastWriteTimeUTC, LastAccessTimeUTC, creationTimeUtc | Export-Csv -NoTypeInformation ("$Destination\Files.csv") -Append
}
end{
$elapsed = $stopwatch.Elapsed
Write-Verbose ("Total time elapsed: {0}" -f $elapsed)
Write-Verbose ("Ended at {0}" -f ((Get-Date).ToUniversalTime()).ToString("yyyy-MM-dd hh:mm:ssZ"))
}
}