Browsing This
Cleanup, VBScript Style
When I first started working at Humana, I noticed that several of our calls consisted of the traditional clearing of cookies and cache and what not. I was later surprised to learn that this process had never been simplified. Everytime they did this, they did it manually. It would take people up to 5 minutes or more just to click around and clear out basic stuff. So I decided to take the initiative and, with my manager’s permission, write a script that would simplify the process. In the end, I think it came out pretty good. It’s a pretty basic script–just deleted cookies, temp files, and internet temp files. I added a few extras though to make it a little “smarter.” It’ll prompt the user before hand if there’s any applications running that might interfere with the cleanup. I also “had” to add a selection if the user wanted to delete the cookies or not.
It was tested by a small crew of people I selected, then turned loose on the Green Bay CSS Team. Now it’s awaiting an upgrade to be posted on the corporate files page and possibly on every computer in Humana (as part of a “Go Home” script that is run every night for maintenance). Now that would be some cool stuff.
For those so technically inclined, here it is, in all it’s monospace glory:
'***************************************************************************
' System Cleanup Script
' Written and managed by Jacob Thornberry (xxxxxxxxxx@humana.x)
'
' 09/26/2006 - v1 - Initial release
' 10/01/2006 - v2 - Process checking, more documentation, removed History deletion
' 10/04/2006 - v2.1 - Cleaned up code, modified MsgBox, added Cookies check
' 10/16/2006 - v2.2 - Script presented to Mindy/IT for testing
' 10/16/2006 - v2.3 - Fixed Local Temp folder clearing issues
' 10/18/2006 - v2.4 - Added check for unusual accounts (user.000, etc.)
'***************************************************************************'***************************************************************************
' Declare Variables
'***************************************************************************
Const WindowsFolder = 0
Const SystemFolder = 1
Const TemporaryFolder = 2Dim WshShell, strTempFolder, Fso, WshNetwork, strComputerName, objWMIService, objProcess, colProcess, strMsgOut, strMsgBox, strAcctCount
Dim intIEcount, intLNcount, intJavacount'***************************************************************************
' Establish variables
'***************************************************************************
Set WshShell = WScript.CreateObject("WScript.Shell")
Set Fso = WScript.CreateObject("Scripting.FileSystemObject")
Set WshNetwork = WScript.CreateObject("Wscript.Network")strTempFolder = fso.GetSpecialFolder(TemporaryFolder)
strComputerName = "."
intIEcount = 0
intLNcount = 0
intJavacount = 0'***************************************************************************
' Connect to the WMI Service to get a list of all running processes
'***************************************************************************
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputerName & "\root\cimv2")
Set colProcess = objWMIService.ExecQuery("SELECT * FROM Win32_Process")'***************************************************************************
' Find and list any processes that may interfere with the cleanup (IE, Notes, Java (for HOD).
' (Note that if the user has consistent errors of a running Notes process, you should run killnotes.vbs)
'***************************************************************************
For Each objProcess in colProcess
strList = strList & vbCr & objProcess.Name
If objProcess.Name = "iexplore.exe" Then intIEcount = intIEcount + 1
If objProcess.Name = "nlnotes.exe" Then intLNcount = intLNcount + 1
If objProcess.Name = "nhldaemn.exe" Then intLNcount = intLNcount + 1
If objProcess.Name = "ntaskldr.exe" Then intLNcount = intLNcount + 1
If objProcess.Name = "notes.exe" Then intLNcount = intLNcount + 1
If objProcess.Name = "naldaemn.exe" Then intLNcount = intLNcount + 1
If objProcess.Name = "javaw.exe" Then intJavacount = intJavacount + 1
Next'***************************************************************************
' Uncomment this to show a list of all processes--for debugging only
'WScript.Echo strList
'***************************************************************************'***************************************************************************
' Build the message box text notifying the user of any interfering processes
'***************************************************************************
If intIEcount > 0 Then strMsgOut = strMsgOut & intIEcount & " Internet Explorer process(es)" & vbCr
If intLNcount > 0 Then strMsgOut = strMsgOut & intLNcount & " Lotus Notes process(es)" & vbCr
If intJavacount > 0 Then strMsgOut = strMsgOut & intJavacount & " HOD Mod/Java application(s)" & vbCrIf intIEcount = 0 And intLNcount = 0 And intJavacount = 0 Then
strMsgBox = MsgBox ("Please verify that Lotus Notes, Internet Explorer, and all other open windows are closed before continuing.", vbInformation, "Cleanup")
Else
strMsgBox = MsgBox ("Please verify that the following applications are closed before continuing:" & vbCr & strMsgOut & "The cleanup can proceed if these processes are not closed, but it will not be a full cleanup.", vbExclamation, "Cleanup")
End If'***************************************************************************
' Prompt if the user is a home user. If the user selects yes, cookies will
' not be deleted.
'***************************************************************************
strMsgBox = Msgbox ("Are you currently running on a home computer?" & vbCr & "If you select Yes, Cookies will not be deleted." & vbCr & "If you select No, Cookies will be deleted." & vbCr & "If you are unsure, click No.", 4, "Cleanup")' Prereq checking complete; begin cleanup process
' Empty out MsgOut variable for reuse
strMsgOut = ""'***************************************************************************
' TEMPORARY FILES IN C:\WINDOWS\Temp
' Deletes all files within the Windows Temp folder
'***************************************************************************If fso.FolderExists("C:\WINDOWS\Temp\") Then
On Error Resume Next
wshShell.run "cmd /c del " & "C:\WINDOWS\Temp\*.* /q", 1, True
fso.DeleteFile ("C:\WINDOWS\Temp\*.*"), True
fso.DeleteFolder ("C:\WINDOWS\Temp\*.*"), True
strMsgOut = strMsgOut & "Windows Temp cleared successfully!" & vbCrEnd If
'***************************************************************************
' TEMPORARY FILES IN C:\Temp
' Deletes all files within the root Temp folder
'***************************************************************************If fso.FolderExists("C:\Temp\") Then
On Error Resume Next
wshShell.run "cmd /c del " & "C:\Temp\*.* /q", 1, True
fso.DeleteFile ("C:\Temp\*.*"), True
fso.DeleteFolder ("C:\Temp\*.*"), True
strMsgOut = strMsgOut & "Root Temp cleared successfully!" & vbCrEnd If
'***************************************************************************
' TEMPORARY FILES IN C:\Documents and Settings\"USERNAME"\Local Settings\Temp
' Deletes all files within the User's Temp folder
'***************************************************************************' User's account is normal (just the userID)
If fso.FolderExists("C:\DOCUME~1\" & WshNetwork.username & "\LOCALS~1\Temp\") ThenOn Error Resume Next
wshShell.run "cmd /c del " & "C:\DOCUME~1\" & WshNetwork.Username & "\LOCALS~1\Temp\*.* /q", 1, True
fso.DeleteFile ("C:\DOCUME~1\" & WshNetwork.username & "\LOCALS~1\Temp\*.*"), True
fso.DeleteFolder ("C:\DOCUME~1\" & WshNetwork.username & "\LOCALS~1\Temp\*.*"), True
strMsgOut = strMsgOut & "Local Temp cleared successfully!" & vbCrEnd If
' User's account was modified (ends with .000-.009)
For strAcctCount = 0 to 9If fso.FolderExists("C:\DOCUME~1\" & WshNetwork.username & ".00" & strAcctCount & "\LOCALS~1\Temp\") Then
On Error Resume Next
wshShell.run "cmd /c del " & "C:\DOCUME~1\" & WshNetwork.Username & ".00" & strAcctCount & "\LOCALS~1\Temp\*.* /q", 1, True
fso.DeleteFile ("C:\DOCUME~1\" & WshNetwork.username & ".00" & strAcctCount & "\LOCALS~1\Temp\*.*"), True
fso.DeleteFolder ("C:\DOCUME~1\" & WshNetwork.username & ".00" & strAcctCount & "\LOCALS~1\Temp\*.*"), TrueEnd If
Next
'***************************************************************************
' COOKIES IN C:\Documents and Settings\"USERNAME"\Cookies
' Deletes all files within the User's Cookies folder
'***************************************************************************If strMsgBox = 7 Then
' User's account is normal (just the userID)
If fso.FolderExists("C:\DOCUME~1\" & WshNetwork.username & "\Cookies\") ThenOn Error Resume Next
wshShell.run "cmd /c del " & "C:\DOCUME~1\" & WshNetwork.Username & "\Cookies\*.* /q", 1, True
fso.DeleteFile ("C:\DOCUME~1\" & WshNetwork.username & "\Cookies\*.*"), True
fso.DeleteFolder ("C:\DOCUME~1\" & WshNetwork.username & "\Cookies\*.*"), True
strMsgOut = strMsgOut & "Cookies cleared successfully!" & vbCrEnd If
' User's account was modified (ends with .000-.009)
For strAcctCount = 0 to 9If fso.FolderExists("C:\DOCUME~1\" & WshNetwork.username & ".00" & strAcctCount & "\Cookies\") Then
On Error Resume Next
wshShell.run "cmd /c del " & "C:\DOCUME~1\" & WshNetwork.Username & ".00" & strAcctCount & "\Cookies\*.* /q", 1, True
fso.DeleteFile ("C:\DOCUME~1\" & WshNetwork.username & ".00" & strAcctCount & "\Cookies\*.*"), True
fso.DeleteFolder ("C:\DOCUME~1\" & WshNetwork.username & ".00" & strAcctCount & "\Cookies\*.*"), TrueEnd If
Next
End If
If strMsgBox = 6 Then
strMsgOut = strMsgOut & "Cookies were NOT cleared!" & vbCr
End If
'***************************************************************************
' OFFLINE WEB PAGES IN C:\WINDOWS\Offline Web Pages
' Deletes all offline web pages
'***************************************************************************On Error Resume Next
wshShell.run "cmd /c del " & "C:\WINDOWS\Offine Web Pages\*.* /q", 1, True
fso.DeleteFile ("C:\WINDOWS\Offine Web Pages\*.*"), True
fso.DeleteFolder ("C:\WINDOWS\Offine Web Pages\*.*"), True'***************************************************************************
' TEMPORARY INTERNET FILES IN C:\Documents and Settings\"USERNAME"\Local
' Settings\Temporary Internet Files
' Deletes all files excluding ones being used by the system
'***************************************************************************' User's account is normal (just the UserID)
On Error Resume Next
wshShell.run "cmd /c del " & "C:\DOCUME~1\" & WshNetwork.Username & "\Tempor~1\*.* /q", 1, True
fso.DeleteFile ("C:\DOCUME~1\" & WshNetwork.username & "\Tempor~1\*.*"), True
fso.DeleteFolder ("C:\DOCUME~1\" & WshNetwork.username & "\Tempor~1\*.*"), TrueIf fso.FolderExists("C:\DOCUME~1\" & WshNetwork.username & "\Local Settings\Temporary Internet Files\") Then
wshShell.run "cmd /c del " & "C:\DOCUME~1\" & WshNetwork.Username & "\Local Settings\Temporary Internet Files\*.* /q", 1, True
fso.DeleteFile ("C:\DOCUME~1\" & WshNetwork.username & "\Local Settings\Temporary Internet Files\*.*"), True
fso.DeleteFile ("C:\DOCUME~1\" & WshNetwork.username & "\Local Settings\Temporary Internet Files\Content.IE5\*.*"), True
fso.DeleteFolder ("C:\DOCUME~1\" & WshNetwork.username & "\Local Settings\Temporary Internet Files\Content.IE5\*.*"), True
strMsgOut = strMsgOut & "Temporary Internet Files cleared successfully!" & vbCr
End If
On error goto 0' User's account was modified (ends with .000-.009)
For strAcctCount = 0 to 9If fso.FolderExists("C:\DOCUME~1\" & WshNetwork.username & ".00" & strAcctCount & "\Tempor~1\") Then
wshShell.run "cmd /c del " & "C:\DOCUME~1\" & WshNetwork.Username & ".00" & strAcctCount & "\Tempor~1\*.* /q", 1, True
fso.DeleteFile ("C:\DOCUME~1\" & WshNetwork.username & ".00" & strAcctCount & "\Tempor~1\*.*"), True
fso.DeleteFolder ("C:\DOCUME~1\" & WshNetwork.username & ".00" & strAcctCount & "\Tempor~1\*.*"), TrueIf fso.FolderExists("C:\DOCUME~1\" & WshNetwork.username & ".00" & strAcctCount & "\Local Settings\Temporary Internet Files\") Then
wshShell.run "cmd /c del " & "C:\DOCUME~1\" & WshNetwork.Username & ".00" & strAcctCount & "\Local Settings\Temporary Internet Files\*.* /q", 1, True
fso.DeleteFile ("C:\DOCUME~1\" & WshNetwork.username & ".00" & strAcctCount & "\Local Settings\Temporary Internet Files\*.*"), True
fso.DeleteFile ("C:\DOCUME~1\" & WshNetwork.username & ".00" & strAcctCount & "\Local Settings\Temporary Internet Files\Content.IE5\*.*"), True
fso.DeleteFolder ("C:\DOCUME~1\" & WshNetwork.username & ".00" & strAcctCount & "\Local Settings\Temporary Internet Files\Content.IE5\*.*"), TrueEnd If
On error goto 0
End If
Next
strMsgBox = MsgBox ("Cleanup Complete. The following operations were performed:" & vbCr & strMsgOut, vbInformation, "Cleanup")
set fso = Nothing
set wshshell = Nothing
set WshNetwork = Nothing
set NewFolder = Nothing
set objShell = Nothing
Set objWMIService = Nothing
~Jaker
1 User Commented In " Cleanup, VBScript Style "
We’ve got a hottie on our hands…
I’m so proud of you and your mad skillz of computerizing. Keep sharing your talent with the world; believe me, the world is in sore need of it.
Just as I am. I mean…
*awaits the sarcastic comment backlash from your other readers*