这是我提出的,但它说"驱动器号不存在"
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"
}
}
我输入的服务器正在ping罚款并返回驱动器号,但它没有返回chkdsk返回值。如何获得chkdsk返回值?
答案 0 :(得分:1)
你很接近$drive
只是一个错误暗示的字符串。
为了执行它,您需要捕获行的返回。由于查询返回一个对象数组,我们只想确保只返回一个(即使你编码只返回一个)。
$wmiDrive = Get-WmiObject -Class Win32_Volume -Filter ("DriveLetter='c:'") | Select-Object -First 1
$wmiDrive.chkdsk($false, $true, $false, $false, $false, $false)
使用$wmiDrive
,您可以执行chkdsk
。
单引号可防止变量扩展,因此您可以将"DriveLetter='c:'"
替换为:
("DriveLetter='" + $drletter + ":'")
答案 1 :(得分:1)
我认为你过度复杂了。尝试
$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 :(得分:0)
参数$ VigorousIndexCheck不正确。专有名称应为$ LessVigorousIndexCheck并设置为默认值'false',因为它与chkdsk参数/ I >>>
相关/ I仅限于NTFS:执行不太严格的索引检查 项。