Previous years i had running several scheduled tasks on all my servers.

Recently I migrated all my Physical Large Iron Hardware (mostly servers) to Virtual Servers (VMs) in XEN. I have an OpenFiler SAN which hosts all the Storage (iSCSI storage or NFS shares).
I still had all my scheduled tasks running at all the Virtual servers, which included Backup and Defrag tasks, they all started somewhere between 02:00 and 06:00 during the night.

Because the defrag and backup tasks were now running all on the same OpenFiler SAN and all at the same time, the SAN was getting a bit slow,… as you can imagine ;)

That’s why i made a sequential defrag script!

It runs from any Windows machine and executes the Windows Defrag process at the other Windows machines (you can set a static list of machinenames/IPs and drives you want to defrag), as soon as the defrag finishes, the next machine is going to defrag.
The script uses WMI to execute the remote process, which is available on every Windows server/client by default. The Scheduled Task can run under any user as long as the user has rights to execute the WMI task at the remote computer (i.e. can be a domain admin, local admin which is created at several servers with the same password, etc.)

If you have a Windows Firewall enabled on the machines which need to defrag, make sure to allow WMI (Remote Management).

If you have a Windows Firewall enabled and using a domain, it can be done much easier: Set the following policy using GPO Edit: Computer Configuration -> Administrative Templates -> Network -> Network Connections -> Windows Firewall -> Domain/Standard Profile -> Windows Firewall: Allow remote administration exception.  Make sure the machine which runs the script (or the whole subnet) is in here.

All jobs are recorded to a logfile.

Download it (defrag_servers.zip [2kb] ) or do with it what you want!
Comments are welcome!