do until user input yes/no

前端 未结 3 1973
梦谈多话
梦谈多话 2020-12-02 01:17

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          


        
相关标签:
3条回答
  • 2020-12-02 01:28

    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)
      
    0 讨论(0)
  • 2020-12-02 01:37

    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')
    }
    
    0 讨论(0)
  • 2020-12-02 01:39

    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")
    }
    
    0 讨论(0)
提交回复
热议问题