Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #PowerShell script to clean out temporary folders
- #Load Active Directory PowerShell Module
- Import-Module activedirectory
- #Variable array for folders to clean, others can be added as needed
- $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\*")
- #$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\*")
- #AD Path to search for computers to clean
- $searchbase = "OU=derp,OU=derp,OU=derp,DC=derp,DC=com"
- #Credentials to use for network traversal if not running from an admin account, will need to add -Credentials to appropriate commands
- #$credentials = Get-Credential -Message "Please provide administrator privileges" -UserName domain\username
- #Set error display preference
- $ErrorActionPreference = "SilentlyContinue"
- #Get the list of computers to clean
- $query = Get-ADComputer -Filter * -SearchBase $searchbase | Select DNSHostName | Sort DNSHostName
- $computers = $query.DNSHostName
- #Set alias for PsExec to be able to be ran from PowerShell. Requires PsExec to on local machine,
- #Preferably in C:\Windows\system32 but where ever the executable is located should be fine
- #Download PsTools here: http://technet.microsoft.com/en-us/sysinternals/bb896649.aspx
- #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
- set-alias psexec "C:\Windows\system32\psexec.exe"
- #Test to see if WinRM is enabled on remote computer
- #Copy and launch a batch file on remote computer using PsExec, if the test errors out
- #This can be time consuming if you have a lot of computers to enable WinRM on
- #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
- #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
- foreach ($computer in $computers)
- {
- if(Test-WSMan $computer){
- Write-Host "WinRM is enabled on $computer!"
- }
- else {
- & psexec -accepteula "\\$($computer)" -h -u administrator -p password -f -c "EnableWinRM.bat"
- }
- }
- #Operation to clear out temp files in specified folders
- #The WinRM method requires winrm quickconfig -q to be run on host machines first
- #The psexec command above should take care of enabling WinRM, including firewall exceptions
- #The UNC method requires access to admin shares, still need to figure out how to get it to fail over to it
- foreach ($computer in $computers)
- {
- Write-Host "$computer - Connecting..."
- if(Test-Connection $computer -Count 1 -Quiet)
- {
- Write-Host -ForegroundColor Green "$computer - Deleting files!"
- Invoke-Command -ComputerName $computer -ScriptBlock { foreach ($folder in $using:tempRM) {Remove-Item -Path $folder -Recurse -Force} }
- Write-Host -ForegroundColor Green "$computer - Done!"
- }
- <# elseif(Test-WSMan $computer)
- {
- Write-Host -ForegroundColor Green "$computer - Deleting files via UNC!"
- Remove-Item -path $tempUNC -force -recurse
- Write-Host -ForegroundColor Green "$computer - Done!"
- } #>
- else
- {
- Write-Host -ForegroundColor Red "$computer - Offline!"
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement