Jump to content



Photo

Disk Maintenance


  • Please log in to reply
3 replies to this topic

#1 JLogan3o13

JLogan3o13

    Down to 98

  • MVPs
  • 2,085 posts

Posted 27 December 2011 - 04:08 PM

I've been toying with the idea of a Disk Maintenance app for some time; something for ease administration of a large number of machines, particularly VMs in my case. For my test VMs, I run this maintenance routine once a month and then take a new snapshot, but I could see uses elsewhere. Below is the logic, as well as the source. I'd be grateful for any constructive criticism.

Caveats:
  • Only works for WinXP right now, building support for Win7
  • Must manually select updates
Tasks:
  • Call MS Updates (continue other portions of script if no updates)
  • Reboot and pick up where script left off
  • Call MS Updates again to finish any missed updates
  • Reboot a second time if necessary or call remainder of script if no further updates
  • Run Disk Cleanup and Defrag
  • Clear Event Viewer
  • Clear Temp Files Directory
  • Alert that script is complete
Plain Text         
#NoTrayIcon #include <File.au3> #include <Array.au3> #include <EventLog.au3> #include <ie.au3> Select   Case $CmdLine[0] = 0    _updates()   Case $CmdLine[1] = "/?"    _help()   Case $CmdLine[1] = "/r1"    _r1()   Case $CmdLine[1] = "/f1"    _f1()   Case $CmdLine[1] = "/r2"    _r2()   Case $CmdLine[1] = "/f2"    _f2() EndSelect Func _updates() $ie = "<a href='http://www.update.microsoft.com/microsoftupdate/v6/default.aspx?ln=en-us' class='bbc_url' title='External link' rel='norewrite nofollow external'>http://www.update.microsoft.com/microsoftupdate/v6/default.aspx?ln=en-us"</a> ShellExecuteWait("sc.exe", 'config "bits" start= auto', "", "", @SW_HIDE) ShellExecuteWait("sc.exe", 'config "wuauserv" start= auto', "", "", @SW_HIDE) RunWait('net start "bits"', "", @SW_HIDE) RunWait('net start "wuauserv"', "", @SW_HIDE) $wsus = _IECreate($ie) WinSetState("Microsoft Update", "", @SW_MAXIMIZE) While Winexists("Microsoft Update - ")    Select     Case WinExists("Installing Updates", "Some updates were not installed")     _failure()     Case WinExists("Installing Updates", "Installation complete")     _updateReboot()    Case Not WinExists("Microsoft Update - ")     _r2()    EndSelect WEnd EndFunc Func _updates2() $ie = "<a href='http://www.update.microsoft.com/microsoftupdate/v6/default.aspx?ln=en-us' class='bbc_url' title='External link' rel='norewrite nofollow external'>http://www.update.microsoft.com/microsoftupdate/v6/default.aspx?ln=en-us"</a> $wsus = _IECreate($ie) WinSetState("Microsoft Update", "", @SW_MAXIMIZE) While Winexists("Microsoft Update - ")    Select     Case WinExists("Installing Updates", "Some updates were not installed")     _failure2()     Case WinExists("Installing Updates", "Installation complete")     _updateReboot2()    Case Not WinExists("Microsoft Update - ")     _r2()   EndSelect WEnd EndFunc Func _cleanup() ShellExecuteWait("CleanMgr.exe", "/sagerun:1", "", "", @SW_HIDE) While ProcessExists("CleanMgr.exe")   Sleep(30000) WEnd ShellExecuteWait("C:WindowsSystem32defrag.exe", "c: -f", "", "", @SW_HIDE) EndFunc Func _clearEvents() Local $logs[3] $logs[0] = _EventLog__Open("", "Application") $logs[1] = _EventLog__Open("", "Security") $logs[2] = _EventLog__Open("", "System") For $element In $logs   _EventLog__Clear($element, "")   _EventLog__Close($element) Next EndFunc Func _deleteTemp() $_dir1 = _FileListToArray(@TempDir, "*", 2) $_dir2 = _FileListToArray(@TempDir, "*", 1) If IsArray($_dir1) Then   For $n = 1 To $_dir1[0]    $sPath = @TempDir & "" & $_dir1[$n]    If FileExists($sPath & "") Then     DirRemove($sPath, 1)    EndIf   Next EndIf If IsArray($_dir2) Then   For $n = 1 To $_dir2[0]    $sPath = "C:Temp" & $_dir2[$n]    If FileExists($sPath) Then     FileDelete($sPath)    EndIf   Next EndIf EndFunc Func _finish() MsgBox(0, "Disk Maintenance Complete", "Disk Maintenance for " & @ComputerName & " is complete.") EndFunc Func _failure() WinClose("Installing Updates") WinClose("Microsoft Update - ") RegWrite("HKLMSOFTWAREMicrosoftWindowsCurrentVersionRun", "R2", "REG_SZ", '"' & @ScriptFullPath & '" /f1') MsgBox(0, "Not all Updates Completed", "Some updates cannot be installed until after a restart. Click OK to reboot and run Microsoft Updates again.", 30) Shutdown(6) Exit EndFunc Func _failure2() WinClose("Installing Updates") WinClose("Microsoft Update - ") RegWrite("HKLMSOFTWAREMicrosoftWindowsCurrentVersionRun", "R2", "REG_SZ", '"' & @ScriptFullPath & '" /f2') RunWait('net stop "wuauserv"', "", @SW_HIDE) ShellExecuteWait("sc.exe", 'config "wuauserv" start= disabled', "", "", @SW_HIDE) MsgBox(0, "Not all Updates Completed", "Some updates could not be installed." & @CRLF & @CRLF & "Click OK to reboot and then continue Disk Maintenance.", 30) Shutdown(6) Exit EndFunc Func _updateReboot() WinClose("Installing Updates") WinClose("Microsoft Update - ") RegWrite("HKLMSOFTWAREMicrosoftWindowsCurrentVersionRun", "R2", "REG_SZ", '"' & @ScriptFullPath & '" /r1') MsgBox(0, "Reboot to complete installation", "Windows needs to perform a restart to complete the update installation." & @CRLF & @CRLF & "Cick OK to reboot and continue Disk Maintenance.", 30) Shutdown(6) Exit EndFunc Func _updateReboot2() WinClose("Installing Updates") WinClose("Microsoft Update - ") RegWrite("HKLMSOFTWAREMicrosoftWindowsCurrentVersionRun", "R2", "REG_SZ", '"' & @ScriptFullPath & '" /r2') RunWait('net stop "wuauserv"', "", @SW_HIDE) ShellExecuteWait("sc.exe", 'config "wuauserv" start= disabled', "", "", @SW_HIDE) MsgBox(0, "Reboot to complete installation", "Windows needs to perform a restart to complete the update installation." & @CRLF & @CRLF & "Cick OK to reboot and continue Disk Maintenance.", 30) Shutdown(6) Exit EndFunc Func _r1() RegDelete("HKLMSOFTWAREMicrosoftWindowsCurrentVersionRun", "R2") _updates2() EndFunc Func _r2() RegDelete("HKLMSOFTWAREMicrosoftWindowsCurrentVersionRun", "R2")    _cleanup()    _clearEvents()    _deleteTemp()    _finish() EndFunc Func _f1() RegDelete("HKLMSOFTWAREMicrosoftWindowsCurrentVersionRun", "R2") _updates2() EndFunc Func _f2() RegDelete("HKLMSOFTWAREMicrosoftWindowsCurrentVersionRun", "R2")    _cleanup()    _clearEvents()    _deleteTemp()    _finish() EndFunc Func _help() MsgBox(0, "Disk Maintenance", "Windows XP Disk Maintenance" & @CRLF) EndFunc


