Sign in to follow this  
Followers 0
JLogan3o13

Disk Maintenance

4 posts in this topic

#1 ·  Posted (edited)

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
#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 = "http://www.update.microsoft.com/microsoftupdate/v6/default.aspx?ln=en-us"

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 = "http://www.update.microsoft.com/microsoftupdate/v6/default.aspx?ln=en-us"
$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:\Windows\System32\defrag.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

√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites



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

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

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

√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

We do something similar for our end-of-year cleanups. Take a look at

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).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0