Share via

Need help with looping a PS script

Mike Charpentier 1 Reputation point
2022-05-20T13:24:01.763+00:00

Hello, I have PS script that searches an entire host for an application version # that's < x.xx.x

Once found the script deletes the old file and replaces it with an updated on. However, on some hosts there are more than 1 file that needs to be replaced and I can't figure out how to keep this looping until all files have been found and replaced.

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
}
Windows for business | Windows Server | User experience | PowerShell
0 comments No comments

1 answer

Sort by: Most helpful
  1. Rich Matheisen 48,116 Reputation points
    2022-05-20T15:16:50.007+00:00

    I'm not sure if this is what you want, but give it a try:

    # 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
    
    # What happens if both C: and D: contain the file? Should D: be excluded ot combinded with results from C:????
    if (!$found) {
        $found = Get-ChildItem D:\ -Filter "log4j-core*.jar" -Recurse  -ErrorAction SilentlyContinue
    }
    
    if ($found) {
        foreach ($f in $found) {
            $destination = Split-Path $f.FullName  -Parent
            "Deleting vulnerable .jar files found in $destination" | Add-Content $LogFile
            try {
                $DeleteFile = $f.FullName
                if ($DeleteFile -ne $NewFile) {
                    Remove-Item $DeleteFile -Force
                    "SUCCESS: Successfully deleted files" | Add-Content $LogFile
                }
                else {
                    "NOACTION: $NewFile was found and ignored" | 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
    }
    

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.