Some issues I've noticed:
  • Currently, once the MS Update site is loaded, you have to select your updates manually. I could always automate just going after the critical updates, but worry something might be installed that would be harmful (NOT that MS ever does this).
  • I'm trying to consider other routine maintenance items that might be useful. For the defragmentation, I am currently using the XP defrag, but am considering purchasing a distro license and including a portable command line tool such as AusLogic or MyDefrag.

Edited by JLogan3o13, 29 December 2011 - 05:43 PM.

J.I spent 10 minutes reviewing code and thinking "What kind of drugs is this guy on?" before realizing it was something I wrote.My Scripts:Include Source with Compiled Script, Disk Maintenance for Windows XP, "Deal-A-Day" Sites, SCCM 2007 Front End, Windows Firewall UDF





#2 storme

storme

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 754 posts

Posted 29 December 2011 - 03:20 PM

Not a bad idea I've got something similar to do updates (well I used to use it to do updates).
It got out of date and I never got around to fixing it, then I foudn better ways.

Anyway, just a question.
Why are you using CALL to call all the functions?
The RUN problem you are having maybe because you are using "runonce" instead of the run key.
I used teh RUN key for my script and never had any troubles. it's worth a try.

Anyway, let me know if you need any help.
John Morrison

#3 JLogan3o13

JLogan3o13

    Down to 98

  • MVPs
  • 2,085 posts

Posted 29 December 2011 - 04:49 PM

Hi, Storme. Thanks for the suggestions. You're correct; I can definitely trim it down by eliminating the Call keywords. Regarding using Run or RunOnce, Run does work - I just need to add in a line to remove it once it reboots, so it is not running on every reboot.

Edited by JLogan3o13, 29 December 2011 - 04:49 PM.

J.I spent 10 minutes reviewing code and thinking "What kind of drugs is this guy on?" before realizing it was something I wrote.My Scripts:Include Source with Compiled Script, Disk Maintenance for Windows XP, "Deal-A-Day" Sites, SCCM 2007 Front End, Windows Firewall UDF

#4 willichan

willichan

    Old Geek

  • Active Members
  • PipPipPipPipPipPip
  • 388 posts

Posted 29 December 2011 - 06:30 PM

We do something similar for our end-of-year cleanups. Take a look at this.
I also have a script that envelops a free third-party utility to ensure that all MS updates are installed and up-to-date. I can PM it to you if you want it. It will honor any "ignore this update" flags, but that still does not stop MS from sending out more poison pills. I usually follow it by removing anything I don't want after (like any of the 'Live' or search bar applications).




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users