直到用户输入是/否

时间:2016-11-30 13:18:37

标签: powershell

我正在尝试编写一个简单的do..until循环,但它不起作用:

$yesNo = Read-Host -Prompt 'Do you want to add alternative DNS names or IPs into Certificate? Y/N: '

do {
    $dnsipname = Read-Host -Prompt "Please input DNS or IP as dns=alternativeCNname or ip=ipName: "
    Write-Output "$dnsipname" 
    $strQuit = Read-Host " do you want to add another DNS? (Y/N)"
    do {
        $dnsipname = Read-Host -Prompt "Please input DNS or IP as dns=alternativeCNname or ip=ipName: "
        Write-Output "$dnsipname" 
        Add-Content D:\Scripts\expiringCerts\request.inf '`r`n_continue_ = "$dnsipname"'
    } until ($strQuit -eq "Y" -or "y")
} until ($yesNo -eq "Y" -or "y")

这个只循环两次,但是每当我点击Y时它都应该循环但是当我点击Nn它应该会中断。

有什么想法吗?

3 个答案:

答案 0 :(得分:7)

在PowerShell中,您基本上有三个选项可以提示用户选择是/否。

  • Read-Host cmdlet:

    $msg = 'Do you want to add alternative DNS names or IPs into Certificate? [Y/N]'
    do {
        $response = Read-Host -Prompt $msg
        if ($response -eq 'y') {
            # prompt for name/address and add to certificate
        }
    } until ($response -eq 'n')
    

    如果您想忽略回复中的尾随字符,请使用-like 'y*'-like 'n*'

  • PromptForChoice()方法:

    $title = 'Certificate Alternative Names'
    $msg   = 'Do you want to add alternative DNS names or IPs?'
    
    $yes = New-Object Management.Automation.Host.ChoiceDescription '&Yes'
    $no  = New-Object Management.Automation.Host.ChoiceDescription '&No'
    $options = [Management.Automation.Host.ChoiceDescription[]]($yes, $no)
    $default = 1  # $no
    
    do {
        $response = $Host.UI.PromptForChoice($title, $msg, $options, $default)
        if ($response -eq 0) {
            # prompt for name/address and add to certificate
        }
    } until ($response -eq 1)
    
  • choice命令:

    $msg = 'Do you want to add alternative DNS names or IPs into Certificate'
    do {
        choice /c yn /m $msg
        $response = $LASTEXITCODE
        if ($response -eq 0) {
            # prompt for name/address and add to certificate
        }
    } until ($response -eq 1)
    

答案 1 :(得分:3)

我认为你不需要两个do-until循环。我也希望do-while(当他确认yY时)。

Add-Content上的字符串插值不起作用,因为您使用单引号。我会在这里使用格式字符串:

$yesNo = Read-Host -prompt 'Do you want to add alternative DNS names or IPs into Certificate? Y/N: '
if ($yesNo -eq 'y')
{
    do 
    {
        $dnsipname = read-host -prompt "Please input DNS or IP as dns=alternativeCNname or ip=ipName: "
        Write-output "$dnsipname" 
        Add-Content -Path "D:\Scripts\expiringCerts\request.inf" -value ('`r`n_continue_ = "{0}"' -f $dnsipname)
        $strQuit = Read-Host " do you want to add another DNS? (Y/N)"                
    }
    while($strQuit -eq 'y')
}

答案 2 :(得分:2)

您需要在until语句中对do语句(对于您的示例中为$ strQuit)进行评估,以便更新它。

do语句中包装if循环也会使用户在继续提示之前向用户提供更友好的信息。

$dnsipname = read-host -prompt "Please input DNS or IP as dns=alternativeCNname or ip=ipName: "
Write-Output $dnsipname
Add-Content D:\Scripts\expiringCerts\request.inf '`r`n_continue_ = "$dnsipname"'

if ((Read-Host "Do you want to add another DNS or IP? (Y/N)") -eq "Y") {
    do {
        $dnsipname = Read-Host "Please input DNS or IP as dns=alternativeCNname or ip=ipName: "
        Write-output "$dnsipname" 
        Add-Content D:\Scripts\expiringCerts\request.inf '`r`n_continue_ = "$dnsipname"'
        $strQuit = Read-Host "Do you want to add another DNS or IP? (Y/N)"
    }
    until ($strQuit -eq "N")
}