Jump to content

Recommended Posts

Posted

We use the following vbs script

 

'defrag.vbs
'Defrags all hard disks - Can be run as a Scheduled Task
'Modified to create an error log and display it
'© Doug Knox - 4/13/2002

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
Next

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
else
 ErrStr(x) = ErrStr(x) &  "Drive " & d & " Defrag aborted with an unknown error level: " & return & vbCRLF
end if

      End If
  Next

'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
     MyFile.WriteLine(ErrStr(I))
  Next
  MyFile.Close

Return = WshShell.Run(Drive & "\defragreport.txt",3,True)

Set WshShell = Nothing
Set fso = Nothing

 

We schedule it on the servers to run at weekends when quiet, on the workstations we schedule it to run on Wednesday lunchtimes when the machines are likely to be on but not used much. To roll it out we use the following script to set up the task:

 

'task.vbs - schedules defrag.vbs

strComputer = "."

Set objWMIService = GetObject("winmgmts:" _

& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

 

Set objNewJob = objWMIService.Get("Win32_ScheduledJob")

 

errJobCreated = objNewJob.Create _

("c:\defrag\defrag.vbs", "********123000.000000-000", _

True , 4, , , JobID)

Wscript.Echo errJobCreated

 

The following script is used to install the defrag.vbs script on the workstations and to run the task.vbs. It checks to see if it is already installed, if it isn't it makes the folder, copies the files and runs task.vbs to set up the scheduled task. We run it as a startup script via a group policy.

 

REM defrag.bat - startup script to install weekly defrag

@ECHO OFF

REM --- Check for an existing installation of defrag

if exist "c:\defrag\defrag.vbs" goto _End

REM --- Deploy to Windows 2000/XP/2003

md "c:\defrag\"

copy "\\server\packages$\defrag\*.*" "c:\defrag\" /y

cscript c:\defrag\task.vbs

REM --- End of the script

:_End

 

 

Hope this is of some use to someone!

  • Thanks 3
Posted

We use o&o's defrag software. Weekly stealth on the servers and weekly stealth on our workstations. The only reason I did not go with Diskeeper was because of their constant daily ringing me up and flood of E-mail advertising.....

 

Note: The Admins Admin Bar [ see below ] has a shortcut to the Windows defrag tool.....

Posted

Apparently automatic defragmentation is the 'hot' thing now, and scheduling a defrag is old and busted. :p From what my friends* told me, they like auto because once it's installed on the workstations they don't need to waste time scheduling or executing defrags for large numbers of machines weekly. The software uses I/O sensing and runs on unused system cycles, so no supervision required...truly install and forget it.

 

(*they are the IT guys, I am merely a dilettante.)

Posted
Apparently automatic defragmentation is the 'hot' thing now, and scheduling a defrag is old and busted. :p From what my friends* told me, they like auto because once it's installed on the workstations they don't need to waste time scheduling or executing defrags for large numbers of machines weekly. The software uses I/O sensing and runs on unused system cycles, so no supervision required...truly install and forget it.

 

(*they are the IT guys, I am merely a dilettante.)

 

The o&o defrag software does that.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...