do until user input yes/no

Solution 1:

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)
    

Solution 2:

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