powershell - 实现 "Press Enter to skip"

标签 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“exit”的部分?

最佳答案

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

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

关于powershell - 实现 "Press Enter to skip",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56789226/

相关文章:

powershell - 使用 Powershell 监听按键,但不要等待

powershell - 使用 PowerShell 循环遍历目录中的文件

PowerShell显示耗时

json - 如何在新的 Windows 终端配置文件中使用 Anaconda 运行 powershell.exe?

visual-studio - 没有开发者模式/开发者许可证,无法旁加载 Win10 UWP 应用程序

Powershell - 展开子数组并重命名字段

powershell - 如何使用Powershell脚本中的参数调用可执行文件

powershell - Powershell快速在大量文件中搜索短语

windows - 我应该为此使用 PowerShell 还是 CMD.exe?

json - 创建JSON字符串,PowerShell对象