I have a script that searches the entire host for a specific application version # that’s < x.xx.x.
Once found the script deletes the file and replaces it with an updated on. The script I have works great but it stops running after replacing 1 file. Some hosts have more then 1 file that need to be replaced. Can someone help me figure out how to keep my script running until all files have been replaced with the new one?
Script:
# Path of New file to replace with
$NewFile = "$PSscriptRoot\log4j-core-2.17.1.jar"
$LogFile = "$PSscriptRoot\Log.txt"
# Default Path to copy files over
$destination = "c:\"
# Find path of log4j...jar files
$found = Get-ChildItem c:\ -Filter "log4j-core*.jar" -Recurse -ErrorAction SilentlyContinue
if(!$found) {
$found = Get-ChildItem D:\ -Filter "log4j-core*.jar" -Recurse -ErrorAction SilentlyContinue
}
if($found)
{
$destination = split-path $found[0].FullName -Parent
"Deleting vulnerable .jar files found in $destination" | add-content $LogFile
try {
$found | % { $DeleteFile = $_.FullName
if($DeleteFile -ne $NewFile)
{
Remove-Item $DeleteFile -Force
}
}
"SUCCESS: Successfully deleted files" | Add-Content $LogFile
}
catch
{
"ERROR: Failed to delete files [$($Error[0].Exception.Message)]" | Add-Content $LogFile
}
} else {
"Vulnerable log4j .jar files Not Found" | add-content $LogFile
}
"Copying over new file to $destination" | Add-Content $LogFile
try {
copy-item $NewFile $destination
"SUCCESS: Successfully Copied New File to $destination" | Add-Content $LogFile
}
catch{
"ERROR: Failed to copy New File in $destination" | Add-Content $LogFile
}