简体   繁体   中英

Executing CHKDSK for remote computer in Powershell

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?

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 + ":'")

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"
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM