[英]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.我输入的服务器 ping 正常并返回驱动器号,但它没有返回 chkdsk 返回值。 How can I get the chkdsk return value?
如何获得 chkdsk 返回值?
You were close $drive
is just a string as the error suggests.正如错误所暗示的那样,您很接近
$drive
只是一个字符串。 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
.使用
$wmiDrive
您可以执行chkdsk
的方法。
The single quotes prevent the variable from expanding so you could replace "DriveLetter='c:'"
with:单引号可防止变量扩展,因此您可以将
"DriveLetter='c:'"
替换为:
("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.参数 $VigorousIndexCheck 不正确。 Proper name should be $LessVigorousIndexCheck and set to default 'false' because is related to chkdsk parameter /I>>>
正确的名称应该是 $LessVigorousIndexCheck 并设置为默认的 'false' 因为与 chkdsk 参数相关 /I>>>
/I NTFS only: Performs a less vigorous check of index entries. /I NTFS only:对索引条目执行较不严格的检查。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.