5
5
# Author: sysadmin0815
6
6
# Date: 16.12.2021
7
7
# ############################
8
- # Mod. Date: 22 .12.2021
9
- $scriptVersion = " 1.5 "
8
+ # Mod. Date: 23 .12.2021
9
+ $scriptVersion = " 1.6 "
10
10
# Change Log:
11
11
# added additional if check to stop the process of bk file not found.
12
12
# added PSScriptRoot for 7zip by default
13
13
# added $enableBackup
14
14
# added $searchAllDrives
15
15
# added verifying process if jndilookup class was removed from jar file
16
+ # bugfix and code cleanup
16
17
#
17
18
# ############################
18
19
# THE SCRIPT IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND.
@@ -56,7 +57,8 @@ $enableBackup = $true
56
57
# if the class was NOT removed successfully, the jar file was NOT modified. So keeping the bk file is optional.
57
58
# --- set to $true to delete bk files if the class is still detected in the jar file (default)
58
59
# --- set to $false to keep bk files if the class is still detected in the jar file.
59
- $removeBkOnFailure = $true # set to $false if you are not sure
60
+ # will be ignored if $enableBackup is set to $false
61
+ $removeBkOnFailure = $true # set to $false if you are not sure
60
62
61
63
62
64
# -------------- SCRIPT START ---------------
@@ -74,6 +76,7 @@ $datelog = (Get-Date -Format "dd.MM.yyyy HH:mm:ss")
74
76
75
77
# Buid LogFolder and LogFile
76
78
$rootFolder = $PSScriptRoot + " \"
79
+ # $rootFolder = "C:\Windows\Logs\Custom\" #if you want the logs stored in a different folder, modify this line and comment the above one.
77
80
$logFileName = " Log4JCleanup.log"
78
81
$folderName = " Log"
79
82
$PathLogs = $rootFolder
@@ -147,10 +150,13 @@ else {
147
150
Add-Content - Path $PathToLogFile - Value " $datelog -- Files found matching pattern $filepattern "
148
151
$log4jFiles.FullName | Add-Content $PathToLogFile
149
152
153
+ # define temp file path for validating if jndilookup class was removed (will be deleted automatically)
150
154
$tmpFile = " $PSScriptRoot \log4jcleanupTmp.log"
151
155
156
+ # process files found matching pattern
152
157
foreach ($file in $log4jFiles ) {
153
158
try {
159
+ # if enableBackup is set to $true process this part
154
160
if ($enableBackup ) {
155
161
if (Test-Path - Path $tmpFile ) {
156
162
Remove-Item - Path $tmpFile - Force
@@ -170,6 +176,7 @@ else {
170
176
Write-Host " -- Checking if JNDILookup Class has been removed"
171
177
Start-Process - FilePath " $7zipPath " - ArgumentList " l `" $ ( $file.FullName ) `" org/apache/logging/log4j/core/lookup/JndiLookup.class" - NoNewWindow - Wait - RedirectStandardOutput " $tmpFile "
172
178
Add-Content - Path $PathToLogFile - Value " $datelog --- Checking if JNDILookup Class has been removed"
179
+ # check if jndilookup class was removed
173
180
$validate = Select-String - Path " $tmpOutFile " - Pattern " JndiLookup.class" - CaseSensitive - Quiet - SimpleMatch
174
181
if (! $validate ) {
175
182
Write-Host " -- Verified: File successully cleaned up." - ForegroundColor Green
@@ -191,6 +198,7 @@ else {
191
198
Remove-Item - Path $tmpFile - Force
192
199
}
193
200
}
201
+ # Error handling if backup file could not be ceated
194
202
else {
195
203
Write-Host " Error creating backup for file $file " - BackgroundColor Red - ForegroundColor Yellow
196
204
Write-Host " -- No changes perfmormed" - BackgroundColor Red - ForegroundColor Yellow
@@ -201,10 +209,33 @@ else {
201
209
}
202
210
}
203
211
204
- else {
205
- Add-Content - Path $PathToLogFile - Value " $datelog -- Backup disabled for file $file "
206
- Add-Content - Path $PathToLogFile - Value " $datelog --- Processing file $file "
207
- & $7zipPath d $file.fullname org/ apache/ logging/ log4j/ core/ lookup/ JndiLookup.class
212
+ # if enableBackup is set to $false, process this part
213
+ else {
214
+ if (Test-Path - Path $tmpFile ) {
215
+ Remove-Item - Path $tmpFile - Force
216
+ }
217
+ Write-Host " Processing file $file "
218
+ Write-Host " -- Backup DISABLED" - ForegroundColor Yellow
219
+ Add-Content - Path $PathToLogFile - Value " $datelog Backup disabled for file $file "
220
+ Add-Content - Path $PathToLogFile - Value " $datelog Processing file $file "
221
+ & $7zipPath d $file.fullname org/ apache/ logging/ log4j/ core/ lookup/ JndiLookup.class | Out-Null
222
+ Write-Host " -- Checking if JNDILookup Class has been removed"
223
+ Start-Process - FilePath " $7zipPath " - ArgumentList " l `" $ ( $file.FullName ) `" org/apache/logging/log4j/core/lookup/JndiLookup.class" - NoNewWindow - Wait - RedirectStandardOutput " $tmpFile "
224
+ Add-Content - Path $PathToLogFile - Value " $datelog --- Checking if JNDILookup Class has been removed"
225
+ $validate = Select-String - Path " $tmpOutFile " - Pattern " JndiLookup.class" - CaseSensitive - Quiet - SimpleMatch
226
+ # check if jndilookup class was removed
227
+ if (! $validate ) {
228
+ Write-Host " -- Verified: File successully cleaned up." - ForegroundColor Green
229
+ Add-Content - Path $PathToLogFile - Value " $datelog --- Verified: File successully cleaned up."
230
+ }
231
+ else {
232
+ Write-Host " -- Failure: Check whether you have write permissions or another process is currently using the file." - BackgroundColor Red - ForegroundColor Yellow
233
+ Add-Content - Path $PathToLogFile - Value " $datelog --- Failure: Check whether you have write permissions or another process is currently using the file."
234
+ $returnCode = 1
235
+ }
236
+ if (Test-Path - Path $tmpFile ) {
237
+ Remove-Item - Path $tmpFile - Force
238
+ }
208
239
}
209
240
210
241
}
0 commit comments