Schedule DiskDefrag using Group Policy
TechStriker Member Posts: 131
I have been googling this for 3 hours I had no luck, I want if there is an option in group policy to schedule a disk defrag and disk clean up? I can setup a scheduled task on each PC but I want to avoid this. I don’t want to go round 500+ clients and schedule defrags if any one did this please let me know best way to do it. If there is third party open source software which can automate this task is even better but I haven’t seen it and we can’t purchase third party software like Diskeeper.
Passed SNIA - SCSP
Working on VCP4
Working on VCP4
at 4:30 "defrag.exe C:"
Take a look at the command line options to customize it to your tastes.
Hope this helps!!
undomiels way is better then you won't have to make a big restart script.
Working on VCP4
First, check out the command "for". It's great when you want to write a script with one variable that will read a text file and substitute the variable with each line/item in the text file in turn. It is easy to create a text file of all your computers (export list) from Active Directory. I'm sure a look through my past posts will show a dozen or more times I have suggested it to people for situations such as this.
This way you can run the script at will using "psexec" from the pstools suite, a free download from Microsoft. To me this is better than a startup script, because honestly your users will get tired of waiting for a defrag and disk cleanup every time they have to restart their computer. It would also be a pain when you're trying to help trouble shoot an issue or install new software requiring a restart.
Anyway, those are my two suggestions, "psexec" and "for".
I can post an example script if you want, it all fits in one line.
Working on VCP4
Working on VCP4
1. You need a text file of all the computers you want to run this on. You can use AD and simply choose the OU with your client computers, right click, select export list. You'll want to pull it into excel (import data) to clean it up and leave only the computer names. Then copy and paste it back to a text file. Let's call it "computers.txt" in c:\temp for example.
2. Download the PSTools from Microsoft and just unzip them into your C:\Windows directory.
3. Then open notepad and type:
for /f %%C in (C:\Temp\computers.txt) do psexec \\%%C defrag C:
4. Save this as "defrag.bat". Now you can either double click it to launch at will or run it as a scheduled task on your computer (not the clients, just yours). This will cause it to run defrag on all the computers in the text file. You can create a second script that runs disk cleanup the same way.
One thing to note about the "for" command, if you use it in a batch file as I described, you use 2 percent symbols for the variable (%%). If you run "for" direct from the cmd line you only use 1 percent symbol (%).
The other thing I noticed is PsExec executes commands sequential one computer at a time I could see from command that it started the defrag the first computer on the list of my computers.txt file after defrag finishes goes to the second computer on the list, is that how is supposed to work
Working on VCP4
By the way, instead of using a for loop by the way you could also do:
psexec @c:\Temp\computers.txt defrag C:
Oh and thanks astorrs for pointing out schtask.exe. Didn't know about that one! "at" I'm familiar with from linux. I love the command line, always something new to find out.
Yes, I do like to split them up into multiple files with 50 or so computers each. You can launch them all at the same time and they work independantly. So you might have 10 lists with 50 computers each, just have each batch file work on different lists:
for /f %%C in (C:\Temp\computers1.txt) do psexec \\%%C defrag C:
for /f %%C in (C:\Temp\computers2.txt) do psexec \\%%C defrag C:
for /f %%C in (C:\Temp\computers3.txt) do psexec \\%%C defrag C:
True, but for some reason I've always had bad luck with that particular syntax. However, another easy way would be using the "start, step, stop" method instead of a text file. Just use your IP addresses like so:
for %%C in (1,1,254) do psexec \\192.168.10.%%C defrag C:
This would run the command on all IP addresses from 192.168.10.1-254. Start at 1, increment (or step) by 1, all the way to 254.
What problems have you had with @? I haven't run into anything so far. Which is not to imply there aren't problems but to know what to keep an eye out for.
I do have a question about the PsInfo.exe though. I am curious where it '****' the information? I ran it on my local system via command line and it just ran and closed the window. I am using Vista on this machine and have not tried it on another machine in the network with a different OS so it might be that.
Is there a way to output it to a text file somewhere on my system and retrieve it?
Anyway...thanks for the defrag information as well.
Would save the output of the command to a txt file called Text.txt. You can call this whatever you like and even in a different directory, just include the full path.
Does the same but appends the file.
Thank you so much!
Might just be a typo, but a single "greater than" sign (>) overwrites a file if one exists with that name or just creates it. Using double "greater than" signs (>>) appends information to the end of an existing file if it exists or just creates it.
Make sure that the default admin$ share is enabled on col3
col3 being the machine I am trying to run this one.
I ran it as:
c:PsTools\psexec -u \\domainname\Administrator -p <PASSWORD> defrag \\col3\c:
Tha machine is part of the domain and I have the user/pass correct.
I did that from a cmd prompt and not from a script. I am sure it is something simple or stupid.
Please advise accordingly...:)
psexec -u \\domainname\Administrator -p <PASSWORD> \\col3 defrag c:
Because from the looks of things in your command it would be trying to access a \\col3\c: computer.
Then try the command like this:
psexec \\col3 -u domain\username password defrag.exe c:
You need to specify the remote computer first, then the credentials, then the command you want to run.
Now to build the script to make it happen...
Can you specify a date/time easily to possibly activate this?
I am trying to schedule a MONTHLY defrag on ALL of the systems in the network...also disk cleanup...
Slowly making my way...but progress is progress...heh..
Put your defrag jobs into a batch file in c:\jobs\defrag.bat for this example.
schtasks /create /sc monthly /st 18:30:37 /tn "Monthly Defrag" /tr "c:\jobs\defrag.bat"
That will set it up to run as the currently logged in user (it will ask for your password) and then ta da it is scheduled. You can use /u domain/user /p password to schedule it under a different account. Take a look at the options and customize it to your needs.
Does it actually WAIT for the first defrag to finish before starting the NEXT machine?
That could be VERY time consuming...it can take an hour of more to defrag a single computer in our collection of crap machines...:)
Is there a way around this or would I have to build a batch file that triggers each of them individually?
Sorry if I missed a solution to this assuming I am correct in my thought process on it...
this is what I run:
psexec @c:\PsTools\scheduletest.txt -u Administrator -p <password> schtasks /create /sc monthly /st 18:30:37 /tn "Monthly Defrag" /tr "C:\WINDOWS\SYSTEM32\defrag.exe"
It then prompts me for the password:
The task will be created under current logged-on user name ("COLL3\Administrator").
Please enter the run as password for COLL3\Administrator: <password>
SUCCESS: The scheduled task "Monthly Defrag" has successfully been created.
schtasks exited on coll3 with error code 0.
Then it moves to the next one...and prompts me again. I tried to add the -u and -p flags in the schedule task command right after the /create, but it says wrong syntax. So where would I specifiy that user/pass in order to avoid the request on 150 computers?
One day at a time...heh...
For example, i want to scan my 100 or so machines, but if they do not require defragging, then skip to the next, but if it does require defragging, then initiate.
Ive tried lots of IF and FOR's, but none seem to truly trigger the way i want. Ive also tried piping with some findstr "", but still running into syntax issues.
I recommend leaving the comments in it. The file should be called defragSubnet.bat. It is recursive, so if you change the file name, you must also change the line of code where it calls itself.
I wrote it according to my current needs, so it will only scan subnets starting with 192.168. and not others. Feel free to modify it as you need to. You'd just need to add a couple more arguments and variables.
It would also be easy to change this to run any command on an entire network, provided that they all have the same administrative password.
Script begins below this line.
REM This batch file takes the following arguments
REM Subnet (192.168.XXX), Starting address (192.168.XXX.YYY), Ending address (192.168.XXX.ZZZ), Number of machines to scan per thread, and a 1
REM this is a recursive program, so the last 1 just indicates that it was the parent of all the children it calls
REM example: defragSubnet.bat 2 100 150 5 1
REM this would defrag all machines from 192.168.2.100 - 192.168.2.150, in blocks of five (thus reducing the load on the machine calling the script)
REM example: defragSubnet.bat 2 100 150 1 1
REM this would defrag all machines from 192.168.2.100 - 192.168.2.150, in blocks of one (essentially doing all target machines simultaneously)
REM Larger blocks mean longer time to completion; smaller blocks mean greater overhead on server calling this script
REM for scheduled jobs, just make a second script that calls this one with your favorite settings
SET /a subnet=%1
SET /a i=%2
SET /a final=%3
SET /a numInstances=%4
SET /a parent=%5
IF %parent%==1 (GOTO callChildren) else (GOTO defrag)
IF %i% GEQ %final% GOTO END
set /a block=%i%+%numInstances%
START "" defragSubnet.bat %1 %i% %block% %4 0
SET /a i=%block%
IF %i%==%final% GOTO END
ECHO 'psexec \\192.168.%subnet%.%i% defrag C:'
psexec \\192.168.%subnet%.%i% defrag C:
SET /a i=%i%+1
[Edit] No, I was wrong. With the default settings, it appears to attempt a defrag even with 0% file fragmentation. [/Edit]
(Comp Config- Preferences - Control Panel Settings - Scheduled Tasks)
Edit: I mention using Vista as I was assuming you do not have any 2008 servers yet.