Microsoft Windows Server are very great products to work with. And,... server administrators and operators do not like to do stupid work or repeating tasks. That's why I made this new category, to share my most favorite general scripts that I often use. This is not a scripting site, there are many more and better sites available for that. These scripts are just handy for common use. So, the initially set up was for my own references, but now I share it with you. Enjoy it !! Due to the fact that a big percentage of visitors of this page are international based, this article is in the english language.

Defrag all disks script:
This script has been made to defrag all disks on a server. Now normally, you can automate this action, or execute these commands remotely through provisioning-, or monitoring software like System Center Operation Manager 2007 R2. I personally use this script most of the times in DMZ environments, as you can schedule this script with the normal task manager and it can run under a normal administrative DMZ account.  Feel free to copy this text, would be nice if you keep my name, or webpage in the script though. Save it with a name you want and you wil find out how handy it can be. You might want to change, or rename the report.txt into a name which includes your workstation name or something like that. Besides that, most servers do not allow a save or creation of a new file into the operatingssystems root location.

'Defrags all hard disks - Can be run as a Scheduled Task
'Modified to create an error log and display it afterwards
'© Ben Oostdam - 24/01/2010
Option Explicit
Dim WshShell, fso, d, dc, ErrStr(), Return, X, A(), MyFile, I, MyBox, Drive
Set WshShell = WScript.CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
X = 0
   Set dc = fso.Drives
For Each d in DC
 If d.DriveType = 2 Then
    X = X + 1
'Determine drive letter of first fixed disk
'This is the drive that the error report will be placed on
  If X = 1 Then
     Drive = d
  End If
 End If
ReDim A(X)
ReDim ErrStr(X)
X = 0
For Each d in dc
      If d.DriveType = 2 Then
      X = X + 1
      Return = WshShell.Run("defrag " & d & " -f", 1, TRUE)
'Determine the Error code returned by Defrag for the current drive and save it
If return = 0 then
  ErrStr(x) = ErrStr(x) &  "Drive " & d & " Defrag completed successfully" & vbCRLF
elseif return = 1 then
  ErrStr(x) = ErrStr(x) &  "Drive " & d & " Defrag aborted with error level " & return & " (defrag was cancelled manually) " & vbCRLF
elseif return = 2 then
  ErrStr(x) = ErrStr(x) &  "Drive " & d & " Defrag aborted with error level " & return & " (there was a command line error. Check your command line for valid switches and drives)" & vbCRLF
elseif return = 3 then
  ErrStr(x) = ErrStr(x) &  "Drive " & d & " Defrag aborted with error level " & return & " (there was an unknown error)" & vbCRLF
elseif return = 4 then
  ErrStr(x) = ErrStr(x) &  "Drive " & d & " Defrag aborted with error level " & return & " (defrag could not run due to insufficient memory resources)" & vbCRLF
  'errorlevel 5 is not currently used
elseif return = 5 then
  ErrStr(x) = ErrStr(x) &  "Drive " & d & " Defrag aborted with error level " & return & " (general error)" & vbCRLF
elseif return = 6 then
  ErrStr(x) = ErrStr(x) &  "Drive " & d & " Defrag aborted with error level " & return & "(System error: either the account used to run defrag is not an administrator, there is a problem loading the resource DLL, or a defrag engine could not be accessed. Check for proper user permissions and run Sfc.exe to validate system files)" & vbCRLF
elseif return = 7 then
  ErrStr(x) = ErrStr(x) &  "Drive " & d & " Defrag aborted with error level " & return & " (There is not enough free space on the drive. Defrag needs 15% free space to run on a volume)" & vbCRLF
  ErrStr(x) = ErrStr(x) &  "Drive " & d & " Defrag aborted with an unknown error level: " & return & vbCRLF
end if
       End If
'Create the Error Report in the root of the first fixed disk.
Set MyFile = fso.OpenTextFile(Drive & "\defragreport.txt", 2, True)
MyFile.WriteLine(Date) & vbCRLF
MyFile.WriteLine(Time) & vbCRLF
   For I = 1 to X
'Do Not open report by default
'Return = WshShell.Run(Drive & "\defragreport.txt",3,True)
Set WshShell = Nothing
Set fso = Nothing

I probally do not have to remind you, that you should always use scripts with care.
Therefore this posting is "as is" without express or implied warranty.

Printscreen: not available


Ben OostdamBen Oostdam has been working with Windows systems since 1993. Worked for several companies as a system administrator, and is currently a Senior Support Engineer for a large company in the Netherlands.

Disclaimer: The information contained in this website/article is for general information purposes only. The information is provided as is, by Ben Oostdam and while we endeavour to keep the information up to date and correct, we make no representations or warranties of any kind, express or implied, about the completeness, accuracy, reliability, suitability or availability with respect to the website or the information, products, services, or related graphics contained on the website for any purpose. Any reliance you place on such information is therefore strictly at your own risk. In no event will we be liable for any loss or damage including without limitation, indirect or consequential loss or damage, or any loss or damage whatsoever arising from loss of data or profits arising out of, or in connection with, the use of this website. Through this website you are sometimes able to link to other websites which are not under my control. I have no control over the nature, content and availability of those sites. The inclusion of any links does not necessarily imply a recommendation or endorse the views expressed within them. Every effort is made to keep the website up and running smoothly. However, I take no responsibility for, and will not be liable for, the website being temporarily unavailable due to technical issues beyond our control. All entries in these articles, are my individual opinion, or from co-writers and they don't necessary reflect the opinion of my employer.


Friday the 7th, May 2021. All rights reserved.. // Oostdam WebDesign