Caveats:
- Only works for WinXP right now, building support for Win7
- Must manually select updates
- 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.







