添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
怕老婆的针织衫  ·  dos for循环嵌套 ...·  7 月前    · 
行走的太阳  ·  django model 条件过滤 ...·  1 年前    · 

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: '
    $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)"
        $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?

also Add-Content D:\Scripts\expiringCerts\request.inf 'rn_continue_ = "$dnsipname"' does not write to file. – user5711825 Nov 30 '16 at 13:19
  • The Read-Host cmdlet:

    $msg = 'Do you want to add alternative DNS names or IPs into Certificate? [Y/N]'
        $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
        $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'
        choice /c yn /m $msg
        $response = $LASTEXITCODE
        if ($response -eq 0) {
            # prompt for name/address and add to certificate
    } until ($response -eq 1)
                    Nice summary. However - I personally don't like user prompts at all. In this particual case I would pass a string[] to the script containing all DNS / IPs the consumer wants to add. There is no need for prompts (in my world) ;-)
                        – Martin Brandl
                    Nov 30 '16 at 13:53
                    @MartinBrandl Prompts do have their uses, although I have to agree that most of the time I prefer being able to pass required information as parameters.
                        – Ansgar Wiechers
                    Nov 30 '16 at 14:21
            

    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')
            $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')
                    mmmmm :) thanks, do you know by the any chance why Add-Content D:\Scripts\expiringCerts\request.inf ('rn_continue_ = "{0}"' -f $dnsipname) does not work?
                        – user5711825
                    Nov 30 '16 at 13:37
                    try Add-Content -Path "D:\Scripts\expiringCerts\request.inf" -value ('rn_continue_ = "{0}"' -f $dnsipname)
                        – Martin Brandl
                    Nov 30 '16 at 13:37
                    I would think -eq "y" works fine since it is case insensitive by default. No need for the match operator.
                        – Matt
                    Nov 30 '16 at 13:46
            

    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") {
            $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")
                    You can mark my answer as accepted, this helps others know your question has been answered and also helps with future searches. Here's how you do it: meta.stackexchange.com/questions/5234/…
                        – James C.
                    Nov 30 '16 at 13:39
                    As I read your script, it will continue the do loop until the user press y or Y, but it should continue as long he is pressing y or Y. Also $strQuit -eq "Y" -or "y" will always return $true thus its an endless loop.
                        – Martin Brandl
                    Nov 30 '16 at 13:39
                    Yes, also don't to remove -or "y" otherwise he won't be able to get out of the loop....
                        – Martin Brandl
                    Nov 30 '16 at 13:42
                    Hm, your script don't ask whether he wants to add alternative DNS names, it just performs the action and then ask to add another. you probably have to refactor this as well. Your if statement is obsolete as well....
                        – Martin Brandl
                    Nov 30 '16 at 13:45
            

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.

    site design / logo © 2020 Stack Exchange Inc; user contributions licensed under cc by-sa 4.0 with attribution required. rev 2020.2.7.36004
  •