vbscript to defrag windows 2000 PCs

Posted on 2005-04-22
Medium Priority
Last Modified: 2010-08-05
Is there anyway to defrag a remote windows 2000 PC using VBScript?
Question by:TASINetwork
LVL 97

Expert Comment

by:Lee W, MVP
ID: 13842772
The built in Defragger is very basic and I've never heard of a way to make it remotely defrag PCs.  If you want this feature, I suggest you look into a 3rd party program such as Diskkeeper.

Author Comment

ID: 13842831
I know I won't be able to do it directly with the built in prog.  I'm hoping to find some alternate means.  I've thought about autodefrag, but I'm not sure if it will run if noone is logged in.

I can run programs remotely using psexec.  Unfortunately, none of the 3rd party licensed product will work with me with the licensing (I'm a consultant for small companyies, and they aren't going to want to pay for quite a few more PCs than they are running - Otherwise I would be using DefragManager).
LVL 38

Expert Comment

ID: 13845076
The trouble with defrag in w2k is that it has no command line interface.  Here is a VBScript that runs defrag using the somewhat flaky Sendkeys method:

You might find it will run best if no one is using the PC as changing the window focus at the wrong time will cause problems with this method.

Maybe you can run this vb script remotely using psexec?  I've never used psexec before.
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 13862176
This works for XP never tested it for Windows 2000.  It uses WMI to schedule a job using the computer's system account.  strComputer="." is the current machine you can put a  remote computer name in there too.
Just copy this out to a txt file and rename it .vbs to test.  You also need to make sure the windows path is valid.  Like I said it works on XP not sure about Windows 2000.

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" _
    & strComputer & "\root\cimv2")
Set objNewJob = objWMIService.Get("Win32_ScheduledJob")
errJobCreated = objNewJob.Create _
    ("C:\windows\system32\defrag.exe c: -f", "********200000.000000-300", _
        True ,1 OR 2 OR 4 OR 8 OR 16 OR 32 OR 64, , False, JobId)
If errJobCreated <> 0 Then
Wscript.Echo "Error on task creation"
Wscript.Echo "Task created"
End If

Author Comment

ID: 13863238
defrag.exe only exists in WinXP.  I appreciate the attempt though.
LVL 97

Accepted Solution

Lee W, MVP earned 1500 total points
ID: 14393357
I'm not sure I agree.  Just because he didn't get a way to do it doesn't mean it's wrong.  As I said to start with, "The built in Defragger is very basic and I've never heard of a way to make it remotely defrag PCs.  If you want this feature, I suggest you look into a 3rd party program such as Diskkeeper".

I think this may be the only valid answer.

Author Comment

ID: 14393424
defrag.exe won't work, so it's wrong.  However, I have yet to find a way, so I'm going to accept your final arguement...  3rd party as the closest that I guess we can come...
LVL 97

Expert Comment

by:Lee W, MVP
ID: 14393443
Microsoft often licenses technology from other companies to include in Windows.  The Windows backup program is licensed from Veritas.  The defrag, from Executive Software.  These programs are often basic and won't work over a network specifically to encourage you to buy their full products.

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question