Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Schedule Autochk.exe 1

Status
Not open for further replies.

FloDiggs

MIS
Jan 20, 2007
296
US
I'm trying to write a script to dynamically schedule Autocheck for all physical hard drives on my servers. I found this snippet, which works, but you have to statically assign the drive letter.
Code:
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set objDisk = objWMIService.Get("Win32_LogicalDisk")
errReturn = objDisk.ScheduleAutoChk(Array("D:"))
Wscript.Echo (errReturn)
So, I made a few modifications so that it would dynamically find the physical drives on the server, but I don't know how to feed the drive letter into the objDisk.ScheduleAutoChk statement. Here is my code:
Code:
strComputer = "."
set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
set colDisks = objWMI.ExecQuery _
    ("Select * from Win32_LogicalDisk WHERE DriveType = '3'")
For Each objDisk in colDisks
	errReturn = objDisk.ScheduleAutoChk (Array(objDisk.DeviceID))
	if errReturn = 0 then
	   WScript.Echo "Autochk scheduled on next reboot for drive " & objDisk.DeviceID
	else
	   WScript.Echo "Error running chkdsk on drive " & objDisk & ". Error Code: " & errReturn
	end if
Next
What am I doing wrong?

 
What is happening is that you are referencing a different object for "objDisk" in the second script.

Here's a proposed that adds the object you need as "objWMIDisk". I changed the code to build an array of drives in the For...Next structure, then call the ScheduleAutoChk method and pass it the pre-built array.

Code:
Option Explicit

Dim strComputer, objWMI, objWMIDisk, colDisks, ctr, objDisk, arrDisks(), errReturn

strComputer = "."
set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set objWMIDisk = objWMI.Get("Win32_LogicalDisk")
set colDisks = objWMI.ExecQuery _
    ("Select * from Win32_LogicalDisk WHERE DriveType = '3'")

ctr = 0
For Each objDisk in colDisks
	ReDim Preserve arrDisks(ctr)
	arrDisks(ctr) = objDisk.DeviceID
	ctr = ctr + 1
Next

errReturn = objWMIDisk.ScheduleAutoChk(arrDisks)
if errReturn = 0 then
	WScript.Echo "Autochk scheduled on next reboot for drive " & Join(arrDisks, ", ")
Else
	WScript.Echo "Failed to schedule Autochk. Error Code: " & errReturn
end If

Now when I was testing this code, I noticed that it didn't cause my system to run a chkdsk when I booted up. I found that the original script doesn't either.

According to MSDN, this WMI command only schedules chkdsk for disks that have the "dirty bit" enabled.
[URL unfurl="true"]http://msdn.microsoft.com/en-us/library/aa393252(VS.85).aspx[/url]

Your mileage may vary.

PSC

Governments and corporations need people like you and me. We are samurai. The keyboard cowboys. And all those other people out there who have no idea what's going on are the cattle. Mooo! --Mr. The Plague, from the movie "Hackers
 
I was afraid of that. My first attempt was to use objDisk.chkdsk, but that didn't cause it run at start up either. I guess I'll have to find a way to force it. Thank you very much for the help!
 
Part of the objDisk.chkdsk method will allow you to check on next reboot, but it may dismount and run chkdsk immediately on non-os partitions (based on lock status of the drive)...

objdisk.chkdsk(true, , , , , true)

See: [URL unfurl="true"]http://msdn.microsoft.com/en-us/library/aa384913(VS.85).aspx[/url]

Or you may want to explore ways of triggering the dirty bit on the volumes you're interested.

PSC

Governments and corporations need people like you and me. We are samurai. The keyboard cowboys. And all those other people out there who have no idea what's going on are the cattle. Mooo! --Mr. The Plague, from the movie "Hackers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top