user5711825
user5711825

Reputation: 115

do until user input yes/no

I am trying to write a simple do..until loop and it does not work:

$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")

This one does loop twice only, but it should loop every time I hit Y but when I hit N or n It should break.

Any ideas?

Upvotes: 7

Views: 37435

Answers (3)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200373

In PowerShell you have basically three options to prompt a user for a yes/no choice.

  • The 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')
    

    Use -like 'y*' and -like 'n*' if you want to ignore trailing characters in the response.

  • The PromptForChoice() method:

    $title   = 'Certificate Alternative Names'
    $msg     = 'Do you want to add alternative DNS names or IPs?'
    $options = '&Yes', '&No'
    $default = 1  # 0=Yes, 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)
    
  • The choice command:

    $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)
    

Upvotes: 24

Martin Brandl
Martin Brandl

Reputation: 58981

I think you don't need two do-until loop. I also would prefer a do-while (while he is confirming with y or Y).

Your string interpolation on the Add-Content doesn't work because you are using single quotes. I would leverage a format string here:

$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')
}

Upvotes: 4

henrycarteruk
henrycarteruk

Reputation: 13227

You need to have the condition that you are evaluating in the until statement ($strQuit for your example) inside the do loop for it to be updated.

Wrapping the do loop in an if statement also makes the process friendlier to the user as it ask them before continuing to prompt.

$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")
}

Upvotes: 2

Related Questions