Script Library
Function fChkDsk(sComputer, sDrive)
Search thecollinshouse script library:
Script Type: vbScript function
Description:
This function will execute a chkdsk, or if the drive is the main system drive, it will schedule the chkdsk for the next restart. The script uses WMI so the chkdsk can be scheduled on a remote computer. Tested on Windows XP.
Script:

'# D.Collins - 11:37 29/04/2008
'# Executes or schedules a Check Disk (ChkDsk) on a computer

strComputer = InputBox ("Enter computer to run chkdsk on:",, ".")
If strComputer = "" Then WScript.Quit

strDrive = InputBox ("Enter drive to run chkdsk on:" & vbCrlf & vbCrlf & "Note: There will be no more dialogs until the chkdsk is complete.",, "C:")
If strDrive = "" Then WScript.Quit

MsgBox fChkDsk (strComputer, strDrive),vbSystemModal, ""



Function fChkDsk(sComputer, sDrive)
    Dim temp, oWMIService, oDisk
    Const FixErrors = True
    Const SkipVigorousIndexCheck = False
    Const SkipFolderCycle = False
    Const OKToRunAtBootUp = True

    If Len(sDrive) <> 2 Or Right(sDrive, 1) <> ":" Then
        MsgBox "Error in parameters: sDrive must be in the format ""D:""" & vbCrlf & "Value received: " & sDrive, vbCritical + vbSystemModal, "Win32_LogicalDisk - ChkDsk"
        Exit Function
    End If
    

    Set oWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & sComputer & "\root\cimv2")

    Set oDisk = oWMIService.Get("Win32_LogicalDisk.DeviceID='" & sDrive & "'")

    temp = oDisk.ChkDsk(FixErrors, SkipVigorousIndexCheck, SkipFolderCycle, False, False, OKToRunAtBootUp)

    Select Case temp
        Case 0 temp = temp & " - Success - Chkdsk Completed"
        Case 1 temp = temp & " - Success - Locked and Chkdsk Scheduled on Reboot"
        Case 2 temp = temp & " - Failure - Unknown File System"
        Case 3 temp = temp & " - Failure - Unknown Error"
    End Select

    fChkDsk = temp
End Function

Script Library Index | thecollinshouse Home
25137