$drives = (Get-Disk | Where-Object -FilterScript {$_.BusType -ne "USB"} | Get-Partition | Get-Volume).DriveLetter
do
{
$drive = Read-Host -Prompt "Type the drive letter"
IF ($drives -eq $drive)
{
"exists"
}
IF ($drive -eq "exit")
{
break
}
else
{
Write-Host "The disk does not exist"
}
}
until ($drives -eq $drive)
如何用“按Enter跳过”问题而不是$ drive -eq“退出”来实现部分?
答案 0 :(得分:2)
只需检查字符串是否为空:
IF ($drives -eq $drive)
{
"exists"
}
ELSEIF ([string]::IsNullOrEmpty($drive))
{
break
}
else
{
Write-Host "The disk does not exist"
}