Advertisement
cosine83

Delete temp files

Mar 4th, 2014
346
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.56 KB | None | 0 0
  1. #PowerShell script to clean out temporary folders
  2.  
  3. #Load Active Directory PowerShell Module
  4. Import-Module activedirectory
  5.  
  6. #Variable array for folders to clean, others can be added as needed
  7. $tempRM = @("C:\Windows\Temp\*", "C:\Windows\Prefetch\*", "C:\Documents and Settings\*\Local Settings\temp\*", "C:\Users\*\Appdata\Local\Temp\*", "C:\Users\*\Appdata\Local\Microsoft\Windows\Temporary Internet Files\*")
  8. #$tempUNC = @("\\$computer\c$\Windows\Temp\*", "\\$computer\c$\Windows\Prefetch\*", "\\$computer\c$\Documents and Settings\*\Local Settings\temp\*", "\\$computer\c$\Users\*\Appdata\Local\Temp\*", "\\$computer\c$\Users\*\Appdata\Local\Microsoft\Windows\Temporary Internet Files\*")
  9.  
  10. #AD Path to search for computers to clean
  11. $searchbase = "OU=derp,OU=derp,OU=derp,DC=derp,DC=com"
  12.  
  13. #Credentials to use for network traversal if not running from an admin account, will need to add -Credentials to appropriate commands
  14. #$credentials = Get-Credential -Message "Please provide administrator privileges" -UserName domain\username
  15.  
  16. #Set error display preference
  17. $ErrorActionPreference = "SilentlyContinue"
  18.  
  19. #Get the list of computers to clean
  20. $query = Get-ADComputer -Filter * -SearchBase $searchbase | Select DNSHostName | Sort DNSHostName
  21. $computers = $query.DNSHostName
  22.  
  23. #Set alias for PsExec to be able to be ran from PowerShell. Requires PsExec to on local machine,
  24. #Preferably in C:\Windows\system32 but where ever the executable is located should be fine
  25. #Download PsTools here: http://technet.microsoft.com/en-us/sysinternals/bb896649.aspx
  26. #Note that it requires an EULA to be accepted on first run on the local machine but has an -acceptEula switch to get around user input
  27.  
  28. set-alias psexec "C:\Windows\system32\psexec.exe"
  29.  
  30. #Test to see if WinRM is enabled on remote computer
  31. #Copy and launch a batch file on remote computer using PsExec, if the test errors out
  32. #This can be time consuming if you have a lot of computers to enable WinRM on
  33. #In effect, this only really has to be done once per computer so this section can be commented out or cut into another script and used as needed
  34. #SSL would be preferred for WinRM for security reasons but it requires setting up a non-self-signed certificate, which would be cumbersome for some to setup
  35.  
  36. foreach ($computer in $computers)
  37. {
  38. if(Test-WSMan $computer){
  39. Write-Host "WinRM is enabled on $computer!"
  40. }
  41. else {
  42. & psexec -accepteula "\\$($computer)" -h -u administrator -p password -f -c "EnableWinRM.bat"
  43. }
  44. }
  45.  
  46. #Operation to clear out temp files in specified folders
  47. #The WinRM method requires winrm quickconfig -q to be run on host machines first
  48. #The psexec command above should take care of enabling WinRM, including firewall exceptions
  49. #The UNC method requires access to admin shares, still need to figure out how to get it to fail over to it
  50.  
  51. foreach ($computer in $computers)
  52. {
  53. Write-Host "$computer - Connecting..."
  54. if(Test-Connection $computer -Count 1 -Quiet)
  55. {
  56. Write-Host -ForegroundColor Green "$computer - Deleting files!"
  57. Invoke-Command -ComputerName $computer -ScriptBlock { foreach ($folder in $using:tempRM) {Remove-Item -Path $folder -Recurse -Force} }
  58. Write-Host -ForegroundColor Green "$computer - Done!"
  59. }
  60. <# elseif(Test-WSMan $computer)
  61. {
  62. Write-Host -ForegroundColor Green "$computer - Deleting files via UNC!"
  63. Remove-Item -path $tempUNC -force -recurse
  64. Write-Host -ForegroundColor Green "$computer - Done!"
  65. } #>
  66. else
  67. {
  68. Write-Host -ForegroundColor Red "$computer - Offline!"
  69. }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement