0

This is what I have come up with but it says "Drive Letter does not exist"

clear
$server = Read-Host "Enter server name"
$driveletter = Read-Host "Enter Drive Letter"
$disks = Get-WmiObject -ComputerName $server -Class Win32_LogicalDisk -Filter  ("DeviceID='" + $driveletter+":'")

$FixErrors          = $false    # does not fix errors 
$VigorousIndexCheck = $true     # performs a vigorous check of the indexes
$SkipFolderCycle    = $false    # does not skip folder cycle checking.
$ForceDismount      = $false    # will not force a dismount (to enable errors to be   fixed)
$RecoverBadSecors   = $false    # does not recover bad sectors
$OKToRunAtBootup    = $false    # runs now, vs at next bootup

foreach($disk in $disks) 
{
    $deviceID = $disk.DeviceID
    Write-Host $deviceID

    If ($deviceID -eq $driveletter)
    {
         $res = $c.chkdsk($FixErrors, 
                    $VigorousIndexCheck, 
                    $SkipFolderCycle, 
                    $ForceDismount,
                     $RecoverBadSecors, 
                   $OKToRunAtBootup)

          $result = $res.ReturnValue
          Write-Host $result

}
Else
{
    "Drive letter does not exist"
}
}

The server I entered is pinging fine and returns the drive letter also but it is not returning the chkdsk return value. How can I get the chkdsk return value?

9
  • 1
    you have to asign the resuslt of the wmi request to a variable, then use it instead of user the $drive var. $theDrive=Get-WmiObject -Class Win32_Volume -Filter ("DriveLetter='$drive'") $theDrive.ChkDsk($false, $true, $false, $false, $false, $false) Commented Aug 25, 2014 at 12:58
  • I am getting the error-- Get-WmiObject : Invalid query "select * from Win32_LogicalDisk where DriveLetter='E'"
    – kay5
    Commented Aug 25, 2014 at 13:23
  • add the : after the drive -> Get-WmiObject -Class Win32_Volume -Filter ("DriveLetter='E:'") Commented Aug 25, 2014 at 13:26
  • Thank you, Kayasax but I have a question. I need to pass a variable in which the user has given the drive letter we want chkdsk to run on. $thedrive = Get-WmiObject -Class Win32_Volume -Filter ("DeviceID='$drletter':") This returns the error "You cannot call a method on a null-valued expression."
    – kay5
    Commented Aug 25, 2014 at 13:39
  • $drive = Read-Host "Enter Drive Letter to perform CHKDSK (read-only): " and after you can do $drive+=":" Commented Aug 25, 2014 at 13:44

3 Answers 3

1

I think you're over complicate it. try

$driveletter = Read-Host "Enter Drive Letter"
$disk = Get-WmiObject -Class Win32_LogicalDisk -Filter  ("DeviceID='" + $driveletter+":'")

if ($disk){
    $FixErrors          = $false    # does not fix errors 
    $VigorousIndexCheck = $true     # performs a vigorous check of the indexes
    $SkipFolderCycle    = $false    # does not skip folder cycle checking.
    $ForceDismount      = $false    # will not force a dismount (to enable errors to be   fixed)
    $RecoverBadSecors   = $false    # does not recover bad sectors
    $OKToRunAtBootup    = $false    # runs now, vs at next bootup
    $res = $disk.chkdsk($FixErrors, 
                    $VigorousIndexCheck, 
                    $SkipFolderCycle, 
                    $ForceDismount,
                     $RecoverBadSecors, 
                   $OKToRunAtBootup)
     #bonus from http://msdn.microsoft.com/en-us/library/aa384915(v=vs.85).aspx     
     $char=$res.returnvalue
     If ($char -ge 0 -and $char -le 5) {
        switch ($char) {
            0{"00-Success"}
            1{"01-sUCCESS (volume locked and chkdsk scheduled for reboot"}
            2{"02-unsupported file system"}
            3{"03-Unknown file system"}
            4{"04-No Media in drive"}
            5{"05-Unknown Error"}
        }
    }

    Else {
        "{0} - *Invalid Result Code*" -f $char
    }
}

Else
{
    "Drive letter does not exist"
}
2
  • Thank you, Kayasax. I got the script working and once again thanks to You and @Matt for helping me out on this script. I am not sure how to mark this question as answered.
    – kay5
    Commented Aug 26, 2014 at 11:56
  • click on the green tick beside the response ;) Commented Aug 26, 2014 at 11:58
1

You were close $drive is just a string as the error suggests. In order to execute it you need to capture the return from your line. Since the query returns an array of objects we just want to ensure only one is returned ( Even though you have it coded to only return one).

$wmiDrive = Get-WmiObject -Class Win32_Volume -Filter ("DriveLetter='c:'") | Select-Object -First 1
$wmiDrive.chkdsk($false, $true, $false, $false, $false, $false)

With $wmiDrive you can execute the method for chkdsk.

The single quotes prevent the variable from expanding so you could replace "DriveLetter='c:'" with:

("DriveLetter='" + $drletter + ":'")
7
  • I am getting the error-- Get-WmiObject : Invalid query "select * from Win32_LogicalDisk where DriveLetter='E'"
    – kay5
    Commented Aug 25, 2014 at 13:22
  • See the comment from Kayasax. Also my example shows that : is needed.
    – Matt
    Commented Aug 25, 2014 at 13:34
  • Thank you, Matt but I have a question. I need to pass a variable in which the user has given the drive letter we want chkdsk to run on. $thedrive = Get-WmiObject -Class Win32_Volume -Filter ("DeviceID='$drletter':") This returns the error "You cannot call a method on a null-valued expression."
    – kay5
    Commented Aug 25, 2014 at 13:38
  • $thedrive is null since the Get-WmiObject cmdlet returned nothing. How do you define $drletter since it is not in your example?
    – Matt
    Commented Aug 25, 2014 at 13:44
  • I am sorry Matt. My mistake. I am getting the drive letter as user input in the beginning of the script: $drletter = Read-Host "Enter Drive Letter to perform CHKDSK (read-only): "
    – kay5
    Commented Aug 25, 2014 at 14:05
0

Parameter $VigorousIndexCheck is not correct. Proper name should be $LessVigorousIndexCheck and set to default 'false' because is related to chkdsk parameter /I>>>

/I NTFS only: Performs a less vigorous check of index entries.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.