Scope:
Procedure to do the following maintenance activities in any Windows Computer - This draft is to welcome more ideas like this and ideas to improve the same script as well.
Note:
Please run the script as System User
Tasks will be performed by the procedure:
Clears all Windows Temp Files
Clears all Internet Temp Files
Disk Cleaning
Defragmenting
Fixing Disk Errors (All Unremovable Disks)
Clear Printer Spool (Queuing Printer Jobs)
Clears DNS Cache
Releases IP
Renews IP
Clear Recycle Bin
Performs S.M.A.R.T. Check
Condition:
Once you have received the execution logs (in other words, have run the script) then you must have to restart your endpoint to take effect. To restart more or one computer from ITSM, follow the steps.
- Goto DEVICES > Device List
- Select the device/devices which you have run the script
- Click Reboot icon from the options given on the top of the device table
def downloadFile(DownTo, fromURL, Ext=None):
import urllib2
try:
fileName = fromURL.split('/')[-1]
if Ext:
DownTo = os.path.join(DownTo, fileName+Ext)
else:
DownTo = os.path.join(DownTo, fileName)
with open(DownTo, 'wb') as f:
f.write(urllib2.urlopen(fromURL).read())
if os.path.isfile(DownTo):
return (DownTo, os.path.getsize(DownTo)/1000)
except:
return 'Please Check %s or %s'%(fromURL, DownTo)
def ecmd(CMD, r=False):
import ctypes
class disable_file_system_redirection:
_disable = ctypes.windll.kernel32.Wow64DisableWow64FsRedirection
_revert = ctypes.windll.kernel32.Wow64RevertWow64FsRedirection
def __enter__(self):
self.old_value = ctypes.c_long()
self.success = self._disable(ctypes.byref(self.old_value))
def __exit__(self, type, value, traceback):
if self.success:
self._revert(self.old_value)
from subprocess import PIPE, Popen
with disable_file_system_redirection():
OBJ = Popen(CMD, shell = True, stdout = PIPE, stderr = PIPE)
out, err = OBJ.communicate()
ret=OBJ.returncode
if r:
return ret
else:
if ret==0:
return out
else:
return ret
def deleteall(path):
import shutil
for root, dirs, files in os.walk(path, topdown=False):
for name in files:
f=os.path.join(root,name)
try:
os.remove(f)
except:
pass
for name in dirs:
p=os.path.join(root,name)
try:
shutil.rmtree(p)
except:
pass
return '%s %s Cleared'%(path, str(20*'.'))
import os
import shutil
import zipfile
import _winreg
import random
import string
osdrive=os.environ['SYSTEMDRIVE']
i=0
fnlist=''
while True:
tmp=str(random.randint(0,10))+random.choice(list(string.ascii_lowercase))
fnlist+=tmp
if i==3:
break
i+=1
os_temp=os.path.join(osdrive, os.sep, fnlist)
ecmd('mkdir %s'%(os_temp))
##--------------------------------------------------
print '.. Clears all Windows Temp Files ..'
print deleteall(os.environ['TEMP'])
rootpath=os.path.join(osdrive, os.sep, 'Users')
for namedirs in os.listdir(rootpath):
if os.path.isdir(os.path.join(rootpath, namedirs)):
temp_path=os.path.join(rootpath, namedirs, 'AppData', 'Local', 'Temp')
if os.path.isdir(temp_path):
print deleteall(temp_path)
##--------------------------------------------------
print '
.. Clears all Internet Temp Files ..'
kill_firefox='taskkill /f /im firefox.exe'
kill_chrome='taskkill /f /im chrome.exe'
kill_ie='taskkill /f /im iexplore.exe'
try:
ecmd(kill_firefox)
ecmd(kill_chrome)
ecmd(kill_ie)
except:
pass
for namedirs in os.listdir(rootpath):
if os.path.isdir(os.path.join(rootpath, namedirs)):
chrome_path=os.path.join(rootpath, namedirs, 'AppData', 'Local', 'Google', 'Chrome', 'User Data', 'Default', 'Cache')
if os.path.isdir(chrome_path):
print deleteall(chrome_path)
firefox_path=os.path.join(rootpath, namedirs, 'AppData', 'Local', 'Mozilla', 'Firefox', 'Profiles')
if os.path.isdir(firefox_path):
print deleteall(firefox_path)
ie_cache="RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 8 "
ie=ecmd(ie_cache, True)
if ie==0:
print 'IE Cache %s Cleared'%(20*'.')
else:
print '%s %d Error on IE Cache'%(20*'.', ie)
##--------------------------------------------------
print '
.. Disk Cleaning ..'
dc=ecmd('cleanmgr /sagerun:64', True)
if dc==0:
print 'Disk %s Cleaned-up'%(20*'.')
else:
print '%s %d Error on Disk Cleans-up'%(20*'.', dc)
curl='https://www.piriform.com/ccleaner/download/portable/downloadfile'
fs=downloadFile(os_temp, curl, '.zip')
if type(fs) is tuple:
f,s=fs
z=zipfile.ZipFile(f, 'r')
try:
ep=os.path.join(os_temp, f.split(os.sep)[-1][:-4])
z.extractall(ep)
z.close()
os.remove(f)
if 'PROGRAMW6432' in os.environ.keys():
pp=os.path.join(ep, 'CCleaner64.exe')
else:
pp=os.path.join(ep, 'CCleaner.exe')
cc=ecmd('"%s" /auto'%pp, True)
if cc==0:
print 'System %s Cleaned-up'%(20*'.')
else:
print '%s %d Error on System Cleans-up'%(20*'.', cc)
shutil.rmtree(ep)
except Exception as e:
print e
else:
print fs
##--------------------------------------------------
print '
.. Defragmenting ..'
url='http://static.auslogics.com/en/disk-defrag/disk-defrag-setup.exe'
ps=downloadFile(os_temp, url)
if type(ps) is tuple:
p,s=downloadFile(os_temp, url)
else:
print ps
if ecmd('"%s" /SP- /SUPPRESSMSGBOXES /VERYSILENT'%p, True)==0:
ecmd('taskkill /f /im DiskDefrag.exe')
if 'PROGRAMW6432' in os.environ.keys():
command_path=os.path.join(os.environ['PROGRAMFILES(X86)'], 'Auslogics', 'Disk Defrag', 'cdefrag.exe')
uninstall_path=os.path.join(os.environ['PROGRAMFILES(X86)'], 'Auslogics', 'Disk Defrag', 'unins000.exe')
else:
command_path=os.path.join(os.environ['PROGRAMFILES'], 'Auslogics', 'Disk Defrag', 'cdefrag.exe')
uninstall_path=os.path.join(os.environ['PROGRAMFILES'], 'Auslogics', 'Disk Defrag', 'unins000.exe')
code={0:'Success', 1:'Error defragmenting one or more disks.', 2:'Administrator access rights are required to defragment disks.', 3:'The command line parameters are invalid.', 4:'Defragmentation was cancelled by user.', 5:'Unsupported Windows version.', 6:'Error creating log file.', 7:'Another instance is already running.', 8:'Low free space on the disk.', 9:'The computer has been turned off or rebooted.'}
print 'Defragmenting %s %s'%(20*'.', code[ecmd('"%s" -c -f'%command_path, True)])
ecmd('"%s" /VERYSILENT /SUPPRESSMSGBOXES'%uninstall_path)
pp=os.sep.join(command_path.split(os.sep)[:3])
if os.path.isdir(pp):
shutil.rmtree(pp)
os.remove(p)
##--------------------------------------------------
print '
.. Fixing Disk Errors (All Unremovable Disks) ..'
dl=ecmd('wmic logicaldisk where drivetype=3 get name')
dls=[i.strip() for i in dl.split('
') if i.strip()][1:]
df=ecmd('for %%i in (%s) do echo y | chkdsk %%i /f'%(' '.join(dls)), True)
if df==0 or df==3:
for i in dls:
if i==os.getenv('SYSTEMDRIVE'):
print 'Disk Error Fixing %s Will Start on Booting the Computer for %s (OS Drive)'%(20*'.', i)
else:
print 'Disk Error Fixing %s Completed for %s'%(20*'.', i)
else:
print '%s %d Error on Fixing Disk Errors'%(20*'.', df)
##--------------------------------------------------
print '
.. Clear Printer Spool (Queuing Printer Jobs) ..'
sp=ecmd('net stop spooler', True)
if sp==0:
cl=ecmd('del %systemroot%\System32\spool\printers\* /Q', True)
if cl==0:
print 'Printer Spool %s Cleared'%(20*'.')
else:
print '%s %d Error on Printer Spool Cleaning'%(20*'.', cl)
ecmd('net start spooler')
##--------------------------------------------------
df=ecmd('ipconfig /flushdns', True)
if df==0:
print '
DNS Cache %s Cleared'%(20*'.')
else:
print '
%s %d Error on DNS Cache Clearing'%(20*'.', df)
ir=ecmd('ipconfig /release', True)
if ir==0:
print 'IP %s Released'%(20*'.')
else:
print '%s %d Error on IP Release'%(20*'.', ir)
ire=ecmd('ipconfig /renew', True)
if ire==0:
print 'IP %s Renewed'%(20*'.')
else:
print '%s %d Error on IP Renew'%(20*'.', ire)
rc=ecmd('rd /s /q %systemdrive%\$Recycle.bin', True)
if rc==0:
print 'Recycle Bin %s Cleared'%(20*'.')
##--------------------------------------------------
if 'PROGRAMFILES(X86)' in os.environ.keys():
k=_winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, os.sep.join(['SOFTWARE', 'Microsoft', 'Windows NT', 'CurrentVersion', 'SystemRestore']), 0, _winreg.KEY_WOW64_64KEY|_winreg.KEY_ALL_ACCESS)
else:
k=_winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, os.sep.join(['SOFTWARE', 'Microsoft', 'Windows NT', 'CurrentVersion', 'SystemRestore']), 0, _winreg.KEY_ALL_ACCESS)
_winreg.SetValueEx(k, 'SystemRestorePointCreationFrequency', 0, 4, 0)
if ecmd('WMIC /Namespace:\\root\default Path SystemRestore Call CreateRestorePoint "BY COMODO ITSM %DATE% %TIME%", 100, 12', True)==0:
print ecmd('POWERSHELL Get-ComputerRestorePoint')
_winreg.DeleteValue(k, 'SystemRestorePointCreationFrequency')
print '
.. Smart Check for all Disk Drives ..'
print ecmd('wmic diskdrive get status')
shutil.rmtree(os_temp)
Sample Output:
Script in JSON format:
20170522-Maintenance.json
20170522-Maintenance.json (12.1 KB)