实施“按Enter跳过”

时间:2019-06-27 10:40:02

标签: powershell

$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“退出”来实现部分?

1 个答案:

答案 0 :(得分:2)

只需检查字符串是否为空:

IF ($drives -eq $drive)
{
    "exists"
}
ELSEIF ([string]::IsNullOrEmpty($drive))
{
    break
}
else
{
    Write-Host "The disk does not exist"
}