DTF – Defragment Them Files – An automated (and free!) approach

One of the fun new bits of system information being collected by System Center Operations Manager 2007 R2 is local drive fragmentation levels.  I knew, back in 2003, when we started going whole-hog with Windows Server deployments, that we would start running into disk fragmentation problems eventually. 

At the time, the boss said “wait until it becomes a problem… we don’t want to buy software that we may not need”.  (Water under the bridge…)  I did implement a local script on a few critical systems to prevent fragmentation, but largely I forgot about it.  Along comes SCOM R2, and now I am being told that over half my servers are over 40% fragmented.  Zounds!  This helped to explain a few (but only a few) system performance issue we were having…

So what to do when you have a big fragmentation problem, and no money to fix it?

Group Policy Client Side Extensions to the rescue again, along with Google, and some VBScript.

I configured a new GP object and associated it with our managed servers.  The policy contains:

  • A “file” Preference which copies the script “defrag2k3LocalDisks.vbs” to the local drive on each managed Server 2003 or Server 2003 R2 system
  • A Scheduled Task Preference which runs this script once per week on each Server 2003 or 2003 R2 system
  • A Scheduled Task Preference which runs the following command on each Server 2008 and Server 2008 R2 system:
    • %windir%system32defrag.exe –c
      (no script is required, as we can defrag all local volumes with a single command)

Yes, we were able to get our systems down to acceptable fragmentation levels within a week, with not a cent spent on defragmentation software.

It is worth mentioning that on some systems, native defrag tools are not enough.  If you have dynamically resized pagefiles, and your systems have been critically low on space for extended periods of time, you may experience significant page file defragmentation.  On 32-bit server operating systems, you could use “pagedefrag.exe” from the SysInternals tool suite to get things under control:

PageDefrag can be set to run on every reboot, and will nicely defrag several files that are normally inaccessible while the OS is running.  Sadly, the tool appears to be a bit out of maintenance.  It will not run on 64-bit systems, for example.

Fortunately, you also can fix page file fragmentation the brute force way… configure the system to use no page file, reboot, and then configure a new, statically-sized page file.  Assuming there is adequate contiguous space on your drive, the new page file should be created with no fragments, and should stay that way.  I am going to assume that this is not a widespread problem for us (perhaps falsely), so I am not going to go looking for trouble by implementing “PageDefrag” across the enterprise (nor am I going to script reporting of page file fragmentation).

Contents of the “defrag2k3LocalDisks.vbs” file follow:

' Defrag2k3LocalDisks.vbs - J. Greg Mackinnon, November 2009
' Runs the native NT defrag utility on all local drives using the Win32_Volume
'   COM object "Defrag" method.
' Tested on Server 2003 only
' Base code taken from somewhere on the Internet... sorry for the lack of
'   attribution.

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\" & strComputer & "rootcimv2")

' Creates a collection of disk volume objects, with type of "local"
Set colVolumes = objWMIService.ExecQuery _
    ("Select * from Win32_Volume Where DriveType = '3'")

For Each objVolume in colVolumes
    ' Execute the "Defrag" method on each volume in  the collection.
    ' This will spawn the dfrgifc.exe process.
    errResult = objVolume.Defrag()

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s