1- <#
1+ <#
22. SYNOPSIS
33 Moves all Windows Event Log (.evtx) files from the default folder to a new target folder and updates registry paths.
44
@@ -66,18 +66,18 @@ if (-not (Test-Administrator)) {
6666}
6767
6868# --- Logging ---
69- $scriptName = [System.IO.Path ]::GetFileNameWithoutExtension($MyInvocation.MyCommand.Name )
70- $logDir = ' C:\Logs-TEMP'
69+ $scriptName = [System.IO.Path ]::GetFileNameWithoutExtension($MyInvocation.MyCommand.Name )
70+ $logDir = ' C:\Logs-TEMP'
7171$logFileName = " ${scriptName} _$ ( Get-Date - Format ' yyyyMMddHHmmss' ) .log"
72- $logPath = Join-Path $logDir $logFileName
72+ $logPath = Join-Path $logDir $logFileName
7373if (-not (Test-Path $logDir )) {
7474 try { New-Item - Path $logDir - ItemType Directory - Force | Out-Null } catch { Write-Error " Failed to create log directory: $logDir " ; exit }
7575}
7676function Write-Log {
7777 [CmdletBinding ()]
7878 param (
7979 [Parameter (Mandatory )][string ]$Message ,
80- [Parameter ()][ValidateSet (' INFO' , ' WARN' , ' ERROR' )] [string ]$Level = ' INFO'
80+ [Parameter ()][ValidateSet (' INFO' , ' WARN' , ' ERROR' )] [string ]$Level = ' INFO'
8181 )
8282 $timestamp = Get-Date - Format " yyyy-MM-dd HH:mm:ss"
8383 $logEntry = " [$timestamp ] [$Level ] $Message "
@@ -97,27 +97,27 @@ $DefaultLogsFolder = "$env:SystemRoot\System32\winevt\Logs"
9797
9898function Get-SafeName {
9999 param ([Parameter (Mandatory )][string ]$Name )
100- $n = $Name -replace ' %4' , ' -'
100+ $n = $Name -replace ' %4' , ' -'
101101 $invalid = ([IO.Path ]::GetInvalidFileNameChars() + [IO.Path ]::GetInvalidPathChars()) | Sort-Object - Unique
102- foreach ($c in $invalid ){ $n = $n -replace [Regex ]::Escape([string ]$c ), ' -' }
103- $n = ($n -replace ' [\s\-]+' , ' -' ).Trim().Trim(' .' ).Trim(' -' )
104- if ([string ]::IsNullOrWhiteSpace($n )){ $n = ' Log' }
102+ foreach ($c in $invalid ) { $n = $n -replace [Regex ]::Escape([string ]$c ), ' -' }
103+ $n = ($n -replace ' [\s\-]+' , ' -' ).Trim().Trim(' .' ).Trim(' -' )
104+ if ([string ]::IsNullOrWhiteSpace($n )) { $n = ' Log' }
105105 return $n
106106}
107107
108108function Files-Differ {
109- param ([Parameter (Mandatory )][string ]$A , [Parameter (Mandatory )][string ]$B )
109+ param ([Parameter (Mandatory )][string ]$A , [Parameter (Mandatory )][string ]$B )
110110 try {
111111 $fa = Get-Item - LiteralPath $A - ErrorAction Stop
112112 $fb = Get-Item - LiteralPath $B - ErrorAction Stop
113- if ($fa.Length -ne $fb.Length ){ return $true }
114- if ([Math ]::Abs(($fa.LastWriteTimeUtc - $fb.LastWriteTimeUtc ).TotalSeconds) -gt 2 ){ return $true }
113+ if ($fa.Length -ne $fb.Length ) { return $true }
114+ if ([Math ]::Abs(($fa.LastWriteTimeUtc - $fb.LastWriteTimeUtc ).TotalSeconds) -gt 2 ) { return $true }
115115 return $false
116116 } catch { return $true }
117117}
118118
119119function New-UniqueArchiveName {
120- param ([Parameter (Mandatory )][string ]$Dir , [Parameter (Mandatory )][string ]$Base )
120+ param ([Parameter (Mandatory )][string ]$Dir , [Parameter (Mandatory )][string ]$Base )
121121 do {
122122 $stamp = Get-Date - Format ' yyyyMMddHHmmssfff'
123123 $candidate = Join-Path $Dir (" {0}_{1}.evtx" -f $Base , $stamp )
@@ -146,7 +146,7 @@ function Snapshot-ServiceState {
146146
147147function Restore-ServiceState {
148148 foreach ($kvp in $Global :ServiceState.GetEnumerator ()) {
149- $name = $kvp.Key
149+ $name = $kvp.Key
150150 $state = $kvp.Value
151151 try {
152152 $svc = Get-Service - Name $name - ErrorAction Stop
@@ -270,15 +270,15 @@ function Move-EventLogs {
270270 }
271271
272272 # Initialize the progress bar on the UI thread.
273- $ProgressBar.Invoke ([System.Action ]{ $ProgressBar.Minimum = 0 })
274- $ProgressBar.Invoke ([System.Action ]{ $ProgressBar.Maximum = $logFiles.Count })
275- $ProgressBar.Invoke ([System.Action ]{ $ProgressBar.Value = 0 })
273+ $ProgressBar.Invoke ([System.Action ] { $ProgressBar.Minimum = 0 })
274+ $ProgressBar.Invoke ([System.Action ] { $ProgressBar.Maximum = $logFiles.Count })
275+ $ProgressBar.Invoke ([System.Action ] { $ProgressBar.Value = 0 })
276276 $i = 0
277277
278278 foreach ($logFile in $logFiles ) {
279279 try {
280280 # Sanitize folder and active filename: <Target>\<Base>\<Base>.evtx
281- $baseName = Get-SafeName - Name $logFile.BaseName
281+ $baseName = Get-SafeName - Name $logFile.BaseName
282282 $targetPath = Join-Path - Path $TargetFolder - ChildPath $baseName
283283
284284 # If the folder does not exist, create it and apply ACL from original logs folder.
@@ -308,7 +308,7 @@ function Move-EventLogs {
308308 Write-Log - Message " Archived previous active: $destinationFile -> $archive " - Level " INFO"
309309 } catch {
310310 Write-Log - Message " Failed to archive existing destination (locked?): $destinationFile . Skipping this log." - Level " WARN"
311- $i ++ ; $ProgressBar.Invoke ([System.Action ]{ $ProgressBar.Value = [Math ]::Min($i , $logFiles.Count ) }); continue
311+ $i ++ ; $ProgressBar.Invoke ([System.Action ] { $ProgressBar.Value = [Math ]::Min($i , $logFiles.Count ) }); continue
312312 }
313313 } else {
314314 Write-Log - Message " Active up-to-date: $destinationFile " - Level " INFO"
@@ -332,7 +332,7 @@ function Move-EventLogs {
332332 }
333333 finally {
334334 $i ++
335- $ProgressBar.Invoke ([System.Action ]{ $ProgressBar.Value = [Math ]::Min($i , $logFiles.Count ) })
335+ $ProgressBar.Invoke ([System.Action ] { $ProgressBar.Value = [Math ]::Min($i , $logFiles.Count ) })
336336 }
337337 }
338338
@@ -352,7 +352,7 @@ function Update-RegistryPaths {
352352 if ($fileProp -ne $null ) {
353353 $logName = $subKey.PSChildName
354354 # Build the new file location: <NewPath>\<logName>\<logName>.evtx
355- $sanLog = Get-SafeName - Name $logName
355+ $sanLog = Get-SafeName - Name $logName
356356 $newFolderPath = Join-Path - Path $NewPath - ChildPath $sanLog
357357 $newLogFilePath = Join-Path - Path $newFolderPath - ChildPath (" {0}.evtx" -f $sanLog )
358358
@@ -421,32 +421,32 @@ function Setup-GUI {
421421 $form.Controls.Add ($buttonClose )
422422
423423 $buttonMove.Add_Click ({
424- $targetFolder = $textBox.Text.Trim ()
425- if ([string ]::IsNullOrWhiteSpace($targetFolder )) {
426- [System.Windows.Forms.MessageBox ]::Show(" Please enter the target root folder." , " Input Error" , [System.Windows.Forms.MessageBoxButtons ]::OK, [System.Windows.Forms.MessageBoxIcon ]::Error) | Out-Null
427- Write-Log - Message " Error: Target root folder not entered." - Level " ERROR"
428- return
429- }
430- try {
431- $statusLabel.Text = " Stopping services (EventLog, dependents, DHCP)..."
432- Stop-For - Migration
424+ $targetFolder = $textBox.Text.Trim ()
425+ if ([string ]::IsNullOrWhiteSpace($targetFolder )) {
426+ [System.Windows.Forms.MessageBox ]::Show(" Please enter the target root folder." , " Input Error" , [System.Windows.Forms.MessageBoxButtons ]::OK, [System.Windows.Forms.MessageBoxIcon ]::Error) | Out-Null
427+ Write-Log - Message " Error: Target root folder not entered." - Level " ERROR"
428+ return
429+ }
430+ try {
431+ $statusLabel.Text = " Stopping services (EventLog, dependents, DHCP)..."
432+ Stop-For - Migration
433433
434- $statusLabel.Text = " Moving .evtx files..."
435- Move-EventLogs - TargetFolder $targetFolder - ProgressBar $progressBar
434+ $statusLabel.Text = " Moving .evtx files..."
435+ Move-EventLogs - TargetFolder $targetFolder - ProgressBar $progressBar
436436
437- $statusLabel.Text = " Updating registry (classic logs)..."
438- Update-RegistryPaths - NewPath $targetFolder
437+ $statusLabel.Text = " Updating registry (classic logs)..."
438+ Update-RegistryPaths - NewPath $targetFolder
439439
440- $statusLabel.Text = " Restoring services..."
441- Start-After - Migration
440+ $statusLabel.Text = " Restoring services..."
441+ Start-After - Migration
442442
443- # Ensure progress shows 100%
444- $progressBar.Value = $progressBar.Maximum
443+ # Ensure progress shows 100%
444+ $progressBar.Value = $progressBar.Maximum
445445
446- $buttonMove.Enabled = $false
447- $buttonClose.Enabled = $true
446+ $buttonMove.Enabled = $false
447+ $buttonClose.Enabled = $true
448448
449- $finalMsg = @"
449+ $finalMsg = @"
450450Event logs have been moved to:
451451 $targetFolder
452452
@@ -464,15 +464,15 @@ A reboot may be required for all changes to take effect.
464464 • Event log paths are valid and writable
465465 • 'L:\DHCP Server\' and 'L:\DHCP Server\Backup\' remain intact
466466"@
467- [System.Windows.Forms.MessageBox ]::Show($finalMsg , " Migration Completed" , [System.Windows.Forms.MessageBoxButtons ]::OK, [System.Windows.Forms.MessageBoxIcon ]::Information) | Out-Null
468- $statusLabel.Text = " Completed. You may close this window."
469- Write-Log - Message " Process finished with exit code 0. Target: $targetFolder " - Level " INFO"
470- Write-Log - Message " DHCP Server requires validation after migration (status, leases, reservations, log paths)." - Level " WARN"
471- }
472- catch {
473- Handle- Error - Message " An error occurred during the log moving process." - Exception $_
474- }
475- })
467+ [System.Windows.Forms.MessageBox ]::Show($finalMsg , " Migration Completed" , [System.Windows.Forms.MessageBoxButtons ]::OK, [System.Windows.Forms.MessageBoxIcon ]::Information) | Out-Null
468+ $statusLabel.Text = " Completed. You may close this window."
469+ Write-Log - Message " Process finished with exit code 0. Target: $targetFolder " - Level " INFO"
470+ Write-Log - Message " DHCP Server requires validation after migration (status, leases, reservations, log paths)." - Level " WARN"
471+ }
472+ catch {
473+ Handle- Error - Message " An error occurred during the log moving process." - Exception $_
474+ }
475+ })
476476
477477 $form.ShowDialog () | Out-Null
478478}
0 commit comments