Schedule DiskDefrag using Group Policy

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
«1

Comments

  • undomielundomiel Posts: 2,818Registered Members
    Push out a script to schedule the tasks using "at"

    i.e.

    at 4:30 "defrag.exe C:"

    Take a look at the command line options to customize it to your tastes.
    Jumping on the IT blogging band wagon -- http://www.jefferyland.com/
  • dabve3dabve3 Posts: 77Registered Members ■■□□□□□□□□
    I did this once by putting it into a startup script. Type in defrag /? to see the syntax for it. Then to trigger a restart I made another script to restart all the PC's. The shutdown command is useful for this. Just schedule the restart script to run on one of your DC's. Unfortunatley you have to add a line for every PC you want to restart so it is a little time consuming. Although it is still a lot faster than going around to everyone and restarting it yourself.

    Hope this helps!!



    undomiels way is better then you won't have to make a big restart script.
  • TechStrikerTechStriker Posts: 131Registered Members
    Thanks guys for your quick response, I will test this command lines with start up script
    Passed SNIA - SCSP
    Working on VCP4
  • astorrsastorrs Posts: 3,139Registered Members
    Instead of AT.EXE (which stems from NT4 and is somewhat handicapped by that) use SCHTASKS.EXE it has far more switches and I believe fully supports everything you can go from the GUI.
  • sprkymrksprkymrk Posts: 4,884Registered Members
    These suggestions should get you on the right track, and I would only add one or two options that are simply my opinions, not necessarily better than other suggestions already given.

    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.
    All things are possible, only believe.
  • TechStrikerTechStriker Posts: 131Registered Members
    sprkymrk can you please post an example script, I really appreciate for all your help and suggestions icon_confused.gif
    Passed SNIA - SCSP
    Working on VCP4
  • TechStrikerTechStriker Posts: 131Registered Members
    I want to schedule defrag and disk clean up once a month at 19.00 I know all client machines are free after 19.00 and we have software which shuts down all clients machines at 21.00, can you post sample script please which will help me do this task.
    Passed SNIA - SCSP
    Working on VCP4
  • sprkymrksprkymrk Posts: 4,884Registered Members
    Well here is a sample script that you can run at will (or simply use the Task Scheduler on your computer to run it once a month). Make sure you run it as an account that has admin rights on all the client machines.

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

    Good luck. :)
    All things are possible, only believe.
  • TechStrikerTechStriker Posts: 131Registered Members
    Thank you very much sprkymrk, your example works like charm, is it best practice to group the clients per 100 or you can run 500 client computers on one file at one go and walk away.

    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
    Passed SNIA - SCSP
    Working on VCP4
  • undomielundomiel Posts: 2,818Registered Members
    psexec is a great program, I would dare say it is my number one favourite command line tool, and I would recommend getting fully familiar with it. Psexec works like this: it jumps on the computer, executes the command, then jumps off and moves on to the next computer. So yes it is one at a time like you saw.

    By the way, instead of using a for loop by the way you could also do:
    psexec @c:\Temp\computers.txt defrag C:

    Edit:
    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.
    Jumping on the IT blogging band wagon -- http://www.jefferyland.com/
  • sprkymrksprkymrk Posts: 4,884Registered Members
    Thank you very much sprkymrk, your example works like charm, is it best practice to group the clients per 100 or you can run 500 client computers on one file at one go and walk away.

    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:

    etc.

    undomiel wrote:
    By the way, instead of using a for loop by the way you could also do:
    psexec @c:\Temp\computers.txt 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.
    All things are possible, only believe.
  • undomielundomiel Posts: 2,818Registered Members
    Wow was I being redundant by the way.

    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.
    Jumping on the IT blogging band wagon -- http://www.jefferyland.com/
  • cwallacecwallace Posts: 8Registered Members ■□□□□□□□□□
    Finally I think I have found a solution to a big problem with our network! Thank you for the insight...

    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.

    Chris
  • SieSie Posts: 1,195Registered Members
    Following a command with the below:

    >> Text.txt

    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.

    Coincidently :

    > Text.txt

    Does the same but appends the file.
    Foolproof systems don't take into account the ingenuity of fools
  • cwallacecwallace Posts: 8Registered Members ■□□□□□□□□□
    I was entering it in the run box...I guess it actually has to be run IN a command window...

    All good...

    Thank you so much!

    Chris
  • sprkymrksprkymrk Posts: 4,884Registered Members
    Sie wrote:
    Following a command with the below:

    >> Text.txt

    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.

    Coincidently :

    > Text.txt

    Does the same but appends the file.

    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.
    All things are possible, only believe.
  • cwallacecwallace Posts: 8Registered Members ■□□□□□□□□□
    I am trying to run the defrag on a remote machine, but get this error now:

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

    Chris
  • undomielundomiel Posts: 2,818Registered Members
    Wouldn't the command go something more along the lines of this?

    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.
    Jumping on the IT blogging band wagon -- http://www.jefferyland.com/
  • cwallacecwallace Posts: 8Registered Members ■□□□□□□□□□
    Nope...same result. Will keep poking around at it...

    Chris
  • sprkymrksprkymrk Posts: 4,884Registered Members
    First make sure the default admin share is available. I am assuming it is, but better to ask.

    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.
    All things are possible, only believe.
  • cwallacecwallace Posts: 8Registered Members ■□□□□□□□□□
    Other than you missing the -p it works perfectly...

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

    Thanks!!

    Chris
  • undomielundomiel Posts: 2,818Registered Members
    As astorrs mentioned earlier you could use schtasks. Open up a command prompt and do schtasks /create /? to find out what the options are for scheduling a task.

    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.
    Jumping on the IT blogging band wagon -- http://www.jefferyland.com/
  • cwallacecwallace Posts: 8Registered Members ■□□□□□□□□□
    Thank you very much sprkymrk, your example works like charm, is it best practice to group the clients per 100 or you can run 500 client computers on one file at one go and walk away.

    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

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

    Chris
  • cwallacecwallace Posts: 8Registered Members ■□□□□□□□□□
    I am also working on setting up the task scheduler, which I know would solve this sequential problem, but in the case of not using the task scheduler is there a way around this...

    Chris
  • undomielundomiel Posts: 2,818Registered Members
    That's why it would be better just to use psexec to have it schedule a defrag on the machines than having it do the defrag on the machines.
    Jumping on the IT blogging band wagon -- http://www.jefferyland.com/
  • cwallacecwallace Posts: 8Registered Members ■□□□□□□□□□
    Setting up the scheduled task command I get prompted for a password for each machine as it does it. I set it up to test on 3 machines on the network to see the results.

    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:

    \\coll3:

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

    Chris
  • wetpiggirlwetpiggirl Posts: 1Registered Members ■□□□□□□□□□
    What about using a triggering syntax?

    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.
  • kaeveskaeves Posts: 2Registered Members ■□□□□□□□□□
    I was trying to solve the same problem of defragging all machines on a network and found this thread. I liked what I saw in your solutions, so I thought I'd try to take it to the next level. Here is the batch file that I wrote trying to implement the best thoughts from this thread.


    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.
    echo off
    REM defragSubnet.bat
    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)

    :callChildren
    IF %i% GEQ %final% GOTO END
    set /a block=%i%+%numInstances%
    START "" defragSubnet.bat %1 %i% %block% %4 0
    SET /a i=%block%
    GOTO callChildren


    :defrag
    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
    GOTO defrag

    :end
    EXIT
  • kaeveskaeves Posts: 2Registered Members ■□□□□□□□□□
    Oh, and to answer wetpiggirl's question about only defragging as needed, I'm pretty sure that the standard defrag command does that. If you add -a it will only analyze and never defrag, and if you add -f it will force a defrag.

    [Edit] No, I was wrong. With the default settings, it appears to attempt a defrag even with 0% file fragmentation. [/Edit]
  • thesemantheseman Posts: 230Registered Members
    You could install the GP client extensions on all of the PC's (WSUS) then use a Vista machine to setup the task.

    (Comp Config- Preferences - Control Panel Settings - Scheduled Tasks)

    -Travis

    Edit: I mention using Vista as I was assuming you do not have any 2008 servers yet.
«1
Sign In or Register to comment.