Link to home
Start Free TrialLog in
Avatar of psych0naut
psych0naut

asked on

Automate Scandisk & Defrag

I've been looking for a way to automate running scandisk and windows xp's native defragmenter after hours on our workstations.  I found the following batchfile on TechRepublic:

********************************************************************************
REM chkdsk and defrag automation
for /F "eol= tokens=1 delims=( " %%i in (DrvLtr.txt) do set DrvLtr=%%i& call :dsKchk
 
:dsKchk
If %DrvLtr% == end goto :eof
chkdsk %DrvLtr%
If not errorlevel 3 goto :defrag
If not exist %DrvLtr%\winnt If not exist %DrvLtr%\windows If not exist %DrvLtr%\pagefile.sys goto :dskchkon
 
:dskchkoff
cd\
%DrvLtr%
echo Y chkdsk /F /R
goto :defrag
 
:dskchkon
chkdsk %DrvLtr% /F /R
 
:defrag
cd\
%DrvLtr%
defrag %DrvLtr% -b
defrag %DrvLtr%
:EOF
********************************************************************************

This is placed in a directory along with a text file called "DrvLtr.txt" which lists which drives the operations will be performed on as follows:


********************************************************************************
C:
D:
end
********************************************************************************

The problem is, if run that from the server it will want to perform the operations on the C and D drive of the server.  How can I edit this so that it performs the actions on each individual workstation?  

Thx in advance!
Avatar of Rob Stone
Rob Stone
Flag of United Kingdom of Great Britain and Northern Ireland image

Is running scheduled tasks not an option, or copying the file to each machine?
Avatar of ScrptMasta
ScrptMasta

I don't do batch scripting, however to accomplish this in VB you would simply use the scripts and schedule them via the task manager to run when you want them to. You could assign them as Startup or even Shutdown scripts via Group Policy, this is the easiest way to do it.

Runs ChkDks.exe against drive D on a computer.
**********************************************************
Const FIX_ERRORS = True

strComputer = "."

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

Set objDisk = objService.Get("Win32_LogicalDisk.DeviceID='D:'")
errReturn = objDisk.ChkDsk(FIX_ERRORS)

Wscript.Echo errReturn
**********************************************************

Defragments volume D on a computer. If you modify this script to defragment a different volume (such as X), note that your WQL query must specify the drive letter followed by a colon and then followed by two forward slashes. Thus volume X would be listed as X:\\.
**********************************************************
strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colVolumes = objWMIService.ExecQuery _
    ("Select * from Win32_Volume Where Name = 'D:\\'")

For Each objVolume in colVolumes
     errResult = objVolume.Defrag()
Next
**********************************************************

Avatar of psych0naut

ASKER

To schedule these scripts using Task Manager would the script need to reside on the computer being scanned/defragged or could the scripts be located at \\server\share\scripts.vbs   ??    If run from the server would it perform the scripted actions on the local machine which points to the scripts in task scheduler???
If you want the scripts to run local then leave them the way they are and they will have to be copied to each machine. I normally have a C:\Windows\Scripts folder in which I store all of my scripts that I need each computer to have. You can get the scripts on each computer in a couple of different ways. You could use yet another script, you could send them out via a package in AD, you can put them on a share as you are asking and have the logon script copy the file over to the pc when they log on.

Now if you simply want to run the script against another machine from your machine then you will need to change the "strComputer" variable with the IP or Host Name of the other computer.
OK...how about this...could you make a script for me that would do this:

-Check "C:\Windows\Scripts" for the name of the defrag/scandisk script.
-If present, schedule the script to run via windows task scheduler every Friday at Midnight.  
-If absent, create the folder in question, copy the script there, and then schedule as above.

Also, I would obviously not want the script to schedule another instance of the scan/defrag script so perhaps we would need to have the aforementioned script check to see if scan/defrag is already scheduled before adding it??
ASKER CERTIFIED SOLUTION
Avatar of ScrptMasta
ScrptMasta

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hey ScrptMasta!  I posted the new question as requested...thx again for your help!