Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.80 KB | None | 0 0
  1. $ProgressPreference="SilentlyContinue"
  2. $ErrorActionPreference = "Continue"
  3.  
  4. for ([byte]$c = [char]'A'; $c -le [char]'Z'; $c++)
  5. {
  6. $variablePath = [char]$c + ':\variables.ps1'
  7.  
  8. if (test-path $variablePath) {
  9. . $variablePath
  10. break
  11. }
  12. }
  13.  
  14. # List installed updates
  15. Write-Host "Listing installed updates"
  16. Get-Hotfix
  17.  
  18. #Detect O/S and use appropriate Dism command
  19. if ($RunDism){
  20. $RunDismCount = 2
  21. While ($RunDismCount -gt 0) {
  22. try{
  23. $osName = (Get-WmiObject win32_operatingsystem).caption
  24.  
  25. switch -wildcard ($osName)
  26. {
  27. "*Server*2012 Standard" {Write-Host "Dism.exe /online /Cleanup-Image /StartComponentCleanup /scratchdir:c:\Windows\Temp"; Dism.exe /online /Cleanup-Image /StartComponentCleanup /scratchdir:c:\Windows\Temp}
  28. "*Server*2012 Datacenter" {Write-Host "Dism.exe /online /Cleanup-Image /StartComponentCleanup /scratchdir:c:\Windows\Temp"; Dism.exe /online /Cleanup-Image /StartComponentCleanup /scratchdir:c:\Windows\Temp}
  29. default {Write-Host "Dism.exe /online /Cleanup-Image /StartComponentCleanup /ResetBase"; Dism.exe /online /Cleanup-Image /StartComponentCleanup /ResetBase }
  30. }
  31. $RunDismCount = $RunDismCount - 1
  32. } catch {
  33. Write-Host "Unable to clean image."
  34. $RunDismCount = $RunDismCount - 1
  35. }
  36. }
  37. # List installed updates
  38. Write-Host "Listing installed updates"
  39. Get-Hotfix
  40. } else {
  41. Write-Host "Skipping Dism cleanup"
  42. }
  43.  
  44. if ($SkipCleanup){
  45. Write-Host "Skipping cleanup"
  46. exit 0
  47. }
  48.  
  49. # Remove 7-zip
  50. $version = $ver7zip
  51. $msi_file_name = "7z$version-x64.msi"
  52. $argumentList = '/x "C:\Windows\Temp\' + $msi_file_name + '" /qn'
  53.  
  54. Write-Host "Removing 7-zip if it exists"
  55. $process = Start-Process -FilePath "msiexec" -ArgumentList $argumentList -NoNewWindow -PassThru -Wait
  56. $process.ExitCode
  57.  
  58. #Clean up temp files
  59. Write-Host "Removing temp folders"
  60. $tempfolders = @("C:\Windows\Temp\*", "C:\Windows\Prefetch\*", "C:\Documents and Settings\*\Local Settings\temp\*", "C:\Users\*\Appdata\Local\Temp\*")
  61. Remove-Item $tempfolders -ErrorAction SilentlyContinue -Force -Recurse
  62.  
  63. #Sdelete - clean disk
  64. $wsusService = Get-Service -Name wuauserv
  65. $bitsService = Get-Service -Name bits
  66.  
  67. #stop bits,wuauserv services (required)
  68. if ($wsusService.Status -eq 'Running') {
  69. Write-Host "wsus is running"
  70. Stop-Service $wsusService}
  71. if ($bitsService.Status -eq 'Running') {
  72. Write-Host "bits is running"
  73. Stop-Service $bitsService}
  74.  
  75. Start-Sleep -s 10
  76.  
  77. #remove the contents only of software distribution directory
  78. Remove-Item -Recurse -Force C:\Windows\SoftwareDistribution\*
  79.  
  80. #accept license agreement
  81. New-Item -Path "HKCU:\Software\Sysinternals" -name Sdelete -Force
  82. # create reg path and key
  83. New-ItemProperty -Path "HKCU:\Software\Sysinternals\Sdelete" -Name "EulaAccepted" -Value "1" -PropertyType "Dword"
  84. # edit path/create/key with a value
  85.  
  86. #run sDelete with switches and wait to complete (runs for 5-10 mins)
  87. Start-process -filepath C:\sDelete\sdelete64.exe -ArgumentList "-q", "-z", "c:" -Wait
  88.  
  89. start-sleep -s 10
  90. #remove sdelete from the system
  91. Remove-Item -Recurse -Force C:\sdelete
  92. #it is not required to start wuauserv as it is set to manual and bits is set to auto
  93.  
  94. #Clean Windows Update Registry.
  95. #Delete Windows Update keys, look for AccountDomainSid, SusClientId and AUOptions specifically, if keys are found - delete.
  96. Write-Host "Clean Windows Update Registry"
  97. Try {
  98. Remove-Item -Path 'HKLM:\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate' -Recurse
  99. }
  100. Catch
  101. {
  102. Write-Host "WindowsUpdate registry path does not exist, nothing to delete."
  103. }
  104.  
  105. Try {
  106. Remove-ItemProperty -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate -Name AccountDomainSid -ErrorAction Stop
  107. Write-Host "AccountDomainSid registry key deleted."
  108. }
  109. Catch {
  110. Write-Host "AccountDomainSid registry key does not exist, nothing to delete."
  111. }
  112.  
  113. Try {
  114. Remove-ItemProperty -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate -Name SusClientId -ErrorAction Stop
  115. Write-Host "SusClientId registry key deleted."
  116. }
  117. Catch {
  118. Write-Host "SusClientId registry key does not exist, nothing to delete."
  119. }
  120.  
  121. Try {
  122. Remove-ItemProperty -Path HKLM:\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU -Name AUOptions -ErrorAction Stop
  123. Write-Host "AUOptions registry key deleted."
  124. }
  125. Catch {
  126. Write-Host "AUOptions registry key does not exist, nothing to delete."
  127. }
  128.  
  129. #Add default Windows Update keys
  130. REG ADD "HKLM\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate" /v DisableWindowsUpdateAccess /t REG_DWORD /d 00000001 /f
  131. REG ADD "HKLM\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate" /v ElevateNonAdmins /t REG_DWORD /d 00000001 /f
  132. REG ADD "HKLM\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU" /v AUOptions /t REG_DWORD /d 00000003 /f
  133.  
  134. #Clean Event Logs
  135. Write-Host "Clear Event Logs"
  136. wevtutil el | Foreach-Object {wevtutil cl "$_"}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement