# Run scandisk and defrag on a remote computer.



## StumpedTechy

I have a situation. We want to go ahead and clean up our PC's on a regular basis. in order to do this we need to clean out temporary files, cookies, and I.E. cached files as well as run a scandisk and defrag.

I want to put this into a VBS script. The major complication in this that i am seeing is the running scandisk and defrag remotely...

Can anyone provide some pointers? Can these 2 programs be kicked of on a remote machine using vbs?


----------



## draceplace

We use a product called Diskeeper and it works really well. It has a set and forget it schedule option.... Free trail @
http://www.diskeeper.com/defrag.asp


----------



## StumpedTechy

Sounds good but I 1) don't have any budget for this and 2) can't worry about installing software on 400+ remote machines....


If there are any other ideas let them come please.


----------



## draceplace

Well installing once to 400 remote machines or kicking off a 400 scan/defrags and monitoring the results every a month?? But the budget is another thing. 

Anyway this will add a scheduled job to a pc to run 1230 pm M W F. You would use Defrag.exe and/or chkdsk (and all its swithces). You'll have to play around to figure out the day time parameters. It may be documented somewhere.


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

Set objNewJob = objWMIService.Get("Win32_ScheduledJob")

errJobCreated = objNewJob.Create _
("Notepad.exe", "********123000.000000-420", _
True , 1 OR 4 OR 16, , , JobID) 
Wscript.Echo errJobCreated

To run a job in script the syntax is

wshShell.Run 'defrag.exe'
or 
job1 = "%Systemroot%\system32\defrag.exe d"
wshShell.Run job1


----------



## StumpedTechy

Ugh I have seen that code before.. the problem is some of our PC's don't keep correct time and I just really wanted to start the exe remotely...

As far as running around to 400 machines I was hoping to avoid this because i can get a machine list and then I can write the failed connections (due to PC's being turned off) to a new list so really it would be running it after hours every weekend or so

I was holing these was something like

defrag *computername*
scandisk *computername*
del computernames temp files and internet files
last run shutdown exe on the computer.

Then I can set the VBS to autorun on my computer and have my computer omiited from the list and this would then do everyones but mine which I could then do it manually...

I really was thinking something along this line but it doesn't work...

strComputer = 
job1 = "\\" & strComputer & "\C$\Windows\system32\defrag.exe"
Set WshShell = CreateObject("Wscript.Shell")
wshShell.Run job1

Where in the strComputer field I put the ocmputername from the list... But I'll be darned if this won't run it to a remote machine... it still is trying to run the job from the remote machine onto the machine I am running the vbs on...

As far as checking results we just want to have it ran were not really going to be monitoring this (except for glowing errors like this is not running on 300 of the 400 computers).


----------



## draceplace

This will set the time on a pc. We have it in the logon script..

'This Sets the time

wshShell.Run "Net Time /Domain:YourDomainNameHere /set /yes",0,False


----------



## draceplace

This code works...I left some commented lines in it..

Const ForWriting = 2
Const ForAppending = 8
Set fs = CreateObject("Scripting.FileSystemObject")
LogFile = "C:\ascripts\new1\notepad.csv"
Dim CurrentDate, CurrentTime
CurrentDate = Date
CurrentTime = Time
DateTime = CurrentDate & "," & CurrentTime

On Error Resume Next

'/ Main
'//////
Set f = fs.Opentextfile(LogFile, ForAppending, True)
'Set f = fs.Opentextfile(LogFile, ForWriting, True)
'/f.WriteLine "ComputerName,Date,Time,Process ID"
'/remote_machine_number = "itd-pc13"
remote_machine_number = InputBox("Enter the computer name. ")
remote_machine_name = remote_machine_number
Set objLocator = CreateObject("WbemScripting.SWbemLocator")
Set objService = objLocator.ConnectServer(remote_machine_name, "Root\DEFAULT")
If Err.Number <> 0 Then
f.WriteLine remote_machine_name & ", Error " & Err.Number & "," & Err.Description
WScript.Echo "Error No Contact!!." 
Err.Clear
Else

Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & remote_machine_name & "\root\cimv2:Win32_Process")
errReturn = objWMIService.Create("calc.exe", null, null, intProcessID)
If errReturn = 0 Then
Wscript.Echo "calc.exe was started with a process ID of " & intProcessID & "."
Else
 Wscript.Echo ".exe could not be started due to error " & errReturn & "."
End If
End If
'/Next
f.Close
WScript.Echo "All Done."


----------



## VaporTrace

Easy batch workaround or rework it into a script.
Task scheduler > if exist "L:\dumbfile.run" deltree /y c:\windows\temp > md c:\windows\temp 

Run like every 10 minutes. If dumbfile is there it runs. If not NOT.


----------

