Last active
January 25, 2025 21:29
-
-
Save Rapidhands/3ab46822b776fc640a252bc45cbf2a5f to your computer and use it in GitHub Desktop.
Archive EventLog files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
param( | |
[Parameter(Mandatory = $false)] | |
[string]$SourcePath = "$env:windir\System32\winevt\Logs", | |
[Parameter(Mandatory = $false)] | |
[string]$DestinationPath = 'C:\temp2', | |
[Parameter(Mandatory = $false)] | |
[string]$FilePattern = '*.evtx', | |
[Parameter(Mandatory = $false)] | |
[string]$ZipName = "logs_$(Get-Date -Format 'yyyyMMdd_HHmmss').zip" | |
) | |
# Create destination folder if it does not exist | |
if (-not (Test-Path -Path $DestinationPath)) | |
{ | |
New-Item -ItemType Directory -Path $DestinationPath | Out-Null | |
Write-Host "The destination folder $DestinationPath has been created." | |
} | |
# Checking for existence of source files | |
$logFiles = Get-ChildItem -Path $SourcePath -Filter $FilePattern -File | |
if ($logFiles.Count -eq 0) | |
{ | |
Write-Warning "No log files found in $SourcePath" | |
exit | |
} | |
# Creating a temporary folder for copying | |
$tempFolder = Join-Path $env:TEMP "TempLogs_$(Get-Date -Format 'yyyyMMdd_HHmmss')" | |
New-Item -ItemType Directory -Path $tempFolder | Out-Null | |
try { | |
# Copying files to the temporary folder | |
foreach ($file in $logFiles) | |
{ | |
Copy-Item -Path $file.FullName -Destination $tempFolder | |
Write-Host "Copiy of $($file.Name)" | |
} | |
# Creating the .zip file | |
$zipPath = Join-Path -Path $DestinationPath -ChildPath $ZipName | |
Compress-Archive -Path "$tempFolder\*" -DestinationPath $zipPath -Force | |
Write-Host "Archive created successfully : $zipPath" | |
Write-Host "Number of files processed : $($logFiles.Count)" | |
} catch | |
{ | |
Write-Error "An error has occurred : $_" | |
} finally | |
{ | |
# Cleaning the temporary folder | |
if (Test-Path $tempFolder) | |
{ | |
Remove-Item -Path $tempFolder -Recurse -Force | |
Write-Host 'Cleaning of the temporary folder was completed.' | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment