-
Notifications
You must be signed in to change notification settings - Fork 0
/
BlueWindowsTriage2.ps1
563 lines (517 loc) · 24.3 KB
/
BlueWindowsTriage2.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
# Parameterize the output directory and log file path
param(
[string]$outputDir = "C:\IncidentResponse\$(Get-Date -Format 'yyyyMMdd_HHmmss')"
)
# Ensure the script is running with administrative privileges
if (-not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
Write-Error "Please run this script as an Administrator."
exit
}
# Record the start time
$scriptStartTime = Get-Date
# Create the output directory
New-Item -ItemType Directory -Path $outputDir -Force | Out-Null
# Initialize the log file
$logFile = Join-Path $outputDir "script_log.txt"
Start-Transcript -Path $logFile -Append
# Initialize a mutex for synchronized logging
$logMutex = New-Object System.Threading.Mutex($false, "LogMutex")
# Global error logging function with batch processing
$global:errorList = [System.Collections.Concurrent.ConcurrentBag[string]]::new()
function Write-Output-Error {
param (
[string] $Message,
[string] $LogFile = (Join-Path $outputDir "error_log.txt")
)
$global:errorList.Add("$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss') ERROR: $Message")
if ($global:errorList.Count -gt 100) {
$logMutex.WaitOne() | Out-Null
try {
$global:errorList | Add-Content -Path $LogFile
$global:errorList = [System.Collections.Concurrent.ConcurrentBag[string]]::new()
} finally {
$logMutex.ReleaseMutex() | Out-Null
}
}
}
# Function to flush remaining errors
function Clear-ErrorLog {
if ($global:errorList.Count -gt 0) {
$logMutex.WaitOne() | Out-Null
try {
$global:errorList | Add-Content -Path (Join-Path $outputDir "error_log.txt")
$global:errorList = [System.Collections.Concurrent.ConcurrentBag[string]]::new()
} finally {
$logMutex.ReleaseMutex() | Out-Null
}
}
}
# Function to calculate file hash with error handling
function Get-FileHashSafely {
param(
[string]$FilePath,
[string]$Algorithm = 'SHA256'
)
try {
$hash = Get-FileHash -Path $FilePath -Algorithm $Algorithm -ErrorAction Stop
return $hash.Hash
} catch {
Write-Output-Error "Error calculating hash for file: $FilePath - $_"
return $null
}
}
# Function to export registry keys
function Export-RegistryKey {
param (
[string]$keyPath,
[string]$outputDir
)
try {
$sanitizedPath = ($keyPath -replace '\\', '_')
REG EXPORT $keyPath (Join-Path $outputDir "$sanitizedPath.reg") /y
} catch {
Write-Output-Error "Failed to export registry key: $keyPath. Error: $_"
}
}
# Function to safely wait for and remove jobs
function Wait-AndRemoveJobs {
param (
[Array]$JobsArray
)
foreach ($job in $JobsArray) {
$job | Wait-Job | Receive-Job
if (Get-Job -Id $job.Id -ErrorAction SilentlyContinue) {
Remove-Job -Id $job.Id
}
}
}
# Convert the Write-Output-Error function to a string
$WriteOutputErrorString = ${function:Write-Output-Error}.ToString()
# Detect Windows version
$os = Get-CimInstance -ClassName Win32_OperatingSystem
$osVersion = [Version]$os.Version
$valueBasedOnOS = switch ($osVersion.Major) {
10 { if ($osVersion.Minor -eq 0) { "Windows 10" } else { "Windows 11" } }
6 { switch ($osVersion.Minor) {
3 { "Windows 8.1" }
2 { "Windows 8" }
1 { "Windows 7" }
0 { "Windows Vista" }
}
}
5 { "Windows XP" }
default { "Unknown OS Version" }
}
# Output the value
"Windows Version: $valueBasedOnOS" | Add-Content -Path (Join-Path $outputDir "winver_log.txt")
# Create subdirectories
$subDirs = @("CE", "FF")
$subDirs | ForEach-Object {
New-Item -ItemType Directory -Path (Join-Path $outputDir $_) -ErrorAction SilentlyContinue
}
# Brave Browser Check
$braveInstalled = Test-Path "HKLM:\Software\BraveSoftware"
if ($braveInstalled) {
"Brave Browser is installed" | Add-Content -Path (Join-Path $outputDir "brave_log.txt")
} else {
"Brave Browser is not installed." | Add-Content -Path (Join-Path $outputDir "brave_log.txt")
}
# Function to copy items maintaining directory structure
function Copy-ItemWithHierarchy {
param (
[string]$source,
[string]$destination
)
Get-ChildItem -Path $source -Recurse | ForEach-Object {
$targetPath = Join-Path $destination $_.FullName.Substring($source.Length)
if ($_.PSIsContainer) {
New-Item -ItemType Directory -Path $targetPath -ErrorAction SilentlyContinue
} else {
Copy-Item -Path $_.FullName -Destination $targetPath -Force
}
}
}
# Brave Browser artifact collection
if ($braveInstalled) {
$bboutputDir = Join-Path $outputDir "BB"
$sourcePath = "C:\Users\*\AppData\Local\BraveSoftware\Brave-Browser\User Data\Default"
New-Item -ItemType Directory -Path $bboutputDir -ErrorAction SilentlyContinue
Copy-ItemWithHierarchy -source $sourcePath -destination $bboutputDir
"Brave Browser artifacts have been copied to $bboutputDir" | Add-Content -Path (Join-Path $outputDir "brave_log.txt")
}
# Core Parallel Processing
$jobs = @()
# Define job functions
$jobFunctions = @{
"SystemInfo" = {
param($outputDir, $WriteOutputErrorString)
# Recreate the Write-Output-Error function in the job's context
$WriteOutputErrorScriptBlock = [ScriptBlock]::Create($WriteOutputErrorString)
Set-Item -Path Function:\Write-Output-Error -Value $WriteOutputErrorScriptBlock
try {
$systemInfo = @{
"Hostname" = $env:COMPUTERNAME
"OS Version" = (Get-WmiObject -Class Win32_OperatingSystem).Caption
"Uptime" = (Get-Date) - (Get-CimInstance -ClassName Win32_OperatingSystem).LastBootUpTime
"Installed Software" = Get-WmiObject -Class Win32_Product | Select-Object Name, Version, InstallDate
"Running Processes" = Get-Process | Select-Object Name, ID, Path, @{Name="User";Expression={$_.GetOwner().User}}, @{Name="ExecutablePath";Expression={$_.Path}}
"Network Configuration" = Get-NetIPConfiguration
}
$systemInfo | ConvertTo-Json -Depth 4 | Out-File -FilePath (Join-Path $outputDir "SystemInfo.json")
} catch {
Write-Output-Error "Error collecting system information - $_"
}
}
"StartupItems" = {
param($outputDir, $WriteOutputErrorString)
# Recreate the Write-Output-Error function in the job's context
$WriteOutputErrorScriptBlock = [ScriptBlock]::Create($WriteOutputErrorString)
Set-Item -Path Function:\Write-Output-Error -Value $WriteOutputErrorScriptBlock
try {
$startupItems = Get-CimInstance -ClassName Win32_StartupCommand | Select-Object Command, Description, User, Location, Name
$startupItems | ConvertTo-Json | Out-File -FilePath (Join-Path $outputDir "StartupItems.json")
} catch {
Write-Output-Error "Error collecting startup items - $_"
}
}
"UserInfo" = {
param($outputDir, $WriteOutputErrorString)
# Recreate the Write-Output-Error function in the job's context
$WriteOutputErrorScriptBlock = [ScriptBlock]::Create($WriteOutputErrorString)
Set-Item -Path Function:\Write-Output-Error -Value $WriteOutputErrorScriptBlock
try {
$userInfo = @{
"Local Users" = Get-LocalUser | Select-Object Name, Enabled, LastLogon
"User Groups" = Get-LocalGroup | Select-Object Name, SID
"Recent User Accounts" = Get-LocalUser | Where-Object {$_.CreateDate -ge (Get-Date).AddDays(-7)} | Select-Object Name, CreateDate
}
$userInfo | ConvertTo-Json | Out-File -FilePath (Join-Path $outputDir "UserInfo.json")
} catch {
Write-Output-Error "Error collecting user and group information - $_"
}
}
}
# Start jobs
foreach ($jobName in $jobFunctions.Keys) {
$jobs += Start-Job -Name $jobName -ScriptBlock $jobFunctions[$jobName] -ArgumentList $outputDir, $WriteOutputErrorString
}
# Event Log Collection
$eventLogs = @("Application", "Security", "System")
foreach ($logName in $eventLogs) {
$jobs += Start-Job -Name "EventLog_$logName" -ScriptBlock {
param($outputDir, $logName)
# Recreate the Write-Output-Error function in the job's context
$WriteOutputErrorScriptBlock = [ScriptBlock]::Create($WriteOutputErrorString)
Set-Item -Path Function:\Write-Output-Error -Value $WriteOutputErrorScriptBlock
try {
$events = Get-WinEvent -LogName $logName -MaxEvents 1500 -ErrorAction Stop
if ($events -and $events.Count -gt 0) {
$events | Export-Clixml -Path (Join-Path $outputDir "${logName}_$(Get-Date -Format 'yyyyMMdd_HHmmss').xml")
} else {
Write-Output-Error "No events found in $logName log"
}
} catch {
Write-Output-Error "Failed to collect $logName event logs: $_"
}
} -ArgumentList $outputDir, $logName, $WriteOutputErrorString
}
# Network Connections
$jobs += Start-Job -Name "NetworkConnections" -ScriptBlock {
param($outputDir, $WriteOutputErrorString)
# Recreate the Write-Output-Error function in the job's context
$WriteOutputErrorScriptBlock = [ScriptBlock]::Create($WriteOutputErrorString)
Set-Item -Path Function:\Write-Output-Error -Value $WriteOutputErrorScriptBlock
try {
$networkConnections = Get-NetTCPConnection | Select-Object State, LocalAddress, LocalPort, RemoteAddress, RemotePort, OwningProcess
$networkConnections | Export-Csv -Path (Join-Path $outputDir "NetworkConnections.csv") -NoTypeInformation
} catch {
Write-Output-Error "Error collecting network connections - $_"
}
} -ArgumentList $outputDir
# Registry Startup Items
$jobs += Start-Job -Name "RegistryStartupItems" -ScriptBlock {
param($outputDir, $WriteOutputErrorString)
# Recreate the Write-Output-Error function in the job's context
$WriteOutputErrorScriptBlock = [ScriptBlock]::Create($WriteOutputErrorString)
Set-Item -Path Function:\Write-Output-Error -Value $WriteOutputErrorScriptBlock
try {
$registryKeys = @(
"HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Run",
"HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce",
"HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Run",
"HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce"
)
foreach ($key in $registryKeys) {
$keyName = ($key -split '\\')[-1]
$keyValues = Get-ItemProperty -Path $key -ErrorAction SilentlyContinue
$keyValues | ConvertTo-Json | Out-File -FilePath (Join-Path $outputDir "Registry_$keyName.json")
}
} catch {
Write-Output-Error "Error collecting registry data - $_"
}
} -ArgumentList $outputDir, $WriteOutputErrorString
# Shimcache Data
$jobs += Start-Job -Name "ShimcacheData" -ScriptBlock {
param($outputDir, $WriteOutputErrorString)
# Recreate the Write-Output-Error function in the job's context
$WriteOutputErrorScriptBlock = [ScriptBlock]::Create($WriteOutputErrorString)
Set-Item -Path Function:\Write-Output-Error -Value $WriteOutputErrorScriptBlock
try {
$shimcacheFile = Join-Path $outputDir "Shimcache.reg"
reg export "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\AppCompatCache" $shimcacheFile /y
} catch {
Write-Output-Error "Error collecting Shimcache data - $_"
}
} -ArgumentList $outputDir, $WriteOutputErrorString
# Browser Data Collection
$browserDataJobs = @(
@{
Name = "FirefoxData"
ScriptBlock = {
param($outputDir, $valueBasedOnOS, $WriteOutputErrorString)
# Recreate the Write-Output-Error function in the job's context
$WriteOutputErrorScriptBlock = [ScriptBlock]::Create($WriteOutputErrorString)
Set-Item -Path Function:\Write-Output-Error -Value $WriteOutputErrorScriptBlock
$ffPath = Join-Path $outputDir "FF"
$firefoxPaths = @(
"C:\Users\*\AppData\Roaming\Mozilla\Firefox\Profiles\*.default*\places.sqlite",
"C:\Users\*\AppData\Roaming\Mozilla\Firefox\Profiles\*.default*\bookmarkbackups\*.jsonlz4",
"C:\Users\*\AppData\Roaming\Mozilla\Firefox\Profiles\*\cookies.sqlite",
"C:\Users\*\AppData\Roaming\Mozilla\Firefox\Profiles\*\logins.json"
)
foreach ($path in $firefoxPaths) {
Get-ChildItem -Path $path -ErrorAction SilentlyContinue | ForEach-Object {
$destinationFile = Join-Path $ffPath ($_.Name + "_" + (Get-Random))
Copy-Item -Path $_.FullName -Destination $destinationFile -Force
}
}
}
},
@{
Name = "ChromeData"
ScriptBlock = {
param($outputDir, $valueBasedOnOS, $WriteOutputErrorString)
# Recreate the Write-Output-Error function in the job's context
$WriteOutputErrorScriptBlock = [ScriptBlock]::Create($WriteOutputErrorString)
Set-Item -Path Function:\Write-Output-Error -Value $WriteOutputErrorScriptBlock
$cePath = Join-Path $outputDir "CE"
$chromePaths = @(
"C:\Users\*\AppData\Local\Google\Chrome\User Data\Default\History",
"C:\Users\*\AppData\Local\Google\Chrome\User Data\Default\Bookmarks",
"C:\Users\*\AppData\Local\Google\Chrome\User Data\Default\Login Data",
"C:\Users\*\AppData\Local\Google\Chrome\User Data\Default\Web Data"
)
foreach ($path in $chromePaths) {
Get-ChildItem -Path $path -ErrorAction SilentlyContinue | ForEach-Object {
$destinationFile = Join-Path $cePath ($_.Name + "_" + (Get-Random))
Copy-Item -Path $_.FullName -Destination $destinationFile -Force
}
}
}
},
@{
Name = "EdgeData"
ScriptBlock = {
param($outputDir, $valueBasedOnOS, $WriteOutputErrorString)
# Recreate the Write-Output-Error function in the job's context
$WriteOutputErrorScriptBlock = [ScriptBlock]::Create($WriteOutputErrorString)
Set-Item -Path Function:\Write-Output-Error -Value $WriteOutputErrorScriptBlock
$cePath = Join-Path $outputDir "CE"
$edgePaths = @(
"C:\Users\*\AppData\Local\Microsoft\Edge\User Data\Default\History",
"C:\Users\*\AppData\Local\Microsoft\Edge\User Data\Default\Bookmarks",
"C:\Users\*\AppData\Local\Microsoft\Edge\User Data\Default\Login Data",
"C:\Users\*\AppData\Local\Microsoft\Edge\User Data\Default\Web Data"
)
foreach ($path in $edgePaths) {
Get-ChildItem -Path $path -ErrorAction SilentlyContinue | ForEach-Object {
$destinationFile = Join-Path $cePath ($_.Name + "_Edge_" + (Get-Random))
Copy-Item -Path $_.FullName -Destination $destinationFile -Force
}
}
}
}
)
foreach ($job in $browserDataJobs) {
$jobs += Start-Job -Name $job.Name -ScriptBlock $job.ScriptBlock -ArgumentList $outputDir, $valueBasedOnOS, $WriteOutputErrorString
}
# Wait for all jobs to complete
Wait-AndRemoveJobs -JobsArray $jobs
# Browser Extensions Collection
$extensionJobs = @(
@{
Name = "FirefoxExtensions"
ScriptBlock = {
param($outputDir, $WriteOutputErrorString)
# Recreate the Write-Output-Error function in the job's context
$WriteOutputErrorScriptBlock = [ScriptBlock]::Create($WriteOutputErrorString)
Set-Item -Path Function:\Write-Output-Error -Value $WriteOutputErrorScriptBlock
$firefoxExtensionsPath = "C:\Users\*\AppData\Roaming\Mozilla\Firefox\Profiles\"
$firefoxExtensionsPath = "C:\Users\*\AppData\Roaming\Mozilla\Firefox\Profiles\*.default*\extensions"
$firefoxExtensions = Get-ChildItem -Path $firefoxExtensionsPath -Recurse -Directory -ErrorAction SilentlyContinue
$firefoxExtensions | ForEach-Object {
$manifestPath = Join-Path $_.FullName "manifest.json"
if (Test-Path -Path $manifestPath) {
$extensionInfo = Get-Content -Path $manifestPath -Raw | ConvertFrom-Json
[PSCustomObject]@{
Id = $_.Name
Name = $extensionInfo.name
Version = $extensionInfo.version
Description = $extensionInfo.description
} | Out-File -FilePath (Join-Path $outputDir "FirefoxExtensions.txt") -Append
}
}
}
},
@{
Name = "ChromeEdgeExtensions"
ScriptBlock = {
param($outputDir, $WriteOutputErrorString)
# Recreate the Write-Output-Error function in the job's context
$WriteOutputErrorScriptBlock = [ScriptBlock]::Create($WriteOutputErrorString)
Set-Item -Path Function:\Write-Output-Error -Value $WriteOutputErrorScriptBlock
$browserPaths = @(
"C:\Users\*\AppData\Local\Google\Chrome\User Data\Default\Extensions\*\*",
"C:\Users\*\AppData\Local\Microsoft\Edge\User Data\Default\Extensions\*\*"
)
foreach ($path in $browserPaths) {
Get-ChildItem -Path $path -Recurse -Directory -ErrorAction SilentlyContinue | ForEach-Object {
$manifestPath = Join-Path $_.FullName "manifest.json"
if (Test-Path -Path $manifestPath) {
$extensionInfo = Get-Content -Path $manifestPath -Raw | ConvertFrom-Json
[PSCustomObject]@{
Id = $_.Name
Name = $extensionInfo.name
Version = $extensionInfo.version
Description = $extensionInfo.description
} | Out-File -FilePath (Join-Path $outputDir "ChromeEdgeExtensions.txt") -Append
}
}
}
}
}
)
foreach ($job in $extensionJobs) {
$jobs += Start-Job -Name $job.Name -ScriptBlock $job.ScriptBlock -ArgumentList $outputDir, $WriteOutputErrorString
}
# Additional artifact collection jobs
$artifactJobs = @(
@{
Name = "PasswordFiles"
ScriptBlock = {
param($outputDir, $WriteOutputErrorString)
# Recreate the Write-Output-Error function in the job's context
$WriteOutputErrorScriptBlock = [ScriptBlock]::Create($WriteOutputErrorString)
Set-Item -Path Function:\Write-Output-Error -Value $WriteOutputErrorScriptBlock
$passwordFiles = Get-ChildItem -Path "C:\Users\*\Documents\*password*" -Recurse -ErrorAction SilentlyContinue
if ($passwordFiles -and $passwordFiles.Count -gt 0) {
$passwordFiles | Export-Clixml -Path (Join-Path $outputDir "${logName}_$(Get-Date -Format 'yyyyMMdd_HHmmss').xml")
} else {
Write-Output-Error "No Passsword Files found in $logName log"
}
$passwordFiles | ForEach-Object {
Copy-Item -Path $_.FullName -Destination (Join-Path $outputDir "PasswordFiles") -Force
}
}
},
@{
Name = "PowerShellHistory"
ScriptBlock = {
param($outputDir, $WriteOutputErrorString)
# Recreate the Write-Output-Error function in the job's context
$WriteOutputErrorScriptBlock = [ScriptBlock]::Create($WriteOutputErrorString)
Set-Item -Path Function:\Write-Output-Error -Value $WriteOutputErrorScriptBlock
$powershellHistoryPath = "C:\Users\*\AppData\Roaming\Microsoft\Windows\PowerShell\PSReadLine\ConsoleHost_history.txt"
Get-ChildItem -Path $powershellHistoryPath -ErrorAction SilentlyContinue | ForEach-Object {
$destinationPath = Join-Path $outputDir $_.Directory.Name
New-Item -ItemType Directory -Path $destinationPath -Force | Out-Null
Copy-Item -Path $_.FullName -Destination $destinationPath -Force
}
}
},
@{
Name = "PrefetchFiles"
ScriptBlock = {
param($outputDir, $WriteOutputErrorString)
# Recreate the Write-Output-Error function in the job's context
$WriteOutputErrorScriptBlock = [ScriptBlock]::Create($WriteOutputErrorString)
Set-Item -Path Function:\Write-Output-Error -Value $WriteOutputErrorScriptBlock
$prefetchDir = Join-Path $outputDir "PreFetch"
New-Item -ItemType Directory -Path $prefetchDir -Force | Out-Null
Get-ChildItem -Path "C:\Windows\Prefetch" -ErrorAction SilentlyContinue | Copy-Item -Destination $prefetchDir -Force
}
},
@{
Name = "JumpLists"
ScriptBlock = {
param($outputDir, $WriteOutputErrorString)
# Recreate the Write-Output-Error function in the job's context
$WriteOutputErrorScriptBlock = [ScriptBlock]::Create($WriteOutputErrorString)
Set-Item -Path Function:\Write-Output-Error -Value $WriteOutputErrorScriptBlock
$jumpListFiles = Get-ChildItem -Path "C:\Users\*\AppData\Roaming\Microsoft\Windows\Recent\AutomaticDestinations" -ErrorAction SilentlyContinue
$jumpListFiles | Copy-Item -Destination (Join-Path $outputDir "JumpLists") -Force
}
}
)
foreach ($job in $artifactJobs) {
$jobs += Start-Job -Name $job.Name -ScriptBlock $job.ScriptBlock -ArgumentList $outputDir, $WriteOutputErrorString
}
# Wait for all jobs to complete
Wait-AndRemoveJobs -JobsArray $jobs
# Hashing of Collected Files
$hashingJob = Start-Job -ScriptBlock {
param($outputDir, $GetFileHashSafelyString, $WriteOutputErrorString)
# Recreate the functions in the job's context
$GetFileHashSafelyScriptBlock = [ScriptBlock]::Create($GetFileHashSafelyString)
Set-Item -Path Function:\Get-FileHashSafely -Value $GetFileHashSafelyScriptBlock
$WriteOutputErrorScriptBlock = [ScriptBlock]::Create($WriteOutputErrorString)
Set-Item -Path Function:\Write-Output-Error -Value $WriteOutputErrorScriptBlock
try {
$collectedFiles = Get-ChildItem -Path $outputDir -File -Recurse -ErrorAction Stop
$hashes = @()
foreach ($file in $collectedFiles) {
$hash = Get-FileHashSafely -FilePath $file.FullName
if ($hash) {
$hashes += [PSCustomObject]@{
FilePath = $file.FullName
Hash = $hash
}
}
}
if ($hashes.Count -gt 0) {
$hashes | Export-Csv -Path (Join-Path $outputDir "Hashes.csv") -NoTypeInformation
} else {
Write-Output-Error "No file hashes were generated"
}
} catch {
Write-Output-Error "Error during file hashing: $_"
}
} -ArgumentList $outputDir, ${function:Get-FileHashSafely}.ToString(), $WriteOutputErrorString
Wait-AndRemoveJobs -JobsArray @($hashingJob)
# Create backup
$parentDirectory = Split-Path -Path $outputDir -Parent
$tempFolderName = "Temp$(Get-Date -Format 'yyyyMMdd_HHmmss')"
$tempFolderPath = Join-Path $parentDirectory $tempFolderName
New-Item -ItemType Directory -Path $tempFolderPath -Force | Out-Null
# Copy files to temporary folder
Copy-ItemWithHierarchy -source $outputDir -destination $tempFolderPath
# Compress the temporary folder
$zipFileName = "IR-$(Get-Date -Format 'yyyyMMdd_HHmmss').zip"
$zipFilePath = Join-Path $parentDirectory $zipFileName
Compress-Archive -Path $tempFolderPath -DestinationPath $zipFilePath -CompressionLevel Optimal
# Check if the zip file was created successfully
if (Test-Path $zipFilePath) {
Remove-Item -Recurse -Force -Path $tempFolderPath
#"Backup created successfully: $zipFilePath" | Add-Content -Path (Join-Path $outputDir "script_log.txt")
} else {
Write-Output-Error "Zip file was not created."
}
# Stop logging
Stop-Transcript
# Calculate and log total script execution time
$scriptEndTime = Get-Date
$executionTime = $scriptEndTime - $scriptStartTime
$readableExecutionTime = "{0:dd} days {0:hh} hours {0:mm} minutes {0:ss} seconds" -f $executionTime
"Total script execution time: $readableExecutionTime" | Add-Content -Path (Join-Path $outputDir "script_log.txt")
# Final error log flush
Clear-ErrorLog