Advertisement
gavzik

Untitled

Oct 25th, 2021
1,324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#
  2. ================
  3. PATCHCLEAN.PS1
  4. =================
  5. Version 1.0 Patch Folder Cleaner by Greg Linares (@Laughing_Mantis)
  6.  
  7. This Tool will go through the patch folders created by PatchExtract.PS1 and look for files created older
  8. than 30 days prior to the current date and move these to a sub folder named "OLD" in the patch folders.
  9.  
  10. This will help identify higher priority binaries that were likely updated in the current patch cycle window.
  11.  
  12. =======    
  13. USAGE
  14. =======
  15. Powershell -ExecutionPolicy Bypass -File PatchClean.ps1 -Path C:\Patches\MS16-121\x86\
  16.  
  17. This would go through the x86 folder and create a subfolder named C:\Patches\MS16-121\x86\OLD\ and place
  18. older files and their folders in that directory.
  19.  
  20. Files remaining in C:\Patches\MS16-121\x86\ should be considered likely targets for containing patched binaries
  21.  
  22. Empty folders are automatically cleaned and removed at the end of processing.
  23.  
  24. -PATH <STRING:FolderPath> [REQUIRED] [NO DEFAULT]
  25.     Specified the folder that the script will parse and look for older files
  26.  
  27.  
  28. ================
  29. VERSION HISTORY
  30. ================
  31.  
  32. Oct 20, 2016 - Version 1 - Initial Release
  33.  
  34.  
  35. ==========
  36. LICENSING
  37. ==========
  38. This script is provided free as beer.  It probably has some bugs and coding issues, however if you like it or find it
  39. useful please give me a shout out on twitter @Laughing_Mantis.  Feedback is encouraged and I will be likely releasing
  40. new scripts and tools and training in the future if it is welcome.
  41.  
  42.  
  43. -GLin
  44.  
  45. #>
  46.  
  47. Param
  48. (
  49.  
  50.     [Parameter(ValueFromPipelineByPropertyName = $true)]
  51.     [ValidateNotNullOrEmpty()]
  52.     [string]$PATH = ""
  53. )
  54.  
  55.  
  56. Clear-Host
  57.  
  58. if ($PATH -eq "")
  59. {
  60.     Throw ("Error: No PATH specified.  Specify a valid folder containing extracted patch files required. Generated by PatchExtract.ps1 ")
  61.    
  62. }
  63.  
  64. if (!(Test-Path $PATH))
  65. {
  66.     Throw ("Error: Invalid PATH specified '$PATH' does not exist.")
  67. }
  68.  
  69. $OldDir = Join-Path -path $PATH -ChildPath "OLD"
  70.  
  71. if (!(Test-Path $OldDir -pathType Container))
  72. {
  73.     New-Item $OldDir -Force -ItemType Directory
  74.     Write-Host "Making $OldDir Folder" -ForegroundColor Green
  75. }
  76.  
  77. $FolderCount = 0
  78. $FileCount = 0
  79. $OldFiles = Get-ChildItem -Path $PATH -Recurse -File -Force -ErrorAction SilentlyContinue | Where{$_.LastWriteTime -lt (Get-Date).AddDays(-30)}
  80.  
  81.  
  82. foreach ($OldFile in $OldFiles)
  83. {
  84.     try
  85.     {
  86.         $FileCount++
  87.         $fileDir = (Get-Item($OldFile).DirectoryName)
  88.         $folderName = (Get-Item $fileDir ).Basename
  89.         $MoveDir = JOIN-Path -path $OldDir -ChildPath $folderName
  90.         if (!(Test-Path $movedir))
  91.         {
  92.             Write-Host "Creating $folderName to $OldDir" -ForegroundColor Green
  93.             New-Item $MoveDir -Force -ItemType Directory
  94.             $FolderCount++
  95.         }
  96.         Move-Item $OldFile.fullname $MoveDir -Force
  97.  
  98.     }
  99.     catch
  100.     {
  101.         Write-Host ("Error Processing " + $OldFile.fullname) -ForegroundColor Red
  102.         Write-Host $_.Exception.Message
  103.         Write-Host $_.Exception.ItemName
  104.     }
  105. }
  106.  
  107. #Clean Up Empty Folders
  108.  
  109. $EmptyFolders = Get-ChildItem -Path $PATH  -Recurse| Where-Object {$_.PSIsContainer -eq $True} | Where-Object {$_.GetFiles().Count -eq 0 -and $_.GetDirectories().Count -eq 0 } | Select-Object FullName
  110.  
  111.  
  112. foreach ($EmptyFolder in $EmptyFolders)
  113. {
  114.     try
  115.     {
  116.         Write-Host ("Removing Empty Folder: " + $EmptyFolder.FullName) -ForegroundColor Yellow
  117.         Remove-Item -Path $EmptyFolder.FullName -Force
  118.     }
  119.     catch
  120.     {
  121.         Write-Host ("Error Removing: " + $EmptyFolder.Fullname) -ForegroundColor Red
  122.     }
  123. }
  124.  
  125. Write-Host "=========================================================="
  126.  
  127. Write-Host "High-Priority Folders within $PATH :"
  128.  
  129. $NewFolders = Get-ChildItem -Path $PATH -Directory
  130. $HighCount = 0
  131.  
  132. foreach ($folderName in $NewFolders)
  133. {
  134.     if (!($folderName -like "OLD"))
  135.     {
  136.         Write-Host $folderName
  137.         $HighCount++
  138.     }
  139.  
  140. }
  141.  
  142. Write-Host "=========================================================="
  143.  
  144. Write-Host ("Low Priority Folders: " + $FolderCount)
  145. Write-Host ("Low Priority Files: " + $FileCount)
  146. Write-Host ("High Priority Folders: " + $HighCount)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement