Advertisement
Laughing_Mantis

PatchClean.ps1 v1.0 by Greg Linares (@Laughing_Mantis)

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