# wmi-chkdsk.ps1
# Runs chkdsk to checks a local hard disk.
# Script just checks C:\
# thomas lee - tfl@psp.co.uk
# Helper function to Decode Return Code
function Get-DfragReturn {
param ([uint16] $char)
# parse and return values
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}
Return
}
# Get all local disks
# then get first drive (C:\)
$disks=gwmi win32_Volume
$c=$disks | where {$_.name -eq "C:\"}
# print start
"Checking {0} on system: {1}" -f $c.name,$c.SystemName
# Now specify chkdsk paramaters and call chkdsk
$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
$start=get-date
"Commencing Defrag"
$res=$c.chkdsk($FixErrors,
$VigorousIndexCheck,
$SkipFolderCycle,
$ForceDismount,
$RecoverBadSecors,
$OKToRunAtBootup)
$finish=get-date
# Now Display returndvalue
"Chkdsk call returned: {0}" -f (Get-DfragReturn($res.ReturnValue))
# Finally print time elapsed
"Starting Check Disk at: {0}" -f $start
"Finished at {0}" -f $finish
$duration = $finish-$start
"Elapsed time {0} minutes" -f ($duration.totalminutes.tostring("0.00"))
This script when run on a Longhorn Beta 2 system produces the followign output:
PS C:\Users\Administrator> .\wmi-chkdsk.ps1
Checking C:\ on system: LHS1
Commencing Defrag
Chkdsk call returned: 00-Success
Starting Check Disk at: 16/06/2007 16:45:55
Finished at 16/06/2007 16:50:31
Elapsed time 4.59 minutes
PS C:\Users\Administrator>