(Just in Town)
2008-11-20 06:36 PM
Weekly Cleanup Script

Hello all. New to the board here. I have not had much experience with scripting and can do very basic tasks but would like to make a script that can defragment, check disk and perform disk cleanup weekly for my users. I have a script that I tried to use but it doesn't seem to work unless defrag and such have reduced their time to seconds. The computers shutdown normally on friday and it is like the script isn't being applied. I have the below script in the Logoff section of the AD Group Policy that is being applied. Any ideas / help would be greatly appreciated, and hopefully through the use of this board I can learn more about scripting and help others! Thanks.

; Global PM Script
; SIJ 11/08/08
; -------------------------
; All Users

WriteValue ("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows
NT\CurrentVersion", "RegisteredOwner", "SIJ", "REG_SZ")
WriteValue ("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows
NT\CurrentVersion", "RegisteredOrganization", "SIJ", "REG_SZ")

Setup Temp Folders", "StateFlags0001", "00000002", "REG_DWORD")
old files", "StateFlags0001", "00000002", "REG_DWORD")
Indexer Cleaner", "StateFlags0001", "00000002", "REG_DWORD")
Program Files", "StateFlags0001", "00000002", "REG_DWORD")
Cache Files", "StateFlags0001", "00000002", "REG_DWORD")
Dump Files", "StateFlags0001", "00000002", "REG_DWORD")
"StateFlags0001", "00000002", "REG_DWORD")
ChkDsk Files", "StateFlags0001", "00000002", "REG_DWORD")
Desktop Cache Files", "StateFlags0001", "00000002", "REG_DWORD")
Log Files", "StateFlags0001", "00000002", "REG_DWORD")
Files", "StateFlags0001", "00000002", "REG_DWORD")
and WebPublisher Cache", "StateFlags0001", "00000002", "REG_DWORD")

IF @Day='Friday'
	RUN "chkdsk.exe C: /F"
	RUN "cleanmgr.exe /dc /sagerun:1"
	RUN "defrag.exe C:"
	RUN "shutdown.exe -s"

(KiX Master)
2008-11-20 08:24 PM
Re: Weekly Cleanup Script

Well you have a few issues

Unless your users have local Admin rights on the system they can't run any of this. (if they do have local Admin rights then that is a no no as well)

Okay so let's suppose they do have local Admin rights. Well you can't use the RUN command because basically that tries to run each command one after the other without waiting for the first to finish. You should use the SHELL command so that it waits.

On your command for CHKDSK - that needs input which you did not supply so basically it will run that but never complete (unless the user sees it and is able to press the Y key for it).

Here is a method you can use to supply that command.

Without using SAGESET I'm not sure you can use the SAGERUN command like that.
How to Automate the Disk Cleanup Tool in Windows XP

The Shutdown command switch is a forward slash / not a hyphen -

Basically what happens is that it sees the CHKDSK command but can't finish it as it's waiting for input,
then it sees and runs CLEANMGR, but then it gets the DEFRAG command but because CLEANMGR is running it may be too high of CPU and Disk utilization already at this point so it can't do much.
Then it gets the SHUTDOWN command, but basically all these commands happen within seconds of each other so you're luck you don't actually cause some type of data corruption.

Try using SHELL for each and input the CHKDSK as shown and hopefully all should be okay.

Now think about how to manage this without the user needing to be an Admin.

(MM club member)
2008-11-21 10:21 AM
Re: Weekly Cleanup Script

Run it as startup script, then the script has admin privilidges.

(Just in Town)
2008-11-21 03:25 PM
Re: Weekly Cleanup Script

Thanks to you both. I will try those suggestions and let you know the result. The users do have local admin on their machine because after a 25 hour server rebuild project, Outlook profiles and other things weren't working unless they had local admin so we are working on that one step at a time. Now you mentioned running it as a Startup script. Wouldn't that slow things down when the system boots, thus not allowing someone to get right to work? Just trying to see what we can do for them with this. Thanks again!

Edit: I should probably be able to find this in the config or readme, but is there a way to output the success of this script to a log file so I can ensure that it is running properly?

(KiX Master)
2008-11-21 08:56 PM
Re: Weekly Cleanup Script

Different ways to do different things. Myself I would probably create a scheduled task for it, but that's my preference. In fact I do already have a couple of the same tasks you're doing as scheduled tasks for my user's machines.

Glenn BarnasAdministrator
(KiX Supporter)
2008-11-21 10:40 PM
Re: Weekly Cleanup Script

Task scheduler is definitely the way to go. You can specify any credentials without relying on the user being logged on, having admin rights, or anything else.


(Starting to like KiXtart)
2008-11-27 11:47 AM
Re: Weekly Cleanup Script

Just a small additionla note on CHKDSK C: /f
This command won't execute until the next reboot on the C: drive.
See output below...

C:\>chkdsk c: /f
The type of the file system is NTFS.
Cannot lock current drive.

Chkdsk cannot run because the volume is in use by another
process. Would you like to schedule this volume to be
checked the next time the system restarts? (Y/N)

Regards, Bill

(Starting to like KiXtart)
2009-01-23 04:06 PM
Re: Weekly Cleanup Script

One quick update on the shutdown command syntax.
It accepts both forward slashes and hyphens for its switches.

Usage: shutdown [-i | -l | -s | -r | -a] [-f] [-m \\computername] [-t xx] [-c "c
omment"] [-d up:xx:yy]

No args Display this message (same as -?)
-i Display GUI interface, must be the first option
-l Log off (cannot be used with -m option)
-s Shutdown the computer
-r Shutdown and restart the computer
-a Abort a system shutdown
-m \\computername Remote computer to shutdown/restart/abort
-t xx Set timeout for shutdown to xx seconds
-c "comment" Shutdown comment (maximum of 127 characters)
-f Forces running applications to close without war
-d [u][p]:xx:yy The reason code for the shutdown
u is the user code
p is a planned shutdown code
xx is the major reason code (positive integer le
ss than 256)
yy is the minor reason code (positive integer le
ss than 65536)