cosine83

[Final] Deleting Temp Files on Remote PCs

Mar 6th, 2014
425
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.06 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. #AD Path to search for computers to clean
  7. $searchbase = "OU=derp,OU=derp,OU=derp,DC=derp,DC=com"
  8.  
  9. #Credentials to use for network traversal if not running from an admin account, will need to add -Credentials to appropriate commands
  10. #$credentials = Get-Credential -Message "Please provide administrator privileges" -UserName domain\username
  11.  
  12. #Set error display preference
  13. $ErrorActionPreference = "SilentlyContinue"
  14.  
  15. #Get the list of computers to clean
  16. $query = Get-ADComputer -Filter * -SearchBase $searchbase | Select Name | Sort Name
  17. $computers = $query.Name
  18.  
  19. #Variable array for folders to clean for WinRM method, others can be added as needed
  20. $tempRM = @("C:\Windows\Temp\*", "C:\Documents and Settings\*\Local Settings\temp\*", "C:\Users\*\Appdata\Local\Temp\*", "C:\Users\*\Appdata\Local\Microsoft\Windows\Temporary Internet Files\*")
  21.  
  22. #Set alias for PsExec to be able to be ran from PowerShell. Requires PsExec to on local machine,
  23. #Preferably in C:\Windows\system32 but where ever the executable is located should be fine
  24. #Download PsTools here: http://technet.microsoft.com/en-us/sysinternals/bb896649.aspx
  25. #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
  26.  
  27. #set-alias psexec "C:\Windows\system32\psexec.exe"
  28.  
  29. #Test to see if WinRM is enabled on remote computer
  30. #Copy and launch a batch file on remote computer using PsExec, if the test errors out
  31. #This can be time consuming if you have a lot of computers to enable WinRM on
  32. #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
  33. #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
  34.  
  35. <# foreach ($computer in $computers)
  36. {
  37. if(Test-WSMan $computer){
  38. Write-Host "WinRM is already enabled on $computer!"
  39. }
  40. else {
  41. & psexec -accepteula "\\$($computer)" -h -u administrator -p password -f -c "EnableWinRM.bat"
  42. }
  43. }
  44. #>
  45. #Operation to clear out temp files in specified folders
  46. #The WinRM method requires winrm quickconfig -q to be run on host machines first
  47. #The psexec command above should take care of enabling WinRM, including firewall exceptions
  48. #The UNC method requires access to admin shares, still need to figure out how to get it to fail over to it
  49.  
  50. foreach ($computer in $computers)
  51. {
  52. Write-Host "Connecting..."
  53. If(Test-Connection $computer -Count 1 -Quiet) #Check that PC is online
  54. {
  55. If(Test-WSMan $computer) #PC is online so check if WinRM enabled
  56. {
  57. #WinRM is enabled, delete temp files via Invoke-Command
  58. Write-Host "WinRM is enabled on $computer, proceeding..."
  59. Write-Host "$computer - Deleting files via WinRM!"
  60. Invoke-Command -ComputerName $computer -ScriptBlock { foreach ($folder in $using:tempRM) {Remove-Item -Path $folder -Recurse -Force} }
  61. Write-Host -ForegroundColor Green "$computer - Done!"
  62. }
  63. else
  64. {
  65. #WinRM not enabled, delete temp files via UNC path
  66. #UNC folder variable must be defined in the foreach loop or it will not populate computer names
  67. $tempUNC = @("\\$($computer)\c$\Windows\Temp\*", "\\$($computer)\c$\Documents and Settings\*\Local Settings\temp\*", "\\$($computer)\c$\Users\*\Appdata\Local\Temp\*", "\\$($computer)\c$\Users\*\Appdata\Local\Microsoft\Windows\Temporary Internet Files\*")
  68. Write-Host "WinRM is not enabled on $computer, proceeding..."
  69. Write-Host "$computer - Deleting files via UNC!"
  70. foreach ($folder in $tempUNC) {Remove-Item $folder -force -recurse}
  71. Write-Host -ForegroundColor Green "$computer - Done!"
  72. }#End inner If/Else
  73. }
  74. else
  75. {
  76. #PC was offline
  77. Write-Host -ForegroundColor Red "$computer - Offline!"
  78. }#End out If/Else
  79. }#End foreach loop
Advertisement
Add Comment
Please, Sign In to add comment